From 31e842472f46d8aa32e2ef316da245f18806589d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 19 Sep 2014 23:22:26 +0200 Subject: [PATCH] Make number printing functions less generic Now that zend_ulong is 64bit on 64bit platforms, it should be sufficient to always use it, rather than supporting multiple types. API changes: * _zend_print_unsigned_to_buf and _zend_print_signed_to_buf no longer exist. * smart_str(ing)_print_long and smart_str(ing)_print_unsigned no longer exist. * Instead of all these, zend_print_ulong_to_buf and zend_print_long_to_buf should be used. * smart_str_append_generic_ex no longer exists. * smart_str(ing)_append_off_t(_ex) no longer exists, use smart_str(ing)_append_long(_ex) instead. --- Zend/zend_operators.c | 3 +-- Zend/zend_operators.h | 37 ++++++++++++++--------------- ext/standard/php_smart_str.h | 42 ++++++++++----------------------- ext/standard/php_smart_string.h | 24 +++---------------- ext/standard/var.c | 6 ++--- sapi/cli/php_cli_server.c | 10 ++++---- sapi/thttpd/thttpd.c | 6 ++--- 7 files changed, 45 insertions(+), 83 deletions(-) diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index a63b1757e2c..8047aa60199 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -2520,8 +2520,7 @@ ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */ ZEND_API zend_string *zend_long_to_str(zend_long num) /* {{{ */ { char buf[MAX_LENGTH_OF_LONG + 1]; - char *res; - _zend_print_signed_to_buf(buf + sizeof(buf) - 1, num, zend_ulong, res); + char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num); return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0); } /* }}} */ diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index ece77936ebf..ddec1621746 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -894,27 +894,26 @@ static zend_always_inline void fast_is_not_identical_function(zval *result, zval return SUCCESS; \ } -/* input: buf points to the END of the buffer */ -#define _zend_print_unsigned_to_buf(buf, num, vartype, result) do { \ - char *__p = (buf); \ - vartype __num = (num); \ - *__p = '\0'; \ - do { \ - *--__p = (char) (__num % 10) + '0'; \ - __num /= 10; \ - } while (__num > 0); \ - result = __p; \ -} while (0) +/* buf points to the END of the buffer */ +static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) { + *buf = '\0'; + do { + *--buf = (char) (num % 10) + '0'; + num /= 10; + } while (num > 0); + return buf; +} /* buf points to the END of the buffer */ -#define _zend_print_signed_to_buf(buf, num, vartype, result) do { \ - if (num < 0) { \ - _zend_print_unsigned_to_buf((buf), (~((vartype)(num)) + 1), vartype, (result)); \ - *--(result) = '-'; \ - } else { \ - _zend_print_unsigned_to_buf((buf), (num), vartype, (result)); \ - } \ -} while (0) +static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) { + if (num < 0) { + char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1); + *--result = '-'; + return result; + } else { + return zend_print_ulong_to_buf(buf, num); + } +} ZEND_API zend_string *zend_long_to_str(zend_long num); diff --git a/ext/standard/php_smart_str.h b/ext/standard/php_smart_str.h index 25d6fe2db4a..1c8c4183703 100644 --- a/ext/standard/php_smart_str.h +++ b/ext/standard/php_smart_str.h @@ -50,8 +50,6 @@ smart_str_setl((dest), (src), strlen(src)); #define smart_str_append_long(dest, val) \ smart_str_append_long_ex((dest), (val), 0) -#define smart_str_append_off_t(dest, val) \ - smart_str_append_off_t_ex((dest), (val), 0) #define smart_str_append_unsigned(dest, val) \ smart_str_append_unsigned_ex((dest), (val), 0) @@ -106,37 +104,21 @@ static zend_always_inline void smart_str_append_ex(smart_str *dest, const smart_ } } +static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) { + char buf[32]; + char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num); + smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent); +} + +static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) { + char buf[32]; + char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num); + smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent); +} + static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) { smart_str_free(dest); smart_str_appendl(dest, src, len); } - -static inline char *smart_str_print_long(char *buf, zend_long num) { - char *r; - _zend_print_signed_to_buf(buf, num, zend_long, r); - return r; -} - -static inline char *smart_str_print_unsigned(char *buf, zend_long num) { - char *r; - _zend_print_unsigned_to_buf(buf, num, zend_ulong, r); - return r; -} - -#define smart_str_append_generic_ex(dest, num, type, vartype, func) do { \ - char __b[32]; \ - char *__t; \ - _zend_print##func##_to_buf (__b + sizeof(__b) - 1, (num), vartype, __t); \ - smart_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \ -} while (0) - -#define smart_str_append_unsigned_ex(dest, num, type) \ - smart_str_append_generic_ex((dest), (num), (type), zend_ulong, _unsigned) - -#define smart_str_append_long_ex(dest, num, type) \ - smart_str_append_generic_ex((dest), (num), (type), zend_ulong, _signed) - -#define smart_str_append_off_t_ex(dest, num, type) \ - smart_str_append_generic_ex((dest), (num), (type), zend_off_t, _signed) #endif diff --git a/ext/standard/php_smart_string.h b/ext/standard/php_smart_string.h index 36647aa27a7..e052574a346 100644 --- a/ext/standard/php_smart_string.h +++ b/ext/standard/php_smart_string.h @@ -89,8 +89,6 @@ smart_string_append_ex((dest), (src), 0) #define smart_string_append_long(dest, val) \ smart_string_append_long_ex((dest), (val), 0) -#define smart_string_append_off_t(dest, val) \ - smart_string_append_off_t_ex((dest), (val), 0) #define smart_string_append_unsigned(dest, val) \ smart_string_append_unsigned_ex((dest), (val), 0) @@ -119,33 +117,17 @@ __dest->len = __nl; \ } while (0) -static inline char *smart_string_print_long(char *buf, zend_long num) { - char *r; - _zend_print_signed_to_buf(buf, num, zend_long, r); - return r; -} - -static inline char *smart_string_print_unsigned(char *buf, zend_long num) { - char *r; - _zend_print_unsigned_to_buf(buf, num, zend_ulong, r); - return r; -} - #define smart_string_append_generic_ex(dest, num, type, vartype, func) do { \ char __b[32]; \ - char *__t; \ - _zend_print##func##_to_buf(__b + sizeof(__b) - 1, (num), vartype, __t); \ + char *__t = zend_print##func##_to_buf(__b + sizeof(__b) - 1, (num)); \ smart_string_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \ } while (0) #define smart_string_append_unsigned_ex(dest, num, type) \ - smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _unsigned) + smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _ulong) #define smart_string_append_long_ex(dest, num, type) \ - smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _signed) - -#define smart_string_append_off_t_ex(dest, num, type) \ - smart_string_append_generic_ex((dest), (num), (type), zend_off_t, _signed) + smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _long) #define smart_string_append_ex(dest, src, what) \ smart_string_appendl_ex((dest), ((smart_string *)(src))->c, \ diff --git a/ext/standard/var.c b/ext/standard/var.c index 9d2de3031fd..532e53a110f 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -611,17 +611,17 @@ static inline int php_add_var_hash(HashTable *var_hash, zval *var_ptr, zval *var var = Z_REFVAL_P(var); } if ((Z_TYPE_P(var) == IS_OBJECT) && Z_OBJ_HT_P(var)->get_class_entry) { - p = smart_str_print_long(id + sizeof(id) - 1, + p = zend_print_long_to_buf(id + sizeof(id) - 1, (zend_long) Z_OBJ_P(var)); *(--p) = 'O'; len = id + sizeof(id) - 1 - p; } else if (var_ptr != var) { - p = smart_str_print_long(id + sizeof(id) - 1, + p = zend_print_long_to_buf(id + sizeof(id) - 1, (zend_long) Z_REF_P(var_ptr)); *(--p) = 'R'; len = id + sizeof(id) - 1 - p; } else { - p = smart_str_print_long(id + sizeof(id) - 1, (zend_long) var); + p = zend_print_long_to_buf(id + sizeof(id) - 1, (zend_long) var); len = id + sizeof(id) - 1 - p; } diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 2d2e399ec87..b2defe0841e 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -380,11 +380,11 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int } smart_str_appendl_ex(buffer, "HTTP", 4, persistent); smart_str_appendc_ex(buffer, '/', persistent); - smart_str_append_generic_ex(buffer, protocol_version / 100, persistent, int, _unsigned); + smart_str_append_long_ex(buffer, protocol_version / 100, persistent); smart_str_appendc_ex(buffer, '.', persistent); - smart_str_append_generic_ex(buffer, protocol_version % 100, persistent, int, _unsigned); + smart_str_append_long_ex(buffer, protocol_version % 100, persistent); smart_str_appendc_ex(buffer, ' ', persistent); - smart_str_append_generic_ex(buffer, response_code, persistent, int, _unsigned); + smart_str_append_long_ex(buffer, response_code, persistent); smart_str_appendc_ex(buffer, ' ', persistent); smart_str_appends_ex(buffer, get_status_string(response_code), persistent); smart_str_appendl_ex(buffer, "\r\n", 2, persistent); @@ -1902,7 +1902,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server append_essential_headers(&buffer, client, 1); smart_str_appends_ex(&buffer, "Content-Type: text/html; charset=UTF-8\r\n", 1); smart_str_appends_ex(&buffer, "Content-Length: ", 1); - smart_str_append_generic_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1, size_t, _unsigned); + smart_str_append_unsigned_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1); smart_str_appendl_ex(&buffer, "\r\n", 2, 1); smart_str_appendl_ex(&buffer, "\r\n", 2, 1); @@ -1993,7 +1993,7 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv } smart_str_appendl_ex(&buffer, "\r\n", 2, 1); smart_str_appends_ex(&buffer, "Content-Length: ", 1); - smart_str_append_generic_ex(&buffer, client->request.sb.st_size, 1, size_t, _unsigned); + smart_str_append_unsigned_ex(&buffer, client->request.sb.st_size, 1); smart_str_appendl_ex(&buffer, "\r\n", 2, 1); smart_str_appendl_ex(&buffer, "\r\n", 2, 1); chunk = php_cli_server_chunk_heap_new(buffer.s, buffer.s->val, buffer.s->len); diff --git a/sapi/thttpd/thttpd.c b/sapi/thttpd/thttpd.c index 529ac2f48b6..1671a5b65b7 100644 --- a/sapi/thttpd/thttpd.c +++ b/sapi/thttpd/thttpd.c @@ -181,7 +181,7 @@ static int sapi_thttpd_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) if (!SG(sapi_headers).http_status_line) { ADD_VEC_S("HTTP/1.1 "); - p = smart_str_print_long(buf+sizeof(buf)-1, + p = zend_print_long_to_buf(buf+sizeof(buf)-1, SG(sapi_headers).http_response_code); ADD_VEC(p, strlen(p)); ADD_VEC_S(" HTTP\r\n"); @@ -293,7 +293,7 @@ static void sapi_thttpd_register_variables(zval *track_vars_array TSRMLS_DC) ADD_STRING_EX("REMOTE_HOST", p); ADD_STRING_EX("SERVER_PORT", - smart_str_print_long(buf + sizeof(buf) - 1, + zend_print_long_to_buf(buf + sizeof(buf) - 1, TG(hc)->hs->port)); buf[0] = '/'; @@ -323,7 +323,7 @@ static void sapi_thttpd_register_variables(zval *track_vars_array TSRMLS_DC) if (TG(hc)->contentlength != -1) { ADD_STRING_EX("CONTENT_LENGTH", - smart_str_print_long(buf + sizeof(buf) - 1, + zend_print_long_to_buf(buf + sizeof(buf) - 1, TG(hc)->contentlength)); }