Skip to content

Commit 01672af

Browse files
author
Sascha Schumann
committed
Improved fix
1 parent 902100a commit 01672af

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

ext/standard/php_smart_str.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,38 @@ static inline void smart_str_appendl_ex(smart_str *dest, const char *src, size_t
7878

7979
static inline char *smart_str_print_long(char *buf, long num)
8080
{
81-
/* TBFixed: think how to do it one-pass */
81+
char *p = buf, *end;
82+
int n;
8283
long tmp;
83-
char *p = buf;
84-
int n = 0;
85-
86-
if(num == 0) {
87-
*p++ = '0';
88-
return p;
89-
}
9084

9185
if (num < 0) {
9286
num = -num;
9387
*p++ = '-';
9488
}
9589

96-
for (tmp = num; tmp > 0; n++) {
97-
tmp /= 10;
90+
/* many numbers are < 10 */
91+
if (num < 10) {
92+
*p++ = num + '0';
93+
return p;
9894
}
99-
p += n;
10095

101-
while (num > 0) {
102-
*(--p) = (num % 10) + '0';
96+
n = 1;
97+
tmp = num;
98+
99+
/* calculate the number of digits we need */
100+
do {
101+
tmp /= 10;
102+
n++;
103+
} while (tmp >= 10);
104+
105+
end = p += n;
106+
107+
do {
108+
*--p = (num % 10) + '0';
103109
num /= 10;
104-
}
110+
} while (--n > 0);
105111

106-
return p+n;
112+
return end;
107113
}
108114

109115
static inline void smart_str_append_long_ex(smart_str *dest, long num, int type)

0 commit comments

Comments
 (0)