[libc-commits] [libc] [libc] Accept the ' flag in printf (PR #239981)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 06:45:15 PST 2026


https://github.com/FireBurn created https://github.com/llvm/llvm-project/pull/239981

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

>From 0073db2fa300a4a06175d5c7f22bf1493a21fcd7 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] [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          | 9 +++++++++
 3 files changed, 14 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..9d0afa5b2f1120 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -4002,4 +4002,13 @@ TEST(LlvmLibcSprintfTest, WideCharStringConversion) {
   EXPECT_EQ(written, 6);
   ASSERT_STREQ_LEN(written, buff, "   (nu");
 }
+
 #endif // !LIBC_COPT_PRINTF_DISABLE_WIDE && !LIBC_TARGET_ARCH_IS_GPU
+
+TEST(LlvmLibcSprintfTest, GroupingFlagIsAccepted) {
+  char buff[64];
+  // The C locale has no grouping, so the flag changes nothing there.
+  int written = LIBC_NAMESPACE::sprintf(buff, "%'d|%'lu", 1234567, 238472938ul);
+  EXPECT_EQ(written, 17);
+  ASSERT_STREQ_LEN(written, buff, "1234567|238472938");
+}



More information about the libc-commits mailing list