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

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


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Mike Lothian (FireBurn)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/239981.diff


3 Files Affected:

- (modified) libc/src/__support/printf_core/core_structs.h (+2-2) 
- (modified) libc/src/__support/printf_core/parser.h (+3) 
- (modified) libc/test/src/stdio/sprintf_test.cpp (+9) 


``````````diff
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");
+}

``````````

</details>


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


More information about the libc-commits mailing list