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

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jan 23 11:27:01 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:

Here's what I was imagining:
```C++
template <typename T> LIBC_INLINE size_t string_length(const T *src) {
  size_t length;
  for (length = 0; *src; ++src, ++length)
    ;
  return length;
}

template <char> LIBC_INLINE size_t string_length(const char *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);
#else
  size_t length;
  for (length = 0; *src; ++src, ++length)
    ;
  return length;
#endif
}
```

The reason is to avoid issues around passing incorrect types to `string_length_wide_read`, I think `if constexpr` is evaluated late enough in the process you might get warnings about it.

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


More information about the libc-commits mailing list