Skip to content

Commit cb77933

Browse files
committed
fixup
1 parent d8b1e71 commit cb77933

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

ext/standard/math.c

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,19 +1134,25 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de
11341134
PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const char *dec_point,
11351135
size_t dec_point_len, const char *thousand_sep, size_t thousand_sep_len)
11361136
{
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;
11391139
zend_string *res;
1140-
zend_string *res2;
11411140
zend_string *tmpbuf;
1142-
char *s, *t, *t2; /* source, target */
1141+
char *s, *t; /* source, target */
11431142
size_t integral;
11441143
size_t reslen = 0;
11451144
int count = 0;
11461145
size_t topad;
11471146
char cur_char;
11481147
int roundup = 0;
11491148

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+
11501156
tmpbuf = strpprintf(0, ZEND_ULONG_FMT, num_abs);
11511157

11521158
/* 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
11731179
integral = zend_safe_addmult((integral-1)/3, thousand_sep_len, integral, "number formatting");
11741180
}
11751181

1176-
reslen = integral + neg;
1182+
reslen = integral + is_negative;
11771183

11781184
if (dec > 0) {
11791185
reslen += dec;
@@ -1185,7 +1191,6 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
11851191

11861192
res = zend_string_alloc(reslen, 0);
11871193

1188-
s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1;
11891194
t = ZSTR_VAL(res) + reslen;
11901195
*t-- = '\0';
11911196

@@ -1208,8 +1213,8 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
12081213
/* copy the numbers before the decimal point,
12091214
* adding thousand separator every three digits,
12101215
* round negative decimal places if needed */
1216+
s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1;
12111217
topad = 0;
1212-
cur_char = *s;
12131218
while (s >= ZSTR_VAL(tmpbuf)) {
12141219
cur_char = *s--;
12151220
if (roundup && cur_char != '-') {
@@ -1238,36 +1243,38 @@ PHPAPI zend_string *_php_math_number_format_long(zend_long num, int dec, const c
12381243
}
12391244
}
12401245

1241-
if (neg) {
1246+
if (is_negative) {
12421247
*t-- = '-';
12431248
}
12441249

12451250
/* 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 */
12471252
if (roundup) {
1253+
/* roundup to next number and eventually add another thousand separator */
12481254
reslen += 1 + (thousand_sep && (count%3)==0 ? thousand_sep_len : 0);
1249-
res2 = zend_string_alloc(reslen, 0);
12501255

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';
12551265
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;
12581268
}
1259-
memcpy(t2, ZSTR_VAL(res)+1, reslen - 2);
1269+
memcpy(t, ZSTR_VAL(tmpbuf)+1, reslen - 1);
12601270
} else {
1261-
*t2++ = '1';
1271+
*t++ = '1';
12621272
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;
12651275
}
1266-
memcpy(t2, ZSTR_VAL(res), reslen - 1);
1276+
memcpy(t, ZSTR_VAL(tmpbuf), reslen);
12671277
}
1268-
1269-
zend_string_release_ex(res, 0);
1270-
res = res2;
12711278
}
12721279

12731280
ZSTR_LEN(res) = reslen;

0 commit comments

Comments
 (0)