[libc-commits] [libc] [libc] Template StringConverter pop function to avoid duplicate code (PR #152204)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Aug 6 09:50:49 PDT 2025


================
@@ -31,14 +32,24 @@ class CharacterConverter {
   bool isEmpty();
   bool isValidState();
 
-  size_t sizeAsUTF32();
-  size_t sizeAsUTF8();
+  template <typename CharType> size_t sizeAs() {
+    if constexpr (cpp::is_same_v<CharType, char8_t>)
+      return state->total_bytes;
+    else        // char32_t
+      return 1; // every character fits in a single char32_t
+  }
 
   int push(char8_t utf8_byte);
   int push(char32_t utf32);
 
   ErrorOr<char8_t> pop_utf8();
   ErrorOr<char32_t> pop_utf32();
+  template <typename CharType> ErrorOr<CharType> pop() {
----------------
michaelrj-google wrote:

you can do the same here

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


More information about the libc-commits mailing list