[llvm] [llvm-strings] Add --encoding={s,S,utf8} option (PR #221794)
Tomohiro Kashiwada via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 07:47:04 PDT 2026
================
@@ -78,12 +90,165 @@ static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
}
}
-static bool isStringChar(char C) { return isPrint(C) || C == '\t'; }
+template <enum Encoding> struct Strings {
+ Strings() = delete;
+
+ // Tries to read one character from the bytes Cur...End and store it in Ch.
+ // Returns true if a (possibly invalid) character was read, false otherwise.
+ //
+ // If Cur...End starts with a valid complete character, Cur and MBState are
+ // updated and true is returned.
+ // If Cur...End is empty, or holds an incomplete character and AtEOF is false,
+ // Cur and MBState are unchanged, Ch is set to 0, and false is returned. This
+ // is intended to allow more bytes to be read and readChar to be called again.
+ // If Cur...End holds an incomplete character and AtEOF is true, or if
+ // Cur...End starts with an invalid character, the first bytes are skipped and
+ // MBState is reset to allow continuing from the next point, Ch is set to 0,
+ // and true is returned. This case is indistinguishable to the caller from a
+ // null byte being successfully converted to a null character, but a null
+ // character is never printable under any settings and that is how invalid
+ // bytes are treated as well.
+ //
+ // The number of skipped bytes for invalid characters follows the Unicode
+ // definition of the maximal subpart of an ill-formed subsequence, applied to
+ // arbitrary locales: it is the longest subsequence that could start a valid
+ // multibyte character, or if the initial byte cannot start a valid multibyte
+ // character, the initial byte. This allows consistent error recovery.
+ static bool readChar(const char *&Cur, const char *End,
+ std::mbstate_t &MBState, bool AtEOF, UTF32 &Ch);
+
+ static bool isStringChar(UTF32 Ch);
+
+ static void endString(raw_ostream &OS, std::mbstate_t &MBState);
+
+ static void run(raw_ostream &OS, StringRef FileName, sys::fs::file_t Handle);
+};
+
+template <>
+bool Strings<Encoding::Ascii>::readChar(const char *&Cur, const char *End,
+ std::mbstate_t &MBState, bool AtEOF,
+ UTF32 &Ch) {
+ if (Cur == End)
+ return false;
+
+ Ch = *Cur++;
+ return true;
+}
+
+template <>
+bool Strings<Encoding::Locale>::readChar(const char *&Cur, const char *End,
+ std::mbstate_t &MBState, bool AtEOF,
+ UTF32 &Ch) {
+ if (Cur == End)
+ return false;
+
+ const char *Next = Cur;
+ std::mbstate_t NextMBState = MBState;
+ wchar_t WCh;
+ for (;;) {
+ // Read one byte at a time. This is usually not the best way to use
+ // mbrtowc(), usually it would make more sense to pass the size of the
+ // buffer, but the strings utility is unusual in that it is expected to
+ // encounter many bytes that do not form valid characters and it is more
+ // useful to optimise for this case.
+ const std::size_t BytesRead = std::mbrtowc(&WCh, Next, 1, &NextMBState);
----------------
kikairoya wrote:
wchar_t is 16bit (UTF-16LE) on Windows, so it can't handle codepoints over U+FFFF. I think that's acceptable but worth to mention in somewhere.
https://godbolt.org/z/GafEnK6ah
https://github.com/llvm/llvm-project/pull/221794
More information about the llvm-commits
mailing list