Skip to content

Commit 980f1f9

Browse files
committed
format_number_with_delim: simplified
1 parent 2e8ca3e commit 980f1f9

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

src/gpujpeg_common.c

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,37 +2176,20 @@ format_number_with_delim(size_t num, char* buf, size_t buflen)
21762176
assert(buflen >= 1);
21772177
buf[buflen - 1] = '\0';
21782178
char* ptr = buf + buflen - 1;
2179-
while ( 1 ) {
2180-
const int tmp = num % 1000;
2181-
num /= 1000;
2182-
if ( num == 0 ) {
2183-
ptr -= 1;
2184-
if ( tmp >= 10 ) {
2185-
ptr -= 1;
2186-
if ( tmp >= 100 ) {
2187-
ptr -= 1;
2188-
}
2189-
}
2190-
if ( ptr < buf ) {
2191-
snprintf(buf, buflen, "%s", "ERR");
2192-
return buf;
2193-
}
2194-
char numbuf[4];
2195-
snprintf(numbuf, sizeof numbuf, "%i", tmp);
2196-
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): prepending, no null-termination
2197-
memcpy(ptr, numbuf, strlen(numbuf));
2198-
return ptr;
2199-
}
2200-
ptr -= 4;
2201-
if ( ptr < buf ) {
2179+
int grp_count = 0;
2180+
do {
2181+
if ( ptr == buf || (grp_count == 3 && ptr == buf + 1) ) {
22022182
snprintf(buf, buflen, "%s", "ERR");
22032183
return buf;
22042184
}
2205-
char numbuf[5];
2206-
snprintf(numbuf, sizeof numbuf, ",%03d", tmp);
2207-
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): prepending, no null-termination
2208-
memcpy(ptr, numbuf, 4);
2209-
}
2185+
if ( grp_count++ == 3 ) {
2186+
grp_count = 1;
2187+
*--ptr = ',';
2188+
}
2189+
*--ptr = (char)('0' + (num % 10));
2190+
num /= 10;
2191+
} while ( num != 0 );
2192+
22102193
return ptr;
22112194
}
22122195

0 commit comments

Comments
 (0)