[sprintf] "Wrong" output for "%'" (triplet_comma) with width

Open
#25 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
50/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c
Domain
backend

Research direction

The bug is in the %' (triplet comma) handling in stb_sprintf.h, around lines 1207-1236, where leading zeros are emitted and the STBSP__TRIPLET_COMMA flag places separators. Start by reading that block, then compile a small test calling printf("%'08i", 1) against the header. Done means the leading separator is no longer printed; the issue proposes "0000,001" but offers alternatives, so confirm the intended output with a maintainer first.

Written by the indexing model from the issue text.

Description

The ' flag is a non-standard extension introducing comma thousand-separators in integer/float values (and also in binary/octal/hex values, with a different "periodicity").
Since it's not standard, STB can behave in any way; however, the current behavior is "surprising": when the width is such that the very first character should be a separator, a separator is indeed printed, which is weird because it is not separating anything, but rather leading the number.
For instance:

printf("%'08i", 1);

produces

",000,001"

I think it would less surprising to just pad with a zero rather than a separator, in this case:

"0000,001"

similarly to what is done in other contexts, eg "1000,000" is sometimes used rather than "1,000,000".
The alternatives would be to use space-padding (" 000,001"), which is surprising as well, since the user wanted 0-padding, or to reduce the width by 1 ("000,001"), which is surprising as well, since the user did specify the width, and the minimum width is something that works consistently across all specifiers and flags.

Fix: substitute lines 1207-1236:
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1207-L1236
with the following (this introduces some logic with 'n'):

            // copy leading zeros
            c = cs >> 24;
            cs &= 0xffffff;
            cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;
            n = 1; // 1: we are at the beginning, so print '0' instead of comma
            while (pr > 0) {
               stbsp__cb_buf_clamp(i, pr);
               pr -= i;
               if ((fl & STBSP__TRIPLET_COMMA) == 0) {
                  while (i) {
                     if ((((stbsp__uintptr)bf) & 3) == 0)
                        break;
                     *bf++ = '0';
                     --i;
                  }
                  while (i >= 4) {
                     *(stbsp__uint32 *)bf = 0x30303030;
                     bf += 4;
                     i -= 4;
                  }
               }
               while (i) {
                  if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {
                     cs = 0;
                     *bf++ = n ? '0' : stbsp__comma;
                  } else
                     *bf++ = '0';
                  n = 0;
                  --i;
               }
               stbsp__chk_cb_buf(1);
            }
Dominant language
C
Stars
18
Forks
5
Avg merge
1m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nothings/stb_sprintf

All issues in nothings/stb_sprintf

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.