Skip to content

Commit

Permalink
Handle (non-conforming) allergy to setlocale in %'
Browse files Browse the repository at this point in the history
As was mentioned on GitHub (PR 1285), "I have a disability that makes me
allergic to calling setlocale() and unable to read large numbers without
grouping". Thus, this patch alters the printf family's <apostrophe> flag
to always group characters together with an apostrophe, even though this
is clearly non-conforming to POSIX when using the default C/POSIX locale
(which does not have a thousands' grouping character, meaning none ought
to be printed even when the ' flag is present, behavior which would thus
normally require change of locale (e.g. using setlocale) to be changed).
  • Loading branch information
GabrielRavier committed Sep 10, 2024
1 parent 38c5348 commit 9e0ead5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions libc/stdio/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ static int __fmt_ntoa2(int out(const char *, void *, size_t), void *arg,
digit &= (1u << log2base) - 1;
value >>= log2base;
}
if ((flags & FLAGS_GROUPING) && count == 3) {
buf[len++] = ',';
if ((flags & (FLAGS_GROUPING | FLAGS_QUOTE)) && count == 3) {
buf[len++] = (flags & FLAGS_QUOTE) ? '\'' : ',';
count = 1;
} else {
count++;
Expand All @@ -289,7 +289,7 @@ static int __fmt_ntoa2(int out(const char *, void *, size_t), void *arg,
static int __fmt_ntoa(int out(const char *, void *, size_t), void *arg,
uint128_t value, unsigned char signbit,
unsigned long log2base, unsigned long prec,
unsigned long width, unsigned char flags,
unsigned long width, unsigned flags,
const char *lang) {
bool neg;
uint128_t sign;
Expand Down
4 changes: 2 additions & 2 deletions test/libc/stdio/snprintf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,6 @@ TEST(snprintf, testLongDoubleRounding) {
TEST(snprintf, apostropheFlag) {
char buf[20];
int i = snprintf(buf, sizeof(buf), "%'d", 1000000);
ASSERT_EQ(7, i);
ASSERT_STREQ("1000000", buf);
ASSERT_EQ(9, i);
ASSERT_STREQ("1'000'000", buf);
}

0 comments on commit 9e0ead5

Please sign in to comment.