summaryrefslogtreecommitdiff
path: root/libc/src/string/string_utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src/string/string_utils.h')
-rw-r--r--libc/src/string/string_utils.h20
1 files changed, 9 insertions, 11 deletions
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
}