From ec135a7f19769bd6f27f2d99c66f3e21bdb676a3 Mon Sep 17 00:00:00 2001 From: imzet Date: Sun, 13 May 2012 02:29:53 +0800 Subject: [PATCH] fix bug #60977 --- ext/standard/math.c | 18 ++++++++-------- ext/standard/php_math.h | 2 +- .../tests/math/number_format_basic.phpt | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/ext/standard/math.c b/ext/standard/math.c index 65187f6fa10f3..6ecd95b96fca4 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -1094,10 +1094,10 @@ PHP_FUNCTION(base_convert) */ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep) { - return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1); + return _php_math_number_format_ex(d, dec, &dec_point, &thousand_sep); } -PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len) +PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, char *thousand_sep) { char *tmpbuf = NULL, *resbuf; char *s, *t; /* source, target */ @@ -1106,6 +1106,8 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size int tmplen, reslen=0; int count=0; int is_negative=0; + size_t dec_point_len = strlen(dec_point); + size_t thousand_sep_len = strlen(thousand_sep); if (d < 0) { is_negative = 1; @@ -1137,7 +1139,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size } /* allow for thousand separators */ - if (thousand_sep) { + if (thousand_sep_len) { integral += thousand_sep_len * ((integral-1) / 3); } @@ -1146,7 +1148,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size if (dec) { reslen += dec; - if (dec_point) { + if (dec_point_len) { reslen += dec_point_len; } } @@ -1182,7 +1184,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size } /* add decimal point */ - if (dec_point) { + if (dec_point_len) { t -= dec_point_len; memcpy(t + 1, dec_point, dec_point_len); } @@ -1192,7 +1194,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size * separator every three digits */ while(s >= tmpbuf) { *t-- = *s--; - if (thousand_sep && (++count%3)==0 && s>=tmpbuf) { + if (thousand_sep_len && (++count%3)==0 && s>=tmpbuf) { t -= thousand_sep_len; memcpy(t + 1, thousand_sep, thousand_sep_len); } @@ -1233,15 +1235,13 @@ PHP_FUNCTION(number_format) case 4: if (dec_point == NULL) { dec_point = &dec_point_chr; - dec_point_len = 1; } if (thousand_sep == NULL) { thousand_sep = &thousand_sep_chr; - thousand_sep_len = 1; } - RETURN_STRING(_php_math_number_format_ex(num, dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len), 0); + RETURN_STRING(_php_math_number_format_ex(num, dec, dec_point, thousand_sep), 0); break; default: WRONG_PARAM_COUNT; diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 3da3cf00891bf..3016ebf5a7612 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -23,7 +23,7 @@ #define PHP_MATH_H PHPAPI char *_php_math_number_format(double, int, char, char); -PHPAPI char *_php_math_number_format_ex(double, int, char *, size_t, char *, size_t); +PHPAPI char *_php_math_number_format_ex(double, int, char *, char *); PHPAPI char * _php_math_longtobase(zval *arg, int base); PHPAPI long _php_math_basetolong(zval *arg, int base); PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret); diff --git a/ext/standard/tests/math/number_format_basic.phpt b/ext/standard/tests/math/number_format_basic.phpt index 7f5f4beb2568e..e070224be640a 100644 --- a/ext/standard/tests/math/number_format_basic.phpt +++ b/ext/standard/tests/math/number_format_basic.phpt @@ -38,6 +38,13 @@ for ($i = 0; $i < count($values); $i++) { $res = number_format($values[$i], 2, ',' , ' '); var_dump($res); } + +echo "\n number_format tests.....Null thousand separater\n"; +for ($i = 0; $i < count($values); $i++) { + $res = number_format($values[$i], 2, '.', "\0"); + var_dump($res); +} + ?> --EXPECTF-- number_format tests.....default @@ -95,3 +102,17 @@ string(6) "123,46" string(4) "0,00" string(4) "1,00" string(4) "0,00" + + number_format tests.....Null thousand separater +string(7) "1234.57" +string(8) "-1234.57" +string(11) "12345678.00" +string(12) "-12345678.90" +string(12) "305450479.00" +string(12) "402653183.00" +string(12) "123456789.00" +string(6) "123.46" +string(6) "123.46" +string(4) "0.00" +string(4) "1.00" +string(4) "0.00"