[libc-commits] [libc] [libc] Accept the ' flag in printf (PR #239981)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Sat Nov 28 03:02:00 PST 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/239981
>From 80d46430483c9cd22396d171606757a76c6ec3ba Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sun, 11 Oct 2026 14:12:18 +0100
Subject: [PATCH 1/2] [libc] Accept the ' flag in printf
glibc takes it to group digits by the locale. The C locale has no
grouping, so the flag is read and changes nothing, rather than the
conversion being written out as text. glib's size formatting uses it.
Assisted-by: Claude
---
libc/src/__support/printf_core/core_structs.h | 4 ++--
libc/src/__support/printf_core/parser.h | 3 +++
libc/test/src/stdio/sprintf_test.cpp | 6 ++++++
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/printf_core/core_structs.h b/libc/src/__support/printf_core/core_structs.h
index a25b9e29ab8421..050b9f689a7661 100644
--- a/libc/src/__support/printf_core/core_structs.h
+++ b/libc/src/__support/printf_core/core_structs.h
@@ -61,9 +61,9 @@ enum FormatFlags : uint8_t {
SPACE_PREFIX = 0x04, // space
ALTERNATE_FORM = 0x08, // #
LEADING_ZEROES = 0x10, // 0
+ GROUP_DECIMALS = 0x20, // '
- // These flags come from the GNU extensions which aren't yet implemented.
- // group_decimals = 0x20, // '
+ // This flag comes from the GNU extensions which aren't yet implemented.
// locale_digits = 0x40, // I
};
diff --git a/libc/src/__support/printf_core/parser.h b/libc/src/__support/printf_core/parser.h
index e0129a98fb891a..2084805beb8525 100644
--- a/libc/src/__support/printf_core/parser.h
+++ b/libc/src/__support/printf_core/parser.h
@@ -361,6 +361,9 @@ template <typename ArgProvider, typename CharT> class Parser {
case CharT{'0'}:
flags = static_cast<FormatFlags>(flags | FormatFlags::LEADING_ZEROES);
break;
+ case CharT{'\''}:
+ flags = static_cast<FormatFlags>(flags | FormatFlags::GROUP_DECIMALS);
+ break;
default:
found_flag = false;
}
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index f5b68b4d66858f..87574647ba385c 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -309,6 +309,11 @@ TEST(LlvmLibcSPrintfTest, IntConv) {
written = LIBC_NAMESPACE::sprintf(buff, "%05d", -581);
ASSERT_STREQ_LEN(written, buff, "-0581");
+ // TODO: The ' flag is only accepted, and the C locale has no grouping, so it
+ // changes nothing. Update this if it is implemented for other locales.
+ written = LIBC_NAMESPACE::sprintf(buff, "%'d", 1234567);
+ ASSERT_STREQ_LEN(written, buff, "1234567");
+
// Combined Tests.
written = LIBC_NAMESPACE::sprintf(buff, "%+ u", 692);
@@ -4002,4 +4007,5 @@ TEST(LlvmLibcSprintfTest, WideCharStringConversion) {
EXPECT_EQ(written, 6);
ASSERT_STREQ_LEN(written, buff, " (nu");
}
+
#endif // !LIBC_COPT_PRINTF_DISABLE_WIDE && !LIBC_TARGET_ARCH_IS_GPU
>From c6603c0432a5743993ea3513b73e405efe1de864 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 28 Nov 2026 04:43:21 +0000
Subject: [PATCH 2/2] [libc] Remove an unrelated blank line from sprintf_test
Assisted-by: Claude
---
libc/test/src/stdio/sprintf_test.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index 87574647ba385c..467a091e25d6ca 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -4007,5 +4007,4 @@ TEST(LlvmLibcSprintfTest, WideCharStringConversion) {
EXPECT_EQ(written, 6);
ASSERT_STREQ_LEN(written, buff, " (nu");
}
-
#endif // !LIBC_COPT_PRINTF_DISABLE_WIDE && !LIBC_TARGET_ARCH_IS_GPU
More information about the libc-commits
mailing list