Avoid superflous allocations in convert_to_string

Taken from zval_get_string.
This commit is contained in:
Nikita Popov 2014-05-01 08:55:59 +02:00
parent e0247de147
commit d820ea9f5e

View File

@ -596,9 +596,6 @@ ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC) /* {{{ */
ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */ ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */
{ {
long lval;
double dval;
switch (Z_TYPE_P(op)) { switch (Z_TYPE_P(op)) {
case IS_NULL: case IS_NULL:
case IS_FALSE: { case IS_FALSE: {
@ -612,32 +609,23 @@ ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */
case IS_STRING: case IS_STRING:
break; break;
case IS_RESOURCE: { case IS_RESOURCE: {
long tmp = Z_RES_HANDLE_P(op); char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
char *str; int len = snprintf(buf, sizeof(buf), "Resource id #%ld", Z_RES_HANDLE_P(op));
int len; ZVAL_NEW_STR(op, STR_INIT(buf, len, 0));
zval_ptr_dtor(op);
len = zend_spprintf(&str, 0, "Resource id #%ld", tmp);
ZVAL_NEW_STR(op, STR_INIT(str, len, 0));
efree(str);
break; break;
} }
case IS_LONG: { case IS_LONG: {
char *str; char buf[MAX_LENGTH_OF_LONG + 1];
int len; int len = snprintf(buf, sizeof(buf), "%ld", Z_LVAL_P(op));
lval = Z_LVAL_P(op); ZVAL_NEW_STR(op, STR_INIT(buf, len, 0));
len = zend_spprintf(&str, 0, "%ld", lval);
ZVAL_NEW_STR(op, STR_INIT(str, len, 0));
efree(str);
break; break;
} }
case IS_DOUBLE: { case IS_DOUBLE: {
char *str; char *str;
int len; int len;
double dval = Z_DVAL_P(op);
TSRMLS_FETCH(); TSRMLS_FETCH();
dval = Z_DVAL_P(op);
len = zend_spprintf(&str, 0, "%.*G", (int) EG(precision), dval); len = zend_spprintf(&str, 0, "%.*G", (int) EG(precision), dval);
/* %G already handles removing trailing zeros from the fractional part, yay */ /* %G already handles removing trailing zeros from the fractional part, yay */
ZVAL_NEW_STR(op, STR_INIT(str, len, 0)); ZVAL_NEW_STR(op, STR_INIT(str, len, 0));