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

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jan 23 13:07:06 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) {
----------------
michaelrj-google wrote:

Looking at it written out, it's probably overcomplicating things to add the template specialization just to avoid the `if constexpr`. If it compiles without warnings, it's probably fine.

I don't think we really need to add the SFINAE to limit the template. In practice I expect it'll be used for `char`, `wchar_t`, and maybe `unsigned char`, but it will behave as expected for any other integer type. Adding the check seems like unnecessary complexity for little practical benefit.

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


More information about the libc-commits mailing list