[libc-commits] [libc] [libc] Add UTF-16 support in CharacterConverter [1/6] (PR #230434)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 23:20:55 PST 2026


================
@@ -154,15 +253,53 @@ LIBC_INLINE ErrorOr<char32_t> CharacterConverter::pop_utf32() {
   // whether enough bytes have been pushed
   if (!isFull())
     return Error(-1);
-  char32_t utf32 = state->partial;
+  const char32_t utf32 = state->partial;
   // reset if successful pop
   clear();
   return utf32;
 }
 
+LIBC_INLINE ErrorOr<char16_t> CharacterConverter::pop_utf16() {
+  if (isEmpty())
+    return Error(-1);
+  LIBC_ASSERT(state->conversion_state != MBConversionState::PUSHING);
+
+  char32_t output;
+  if (state->total_bytes <= 3) {
----------------
yangbowen wrote:

`state->total_bytes` always refers to UTF-8 bytes. 3 byte UTF-8 has maximum code point `U+FFFF` and can fit in a UTF-16. 4 byte UTF-8 needs 2 UTF-16 code units.
I'll add a comment explaining this.

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


More information about the libc-commits mailing list