[libc-commits] [libc] [libc][wchar] implement wcslen (PR #124150)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jan 23 09:52:38 PST 2025


================
@@ -79,24 +80,21 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) {
   return char_ptr - src;
 }
 
-LIBC_INLINE size_t string_length_byte_read(const char *src) {
-  size_t length;
-  for (length = 0; *src; ++src, ++length)
-    ;
-  return length;
-}
-
 // Returns the length of a string, denoted by the first occurrence
 // of a null terminator.
-LIBC_INLINE size_t string_length(const char *src) {
+template <typename T> LIBC_INLINE size_t string_length(const T *src) {
 #ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
   // Unsigned int is the default size for most processors, and on x86-64 it
   // performs better than larger sizes when the src pointer can't be assumed to
   // be aligned to a word boundary, so it's the size we use for reading the
   // string a block at a time.
-  return string_length_wide_read<unsigned int>(src);
+  if constexpr (cpp::is_same_v<T, char>)
+    return string_length_wide_read<unsigned int>(src);
 #else
-  return string_length_byte_read(src);
+  size_t length;
+  for (length = 0; *src; ++src, ++length)
+    ;
+  return length;
----------------
michaelrj-google wrote:

ah, reading back through the comments I see you already tried this. In the case would it make sense to have a generic template for `string_length` that just calls a templated `string_length_byte_read`, then a specialization for `char` with the wide read variant?

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


More information about the libc-commits mailing list