Skip to content

Commit fe8ac14

Browse files
committed
Optimize and simplify __str_base10() by removing temporary buffer
Eliminate the temporary tmp[] buffer in __str_base10() by writing digits directly into the output buffer. This avoids an extra memory copy and improves efficiency while keeping the logic simple. The maximum digit count comment is also removed as it is no longer relevant.
1 parent 90b5dea commit fe8ac14

File tree

1 file changed

+1
-9
lines changed

1 file changed

+1
-9
lines changed

lib/libc.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,6 @@ static char *__str_base10(uint32_t value, char *buffer, int *length)
470470
*length = 1;
471471
return buffer;
472472
}
473-
474-
/* Max digits for 32-bit: 4,294,967,295 (10 digits) + sign + null */
475-
char tmp[12];
476473
int pos = 0;
477474

478475
while (value > 0) {
@@ -488,15 +485,10 @@ static char *__str_base10(uint32_t value, char *buffer, int *length)
488485
q += t;
489486
r -= (((t << 2) + t) << 1);
490487

491-
tmp[pos++] = '0' + r;
488+
buffer[pos++] = '0' + r;
492489
value = q;
493490
}
494-
495-
/* Reverse digits into output buffer */
496491
*length = pos;
497-
for (int i = 0; i < pos; i++) {
498-
buffer[i] = tmp[i];
499-
}
500492

501493
return buffer;
502494
}

0 commit comments

Comments
 (0)