1 //===-- Standalone implementation std::string_view --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGVIEW_H
10 #define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGVIEW_H
12 #include "src/__support/common.h"
16 namespace __llvm_libc
{
19 // This is very simple alternate of the std::string_view class. There is no
20 // bounds check performed in any of the methods. The callers are expected to
21 // do the checks before invoking the methods.
23 // This class will be extended as needed in future.
29 static size_t min(size_t A
, size_t B
) { return A
<= B
? A
: B
; }
31 LIBC_INLINE
static int compareMemory(const char *Lhs
, const char *Rhs
,
33 for (size_t I
= 0; I
< Length
; ++I
)
34 if (int Diff
= (int)Lhs
[I
] - (int)Rhs
[I
])
39 LIBC_INLINE
static constexpr size_t length(const char *Str
) {
40 for (const char *End
= Str
;; ++End
)
45 LIBC_INLINE
bool equals(string_view Other
) const {
46 return (Len
== Other
.Len
&&
47 compareMemory(Data
, Other
.Data
, Other
.Len
) == 0);
51 using value_type
= char;
52 using size_type
= size_t;
53 using difference_type
= ptrdiff_t;
54 using pointer
= char *;
55 using const_pointer
= const char *;
56 using reference
= char &;
57 using const_reference
= const char &;
58 using const_iterator
= char *;
59 using iterator
= const_iterator
;
61 // special value equal to the maximum value representable by the type
63 LIBC_INLINE_VAR
static constexpr size_t npos
= -1;
65 LIBC_INLINE
constexpr string_view() : Data(nullptr), Len(0) {}
67 // Assumes Str is a null-terminated string. The length of the string does
68 // not include the terminating null character.
69 // Preconditions: [Str, Str + ​length(Str)) is a valid range.
70 LIBC_INLINE
constexpr string_view(const char *Str
)
71 : Data(Str
), Len(length(Str
)) {}
73 // Preconditions: [Str, Str + N) is a valid range.
74 LIBC_INLINE
constexpr string_view(const char *Str
, size_t N
)
75 : Data(Str
), Len(N
) {}
77 LIBC_INLINE
constexpr const char *data() const { return Data
; }
79 // Returns the size of the string_view.
80 LIBC_INLINE
constexpr size_t size() const { return Len
; }
82 // Returns whether the string_view is empty.
83 LIBC_INLINE
constexpr bool empty() const { return Len
== 0; }
85 // Returns an iterator to the first character of the view.
86 LIBC_INLINE
const char *begin() const { return Data
; }
88 // Returns an iterator to the character following the last character of the
90 LIBC_INLINE
const char *end() const { return Data
+ Len
; }
92 // Returns a const reference to the character at specified location pos.
93 // No bounds checking is performed: the behavior is undefined if pos >=
95 LIBC_INLINE
constexpr const char &operator[](size_t Index
) const {
99 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
100 /// is lexicographically less than, equal to, or greater than the \p Other.
101 LIBC_INLINE
int compare(string_view Other
) const {
102 // Check the prefix for a mismatch.
103 if (int Res
= compareMemory(Data
, Other
.Data
, min(Len
, Other
.Len
)))
104 return Res
< 0 ? -1 : 1;
105 // Otherwise the prefixes match, so we only need to check the lengths.
106 if (Len
== Other
.Len
)
108 return Len
< Other
.Len
? -1 : 1;
111 LIBC_INLINE
bool operator==(string_view Other
) const { return equals(Other
); }
112 LIBC_INLINE
bool operator!=(string_view Other
) const {
113 return !(*this == Other
);
115 LIBC_INLINE
bool operator<(string_view Other
) const {
116 return compare(Other
) == -1;
118 LIBC_INLINE
bool operator<=(string_view Other
) const {
119 return compare(Other
) != 1;
121 LIBC_INLINE
bool operator>(string_view Other
) const {
122 return compare(Other
) == 1;
124 LIBC_INLINE
bool operator>=(string_view Other
) const {
125 return compare(Other
) != -1;
128 // Moves the start of the view forward by n characters.
129 // The behavior is undefined if n > size().
130 LIBC_INLINE
void remove_prefix(size_t N
) {
135 // Moves the end of the view back by n characters.
136 // The behavior is undefined if n > size().
137 LIBC_INLINE
void remove_suffix(size_t N
) { Len
-= N
; }
139 // Check if this string starts with the given Prefix.
140 LIBC_INLINE
bool starts_with(string_view Prefix
) const {
141 return Len
>= Prefix
.Len
&&
142 compareMemory(Data
, Prefix
.Data
, Prefix
.Len
) == 0;
145 // Check if this string starts with the given Prefix.
146 LIBC_INLINE
bool starts_with(const char Prefix
) const {
147 return !empty() && front() == Prefix
;
150 // Check if this string ends with the given Prefix.
151 LIBC_INLINE
bool ends_with(const char Suffix
) const {
152 return !empty() && back() == Suffix
;
155 // Check if this string ends with the given Suffix.
156 LIBC_INLINE
bool ends_with(string_view Suffix
) const {
157 return Len
>= Suffix
.Len
&&
158 compareMemory(end() - Suffix
.Len
, Suffix
.Data
, Suffix
.Len
) == 0;
161 // Return a reference to the substring from [Start, Start + N).
163 // Start The index of the starting character in the substring; if the index is
164 // npos or greater than the length of the string then the empty substring will
167 // N The number of characters to included in the substring. If N exceeds the
168 // number of characters remaining in the string, the string suffix (starting
169 // with Start) will be returned.
170 LIBC_INLINE string_view
substr(size_t Start
, size_t N
= npos
) const {
171 Start
= min(Start
, Len
);
172 return string_view(Data
+ Start
, min(N
, Len
- Start
));
175 // front - Get the first character in the string.
176 LIBC_INLINE
char front() const { return Data
[0]; }
178 // back - Get the last character in the string.
179 LIBC_INLINE
char back() const { return Data
[Len
- 1]; }
181 // Finds the first occurence of c in this view, starting at position From.
182 LIBC_INLINE
size_t find_first_of(const char c
, size_t From
= 0) const {
183 for (size_t Pos
= From
; Pos
< size(); ++Pos
)
184 if ((*this)[Pos
] == c
)
189 // Finds the last occurence of c in this view, ending at position End.
190 LIBC_INLINE
size_t find_last_of(const char c
, size_t End
= npos
) const {
191 End
= End
>= size() ? size() : End
+ 1;
192 for (; End
> 0; --End
)
193 if ((*this)[End
- 1] == c
)
200 } // namespace __llvm_libc
202 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_STRINGVIEW_H