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.
This commit is contained in:
Nikita Popov 2014-09-19 23:22:26 +02:00
parent ad3e1830ba
commit 31e842472f
7 changed files with 45 additions and 83 deletions

View File

@ -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);
}
/* }}} */

View File

@ -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);

View File

@ -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

View File

@ -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, \

View File

@ -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;
}

View File

@ -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);

View File

@ -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));
}