Skip to content

Commit 4f34e64

Browse files
just use string_length
1 parent b0b7b9a commit 4f34e64

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

libc/src/string/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ add_header_library(
1717
DEPENDS
1818
.memory_utils.inline_bzero
1919
.memory_utils.inline_memcpy
20+
libc.hdr.types.size_t
2021
libc.include.stdlib
21-
libc.src.__support.common
2222
libc.src.__support.CPP.bitset
23+
libc.src.__support.CPP.type_traits
24+
libc.src.__support.common
2325
${string_config_options}
2426
)
2527

libc/src/string/string_utils.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
#ifndef LLVM_LIBC_SRC_STRING_STRING_UTILS_H
1515
#define LLVM_LIBC_SRC_STRING_STRING_UTILS_H
1616

17+
#include "hdr/types/size_t.h"
1718
#include "src/__support/CPP/bitset.h"
19+
#include "src/__support/CPP/type_traits.h" // cpp::is_same_v
1820
#include "src/__support/macros/config.h"
1921
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2022
#include "src/string/memory_utils/inline_bzero.h"
2123
#include "src/string/memory_utils/inline_memcpy.h"
22-
#include <stddef.h> // For size_t
2324

2425
namespace LIBC_NAMESPACE_DECL {
2526
namespace internal {
@@ -79,25 +80,22 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) {
7980
return char_ptr - src;
8081
}
8182

82-
template<typename T>
83-
LIBC_INLINE size_t string_length_trivial(const T *src) {
84-
size_t length;
85-
for (length = 0; *src; ++src, ++length)
86-
;
87-
return length;
88-
}
89-
9083
// Returns the length of a string, denoted by the first occurrence
9184
// of a null terminator.
92-
LIBC_INLINE size_t string_length(const char *src) {
85+
template<typename T>
86+
LIBC_INLINE size_t string_length(const T *src) {
9387
#ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
9488
// Unsigned int is the default size for most processors, and on x86-64 it
9589
// performs better than larger sizes when the src pointer can't be assumed to
9690
// be aligned to a word boundary, so it's the size we use for reading the
9791
// string a block at a time.
98-
return string_length_wide_read<unsigned int>(src);
92+
if constexpr (cpp::is_same_v<T, char>)
93+
return string_length_wide_read<unsigned int>(src);
9994
#else
100-
return string_length_trivial(src);
95+
size_t length;
96+
for (length = 0; *src; ++src, ++length)
97+
;
98+
return length;
10199
#endif
102100
}
103101

libc/src/wchar/wcslen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818

1919
LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *src)) {
20-
return internal::string_length_trivial(src);
20+
return internal::string_length(src);
2121
}
2222

2323
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)