[libc-commits] [libc] [libc] Support %lc in printf (PR #169983)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Jan 6 10:02:35 PST 2026


================
@@ -20,20 +29,49 @@ namespace printf_core {
 template <WriteMode write_mode>
 LIBC_INLINE int convert_char(Writer<write_mode> *writer,
                              const FormatSection &to_conv) {
-  char c = static_cast<char>(to_conv.conv_val_raw);
 
-  constexpr int STRING_LEN = 1;
+  char buffer[MB_LEN_MAX];
+  size_t write_size = 0;
+
+  if (to_conv.length_modifier == LengthModifier::l) {
+#ifndef LIBC_COPT_PRINTF_DISABLE_WIDE
+    wint_t wi = static_cast<wint_t>(to_conv.conv_val_raw);
+
+    if (wi == WEOF) {
+      return ILLEGAL_WIDE_CHAR;
+    }
+
+    internal::mbstate mbstate;
+    wchar_t wc = static_cast<wchar_t>(wi);
+    auto ret = internal::wcrtomb(buffer, wc, &mbstate);
+
+    if (!ret.has_value()) {
+      return MB_CONVERSION_ERROR;
+    }
+
+    write_size = ret.value();
+#else
+    // If wide characters are disabled, treat the 'l' modifier as a no-op.
+    buffer[0] = static_cast<char>(to_conv.conv_val_raw);
+    write_size = 1;
+
+#endif // LIBC_COPT_PRINTF_DISABLE_WIDE
+  } else {
+    buffer[0] = static_cast<char>(to_conv.conv_val_raw);
+    write_size = 1;
+  }
 
+  int wlen = static_cast<int>(write_size);
----------------
michaelrj-google wrote:

nit: this variable feels unnecessary. You can probably just cast `write_size` in the `padding_spaces` condition.

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


More information about the libc-commits mailing list