Skip to content

Commit 5e3c291

Browse files
[libc] fix
1 parent e3b71d2 commit 5e3c291

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

libc/src/__support/CPP/string_view.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class LIBC_GSL_POINTER string_view {
4444
return static_cast<size_t>(End - Str);
4545
}
4646

47+
template <size_t N>
48+
LIBC_INLINE static constexpr size_t
49+
bounded_length(LIBC_LIFETIME_BOUND const char (&Str)[N]) {
50+
for (size_t i = 0; i < N; ++i)
51+
if (Str[i] == '\0')
52+
return i;
53+
return N;
54+
}
55+
4756
LIBC_INLINE bool equals(string_view Other) const {
4857
return (Len == Other.Len &&
4958
compareMemory(Data, Other.Data, Other.Len) == 0);
@@ -77,8 +86,9 @@ class LIBC_GSL_POINTER string_view {
7786
LIBC_INLINE constexpr string_view(const char *Str, size_t N)
7887
: Data(Str), Len(N) {}
7988

80-
LIBC_INLINE constexpr string_view(LIBC_LIFETIME_BOUND const char (&Str)[])
81-
: Data(Str), Len(length(Str)) {}
89+
template <size_t N>
90+
LIBC_INLINE constexpr string_view(LIBC_LIFETIME_BOUND const char (&Str)[N])
91+
: Data(Str), Len(bounded_length(Str)) {}
8292

8393
LIBC_INLINE constexpr const char *data() const { return Data; }
8494

0 commit comments

Comments
 (0)