@@ -1134,19 +1134,25 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de
1134
1134
PHPAPI zend_string * _php_math_number_format_long (zend_long num , int dec , const char * dec_point ,
1135
1135
size_t dec_point_len , const char * thousand_sep , size_t thousand_sep_len )
1136
1136
{
1137
- int neg = num < 0 ;
1138
- zend_ulong num_abs = neg ? (( zend_ulong ) - ( num + 1 )) + 1 : ( zend_ulong ) num ;
1137
+ int is_negative = 0 ;
1138
+ zend_ulong num_abs ;
1139
1139
zend_string * res ;
1140
- zend_string * res2 ;
1141
1140
zend_string * tmpbuf ;
1142
- char * s , * t , * t2 ; /* source, target */
1141
+ char * s , * t ; /* source, target */
1143
1142
size_t integral ;
1144
1143
size_t reslen = 0 ;
1145
1144
int count = 0 ;
1146
1145
size_t topad ;
1147
1146
char cur_char ;
1148
1147
int roundup = 0 ;
1149
1148
1149
+ if (num < 0 ) {
1150
+ is_negative = 1 ;
1151
+ num_abs = ((zend_ulong )- (num + 1 )) + 1 ;
1152
+ } else {
1153
+ num_abs = (zend_ulong )num ;
1154
+ }
1155
+
1150
1156
tmpbuf = strpprintf (0 , ZEND_ULONG_FMT , num_abs );
1151
1157
1152
1158
/* rounding more places than length of num
@@ -1173,7 +1179,7 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
1173
1179
integral = zend_safe_addmult ((integral - 1 )/3 , thousand_sep_len , integral , "number formatting" );
1174
1180
}
1175
1181
1176
- reslen = integral + neg ;
1182
+ reslen = integral + is_negative ;
1177
1183
1178
1184
if (dec > 0 ) {
1179
1185
reslen += dec ;
@@ -1185,7 +1191,6 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
1185
1191
1186
1192
res = zend_string_alloc (reslen , 0 );
1187
1193
1188
- s = ZSTR_VAL (tmpbuf ) + ZSTR_LEN (tmpbuf ) - 1 ;
1189
1194
t = ZSTR_VAL (res ) + reslen ;
1190
1195
* t -- = '\0' ;
1191
1196
@@ -1208,8 +1213,8 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
1208
1213
/* copy the numbers before the decimal point,
1209
1214
* adding thousand separator every three digits,
1210
1215
* round negative decimal places if needed */
1216
+ s = ZSTR_VAL (tmpbuf ) + ZSTR_LEN (tmpbuf ) - 1 ;
1211
1217
topad = 0 ;
1212
- cur_char = * s ;
1213
1218
while (s >= ZSTR_VAL (tmpbuf )) {
1214
1219
cur_char = * s -- ;
1215
1220
if (roundup && cur_char != '-' ) {
@@ -1238,36 +1243,38 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
1238
1243
}
1239
1244
}
1240
1245
1241
- if (neg ) {
1246
+ if (is_negative ) {
1242
1247
* t -- = '-' ;
1243
1248
}
1244
1249
1245
1250
/* Allocate more bytes in case we have to round up to more places than initially thought
1246
- * E.g. 999 with negative decimals between -1 and -3 needs to end up as 1.000 */
1251
+ * E.g. 999 with negative decimals between -1 and -3 ends up as 1.000 */
1247
1252
if (roundup ) {
1253
+ /* roundup to next number and eventually add another thousand separator */
1248
1254
reslen += 1 + (thousand_sep && (count %3 )== 0 ? thousand_sep_len : 0 );
1249
- res2 = zend_string_alloc (reslen , 0 );
1250
1255
1251
- t2 = ZSTR_VAL (res2 );
1252
- if (neg ) {
1253
- * t2 ++ = '-' ;
1254
- * t2 ++ = '1' ;
1256
+ /* previous result is the new temporary buffer */
1257
+ zend_string_release_ex (tmpbuf , 0 );
1258
+ tmpbuf = res ;
1259
+ res = zend_string_alloc (reslen , 0 );
1260
+
1261
+ t = ZSTR_VAL (res );
1262
+ if (is_negative ) {
1263
+ * t ++ = '-' ;
1264
+ * t ++ = '1' ;
1255
1265
if (thousand_sep && (count %3 ) == 0 ) {
1256
- memcpy (t2 , thousand_sep , thousand_sep_len );
1257
- t2 += thousand_sep_len ;
1266
+ memcpy (t , thousand_sep , thousand_sep_len );
1267
+ t += thousand_sep_len ;
1258
1268
}
1259
- memcpy (t2 , ZSTR_VAL (res )+ 1 , reslen - 2 );
1269
+ memcpy (t , ZSTR_VAL (tmpbuf )+ 1 , reslen - 1 );
1260
1270
} else {
1261
- * t2 ++ = '1' ;
1271
+ * t ++ = '1' ;
1262
1272
if (thousand_sep && (count %3 ) == 0 ) {
1263
- memcpy (t2 , thousand_sep , thousand_sep_len );
1264
- t2 += thousand_sep_len ;
1273
+ memcpy (t , thousand_sep , thousand_sep_len );
1274
+ t += thousand_sep_len ;
1265
1275
}
1266
- memcpy (t2 , ZSTR_VAL (res ), reslen - 1 );
1276
+ memcpy (t , ZSTR_VAL (tmpbuf ), reslen );
1267
1277
}
1268
-
1269
- zend_string_release_ex (res , 0 );
1270
- res = res2 ;
1271
1278
}
1272
1279
1273
1280
ZSTR_LEN (res ) = reslen ;
0 commit comments