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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Jan 23 12:38:37 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) {
----------------
nickdesaulniers wrote:

The duplication between the two versions (for loop) isn't as DRY as I prefer; I don't think the above approach can be simplified to have the `char` specialized version of `string_length` call the non-specialized version.

---
> The reason is to avoid issues around passing incorrect types to string_length_wide_read

The parameter of `string_length_wide_read` isn't the template variable; the `if constexpr` statement as written compiles without warning. https://godbolt.org/z/ajYhWbddd

---
Now that I am pulling in cpp::type_traits, I could add some SFINAE to `string_length` so that it can only be called with `char*` and `wchar_t*`, if you'd like?

I don't feel strongly about any of the above, but please let me know what you prefer. I'll wait to land this until then.

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


More information about the libc-commits mailing list