Skip to content

Commit

Permalink
format_number_with_delim: simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinPulec committed Jan 21, 2025
1 parent 2e8ca3e commit 861b9d2
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions src/gpujpeg_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2176,37 +2176,20 @@ format_number_with_delim(size_t num, char* buf, size_t buflen)
assert(buflen >= 1);
buf[buflen - 1] = '\0';
char* ptr = buf + buflen - 1;
while ( 1 ) {
const int tmp = num % 1000;
num /= 1000;
if ( num == 0 ) {
ptr -= 1;
if ( tmp >= 10 ) {
ptr -= 1;
if ( tmp >= 100 ) {
ptr -= 1;
}
}
if ( ptr < buf ) {
snprintf(buf, buflen, "%s", "ERR");
return buf;
}
char numbuf[4];
snprintf(numbuf, sizeof numbuf, "%i", tmp);
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): prepending, no null-termination
memcpy(ptr, numbuf, strlen(numbuf));
return ptr;
}
ptr -= 4;
if ( ptr < buf ) {
int grp_count = 0;
do {
if ( ptr == buf || (grp_count == 3 && ptr == buf + 1) ) {
snprintf(buf, buflen, "%s", "ERR");
return buf;
}
char numbuf[5];
snprintf(numbuf, sizeof numbuf, ",%03d", tmp);
// NOLINTNEXTLINE(bugprone-not-null-terminated-result): prepending, no null-termination
memcpy(ptr, numbuf, 4);
}
if ( grp_count++ == 3 ) {
grp_count = 1;
*--ptr = ',';
}
*--ptr = (char)('0' + (num % 10));
num /= 10;
} while (num != 0);

return ptr;
}

Expand Down

0 comments on commit 861b9d2

Please sign in to comment.