|
14 | 14 | #ifndef LLVM_LIBC_SRC_STRING_STRING_UTILS_H |
15 | 15 | #define LLVM_LIBC_SRC_STRING_STRING_UTILS_H |
16 | 16 |
|
| 17 | +#include "hdr/types/size_t.h" |
17 | 18 | #include "src/__support/CPP/bitset.h" |
| 19 | +#include "src/__support/CPP/type_traits.h" // cpp::is_same_v |
18 | 20 | #include "src/__support/macros/config.h" |
19 | 21 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
20 | 22 | #include "src/string/memory_utils/inline_bzero.h" |
21 | 23 | #include "src/string/memory_utils/inline_memcpy.h" |
22 | | -#include <stddef.h> // For size_t |
23 | 24 |
|
24 | 25 | namespace LIBC_NAMESPACE_DECL { |
25 | 26 | namespace internal { |
@@ -79,24 +80,21 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) { |
79 | 80 | return char_ptr - src; |
80 | 81 | } |
81 | 82 |
|
82 | | -LIBC_INLINE size_t string_length_byte_read(const char *src) { |
83 | | - size_t length; |
84 | | - for (length = 0; *src; ++src, ++length) |
85 | | - ; |
86 | | - return length; |
87 | | -} |
88 | | - |
89 | 83 | // Returns the length of a string, denoted by the first occurrence |
90 | 84 | // of a null terminator. |
91 | | -LIBC_INLINE size_t string_length(const char *src) { |
| 85 | +template <typename T> LIBC_INLINE size_t string_length(const T *src) { |
92 | 86 | #ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ |
93 | 87 | // Unsigned int is the default size for most processors, and on x86-64 it |
94 | 88 | // performs better than larger sizes when the src pointer can't be assumed to |
95 | 89 | // be aligned to a word boundary, so it's the size we use for reading the |
96 | 90 | // string a block at a time. |
97 | | - return string_length_wide_read<unsigned int>(src); |
| 91 | + if constexpr (cpp::is_same_v<T, char>) |
| 92 | + return string_length_wide_read<unsigned int>(src); |
98 | 93 | #else |
99 | | - return string_length_byte_read(src); |
| 94 | + size_t length; |
| 95 | + for (length = 0; *src; ++src, ++length) |
| 96 | + ; |
| 97 | + return length; |
100 | 98 | #endif |
101 | 99 | } |
102 | 100 |
|
|
0 commit comments