1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00

Initial patch for 0 mode float conversion. The magic number is better to be improved. Any suggestion where to define it?

This commit is contained in:
Yasuo Ohgaki
2015-08-05 08:12:10 +09:00
committed by Jakub Zelenka
parent 8de8636a2b
commit f943daf2d7
5 changed files with 61 additions and 17 deletions

View File

@@ -104,7 +104,11 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
{
size_t len;
char num[PHP_JSON_DOUBLE_MAX_LENGTH];
php_gcvt(d, (int)EG(precision), '.', 'e', &num[0]);
if (PG(serialize_precision) == -1) {
php_0cvt(d, 17, '.', 'e', num);
} else {
php_gcvt(d, PG(serialize_precision), '.', 'e', num);
}
len = strlen(num);
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_JSON_DOUBLE_MAX_LENGTH - 2) {
num[len++] = '.';

View File

@@ -436,8 +436,7 @@ static void php_object_element_export(zval *zv, zend_ulong index, zend_string *k
PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
{
HashTable *myht;
char *tmp_str;
size_t tmp_len;
char tmp_str[2048]; /* Use the same magic number of spprintf.c NUM_BUF_SIZE */
zend_string *ztmp, *ztmp2;
zend_ulong index;
zend_string *key;
@@ -458,18 +457,26 @@ again:
smart_str_append_long(buf, Z_LVAL_P(struc));
break;
case IS_DOUBLE:
/* TODO: check INF, -INF and NAN in the new logic
tmp_len = spprintf(&tmp_str, 0,"%.*H", PG(serialize_precision), Z_DVAL_P(struc));
smart_str_appendl(buf, tmp_str, tmp_len);
/* Without a decimal point, PHP treats a number literal as an int.
* Without a decimal point, PHP treats a number literal as an int.
* This check even works for scientific notation, because the
* mantissa always contains a decimal point.
* We need to check for finiteness, because INF, -INF and NAN
* must not have a decimal point added.
*/
*
if (zend_finite(Z_DVAL_P(struc)) && NULL == strchr(tmp_str, '.')) {
smart_str_appendl(buf, ".0", 2);
}
efree(tmp_str);
*/
if (PG(serialize_precision < 0)) {
php_0cvt(Z_DVAL_P(struc), 17, '.', 'E', tmp_str);
} else {
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
}
smart_str_appends(buf, tmp_str);
break;
case IS_STRING:
ztmp = php_addcslashes(Z_STR_P(struc), 0, "'\\", 2);
@@ -844,16 +851,17 @@ again:
return;
case IS_DOUBLE: {
char *s;
smart_str_appendl(buf, "d:", 2);
s = (char *) safe_emalloc(PG(serialize_precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', s);
smart_str_appends(buf, s);
smart_str_appendc(buf, ';');
efree(s);
return;
char tmp_str[2048]; /* Use the same magic number of spprintf.c NUM_BUF_SIZE */
smart_str_appendl(buf, "d:", 2);
if (PG(serialize_precision < 0)) {
php_0cvt(Z_DVAL_P(struc), 17, '.', 'E', tmp_str);
} else {
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
}
smart_str_appends(buf, tmp_str);
smart_str_appendc(buf, ';');
return;
}
case IS_STRING:
php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));

View File

@@ -140,6 +140,23 @@ static PHP_INI_MH(OnSetPrecision)
}
/* }}} */
/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnSetSerializePrecision)
{
zend_long i;
ZEND_ATOL(i, ZSTR_VAL(new_value));
if (i >= -1) {
PG(serialize_precision) = i;
return SUCCESS;
} else {
return FAILURE;
}
}
/* }}} */
/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnChangeMemoryLimit)
@@ -533,7 +550,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("track_errors", "0", PHP_INI_ALL, OnUpdateBool, track_errors, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("unserialize_callback_func", NULL, PHP_INI_ALL, OnUpdateString, unserialize_callback_func, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("serialize_precision", "17", PHP_INI_ALL, OnUpdateLongGEZero, serialize_precision, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("serialize_precision", "-1", PHP_INI_ALL, OnSetSerializePrecision, serialize_precision, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("arg_separator.output", "&", PHP_INI_ALL, OnUpdateStringUnempty, arg_separator.output, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("arg_separator.input", "&", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateStringUnempty, arg_separator.input, php_core_globals, core_globals)

View File

@@ -139,12 +139,12 @@ static inline char *php_fcvt(double value, int ndigit, int *decpt, int *sign) /*
}
/* }}} */
PHPAPI char *php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf) /* {{{ */
static inline char *_php_cvt(double value, int ndigit, char dec_point, char exponent, char *buf, int mode) /* {{{ */
{
char *digits, *dst, *src;
int i, decpt, sign;
digits = zend_dtoa(value, 2, ndigit, &decpt, &sign, NULL);
digits = zend_dtoa(value, mode, ndigit, &decpt, &sign, NULL);
if (decpt == 9999) {
/*
* Infinity or NaN, convert to inf or nan with sign.
@@ -234,6 +234,20 @@ PHPAPI char *php_gcvt(double value, int ndigit, char dec_point, char exponent, c
}
/* }}} */
PHPAPI char *php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf) /* {{{ */
{
return _php_cvt(value, ndigit, dec_point, exponent, buf, 2);
}
/* }}} */
PHPAPI char *php_0cvt(double value, int ndigit, char dec_point, char exponent, char *buf) /* {{{ */
{
return _php_cvt(value, ndigit, dec_point, exponent, buf, 0);
}
/* }}} */
/* {{{ Apache license */
/* ====================================================================
* Copyright (c) 1995-1998 The Apache Group. All rights reserved.

View File

@@ -86,6 +86,7 @@ PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
PHPAPI char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
PHPAPI char * php_0cvt(double value, int ndigit, char dec_point, char exponent, char *buf);
PHPAPI char * php_conv_fp(char format, double num,
boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, size_t *len);