diff options
| author | Nick Desaulniers <ndesaulniers@google.com> | 2025-01-23 13:33:04 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-23 13:33:04 -0800 |
| commit | 631a6e0004e57ca85569b99ea411418627925697 (patch) | |
| tree | 567d744bdaee75dace51cb01e822c54420f0ba5d /libc/src/string | |
| parent | 3ed28bbf195d5fe93c8711d09abc8bda7c73963e (diff) | |
[libc][wchar] implement wcslen (#124150)
Update string_utils' string_length to work with char* or wchar_t*, so that it
may be reusable when implementing wmemchr, wcspbrk, wcsrchr, wcsstr.
Link: #121183
Link: #124027
Co-authored-by: Nick Desaulniers <ndesaulniers@google.com>
---------
Co-authored-by: Tristan Ross <tristan.ross@midstall.com>
Diffstat (limited to 'libc/src/string')
| -rw-r--r-- | libc/src/string/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | libc/src/string/string_utils.h | 20 |
2 files changed, 12 insertions, 12 deletions
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt index e3faa543e630..2c607bf8ea89 100644 --- a/libc/src/string/CMakeLists.txt +++ b/libc/src/string/CMakeLists.txt @@ -17,9 +17,11 @@ add_header_library( DEPENDS .memory_utils.inline_bzero .memory_utils.inline_memcpy + libc.hdr.types.size_t libc.include.stdlib - libc.src.__support.common libc.src.__support.CPP.bitset + libc.src.__support.CPP.type_traits + libc.src.__support.common ${string_config_options} ) diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h index fc617bd18e8f..583d35014d39 100644 --- a/libc/src/string/string_utils.h +++ b/libc/src/string/string_utils.h @@ -14,12 +14,13 @@ #ifndef LLVM_LIBC_SRC_STRING_STRING_UTILS_H #define LLVM_LIBC_SRC_STRING_STRING_UTILS_H +#include "hdr/types/size_t.h" #include "src/__support/CPP/bitset.h" +#include "src/__support/CPP/type_traits.h" // cpp::is_same_v #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY #include "src/string/memory_utils/inline_bzero.h" #include "src/string/memory_utils/inline_memcpy.h" -#include <stddef.h> // For size_t namespace LIBC_NAMESPACE_DECL { namespace internal { @@ -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; #endif } |
