[libc-commits] [libc] [libc] Support ls in printf (PR #178841)

Shubh Pachchigar via libc-commits libc-commits at lists.llvm.org
Mon Feb 2 23:20:40 PST 2026


================
@@ -32,13 +40,58 @@ LIBC_INLINE int convert_string(Writer<write_mode> *writer,
   }
 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
 
-  for (const char *cur_str = (str_ptr); cur_str[string_len]; ++string_len) {
-    ;
-  }
+  if (to_conv.length_modifier == LengthModifier::l) {
+#ifndef LIBC_COPT_PRINTF_DISABLE_WIDE
+    const wchar_t *wstr_ptr =
+        reinterpret_cast<const wchar_t *>(to_conv.conv_val_ptr);
+
+#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
+    if (wstr_ptr == nullptr) {
+      wstr_ptr = L"(null)";
+    }
+#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
+
+    char buffer[MB_LEN_MAX];
+    internal::mbstate mbstate;
+    size_t written = 0;
+    for (const wchar_t *cur_str = (wstr_ptr); cur_str[string_len];
+         ++string_len) {
+      wchar_t wc = cur_str[string_len];
+
+      auto ret = internal::wcrtomb(buffer, wc, &mbstate);
+      if (!ret.has_value()) {
+        return MB_CONVERSION_ERROR;
+      }
+      written += ret.value();
+
+      if (to_conv.precision >= 0 &&
+          static_cast<size_t>(to_conv.precision) < written) {
+        written -= ret.value();
+        break;
+      }
+    }
+    string_len = written;
+#else
+    for (const char *cur_str = (str_ptr); cur_str[string_len]; ++string_len) {
----------------
shubhe25p wrote:

yes it is, sure I will use internal implementation instead.

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


More information about the libc-commits mailing list