[llvm] [llvm-strings] Add --encoding={s,S,utf8} option (PR #221794)

Harald van Dijk via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 4 13:42:05 PDT 2026


================
@@ -101,6 +111,127 @@ static void strings(raw_ostream &OS, StringRef FileName,
     }
   };
 
+  std::locale Loc("");
+  auto &Cvt = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(Loc);
+  auto &Ctype = std::use_facet<std::ctype<wchar_t>>(Loc);
+
+  auto IsStringChar = [&Ctype](UTF32 Ch) {
+    if (Ch == '\t')
+      return true;
+
+    switch (Encoding) {
+    case Encoding::Ascii:
+      return isPrint(Ch);
+
+    case Encoding::Locale:
+      return Ctype.is(std::ctype_base::print, Ch);
+
+    case Encoding::Utf8:
+      return sys::unicode::isPrintable(Ch);
+    }
+
+    llvm_unreachable("unhandled encoding");
+  };
+
+  // Tries to read one character from the bytes P...E and store it in Ch.
+  // Returns true if a (possibly invalid) character was read, false otherwise.
+  //
+  // If P...E starts with a valid complete character, P and MBState are updated
+  // and true is returned.
+  // If P...E is empty, or holds an incomplete character and AtEOF is false, P
+  // and MBState are unchanged, Ch is set to 0, and false is returned. This is
+  // intended to allow more bytes to be read and Read to be called again.
+  // If P...E holds an incomplete character and AtEOF is true, or if P...E
+  // starts with an invalid character, the first byte is skipped and MBState is
+  // reset to allow continuing from the next point, Ch is set to 0, and true is
+  // returned.
+  auto Read = [&Cvt](const char *&P, const char *E, std::mbstate_t &MBState,
+                     bool AtEOF, UTF32 &Ch) -> bool {
----------------
hvdijk wrote:

Previously, that would have been annoying by requiring explicit parameters rather than being able to capture `Cvt`, but as my work to avoid `std::locale` for performance reasons also removes the benefit of capturing anything, sure, that looks much easier now.

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


More information about the llvm-commits mailing list