Skip to content

Commit cdba96d

Browse files
committed
perf(printf): check flags outside while loop
1 parent 735abbc commit cdba96d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

printf.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
166166
const size_t start_idx = idx;
167167

168168
// pad leading zeros
169-
while (!(flags & FLAGS_LEFT) && (len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
170-
buf[len++] = '0';
171-
}
172-
while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
173-
buf[len++] = '0';
169+
if (!(flags & FLAGS_LEFT)) {
170+
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
171+
buf[len++] = '0';
172+
}
173+
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
174+
buf[len++] = '0';
175+
}
174176
}
175177

176178
// handle hash
@@ -381,8 +383,10 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
381383
}
382384

383385
// pad leading zeros
384-
while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
385-
buf[len++] = '0';
386+
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
387+
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
388+
buf[len++] = '0';
389+
}
386390
}
387391

388392
// handle sign

0 commit comments

Comments
 (0)