LLVM 19.0.0git
SmallString.h
Go to the documentation of this file.
1//===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the SmallString class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_SMALLSTRING_H
15#define LLVM_ADT_SMALLSTRING_H
16
18#include "llvm/ADT/StringRef.h"
19#include <cstddef>
20
21namespace llvm {
22
23/// SmallString - A SmallString is just a SmallVector with methods and accessors
24/// that make it work better as a string (e.g. operator+ etc).
25template<unsigned InternalLen>
26class SmallString : public SmallVector<char, InternalLen> {
27public:
28 /// Default ctor - Initialize to empty.
29 SmallString() = default;
30
31 /// Initialize from a StringRef.
32 SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
33
34 /// Initialize by concatenating a list of StringRefs.
35 SmallString(std::initializer_list<StringRef> Refs)
36 : SmallVector<char, InternalLen>() {
37 this->append(Refs);
38 }
39
40 /// Initialize with a range.
41 template<typename ItTy>
42 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
43
44 /// @}
45 /// @name String Assignment
46 /// @{
47
48 using SmallVector<char, InternalLen>::assign;
49
50 /// Assign from a StringRef.
53 }
54
55 /// Assign from a list of StringRefs.
56 void assign(std::initializer_list<StringRef> Refs) {
57 this->clear();
58 append(Refs);
59 }
60
61 /// @}
62 /// @name String Concatenation
63 /// @{
64
65 using SmallVector<char, InternalLen>::append;
66
67 /// Append from a StringRef.
70 }
71
72 /// Append from a list of StringRefs.
73 void append(std::initializer_list<StringRef> Refs) {
74 size_t CurrentSize = this->size();
75 size_t SizeNeeded = CurrentSize;
76 for (const StringRef &Ref : Refs)
77 SizeNeeded += Ref.size();
78 this->resize_for_overwrite(SizeNeeded);
79 for (const StringRef &Ref : Refs) {
80 std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize);
81 CurrentSize += Ref.size();
82 }
83 assert(CurrentSize == this->size());
84 }
85
86 /// @}
87 /// @name String Comparison
88 /// @{
89
90 /// Check for string equality. This is more efficient than compare() when
91 /// the relative ordering of inequal strings isn't needed.
92 [[nodiscard]] bool equals(StringRef RHS) const { return str().equals(RHS); }
93
94 /// Check for string equality, ignoring case.
95 [[nodiscard]] bool equals_insensitive(StringRef RHS) const {
96 return str().equals_insensitive(RHS);
97 }
98
99 /// compare - Compare two strings; the result is negative, zero, or positive
100 /// if this string is lexicographically less than, equal to, or greater than
101 /// the \p RHS.
102 [[nodiscard]] int compare(StringRef RHS) const { return str().compare(RHS); }
103
104 /// compare_insensitive - Compare two strings, ignoring case.
105 [[nodiscard]] int compare_insensitive(StringRef RHS) const {
106 return str().compare_insensitive(RHS);
107 }
108
109 /// compare_numeric - Compare two strings, treating sequences of digits as
110 /// numbers.
111 [[nodiscard]] int compare_numeric(StringRef RHS) const {
112 return str().compare_numeric(RHS);
113 }
114
115 /// @}
116 /// @name String Predicates
117 /// @{
118
119 /// starts_with - Check if this string starts with the given \p Prefix.
120 [[nodiscard]] bool starts_with(StringRef Prefix) const {
121 return str().starts_with(Prefix);
122 }
123
124 /// ends_with - Check if this string ends with the given \p Suffix.
125 [[nodiscard]] bool ends_with(StringRef Suffix) const {
126 return str().ends_with(Suffix);
127 }
128
129 /// @}
130 /// @name String Searching
131 /// @{
132
133 /// find - Search for the first character \p C in the string.
134 ///
135 /// \return - The index of the first occurrence of \p C, or npos if not
136 /// found.
137 [[nodiscard]] size_t find(char C, size_t From = 0) const {
138 return str().find(C, From);
139 }
140
141 /// Search for the first string \p Str in the string.
142 ///
143 /// \returns The index of the first occurrence of \p Str, or npos if not
144 /// found.
145 [[nodiscard]] size_t find(StringRef Str, size_t From = 0) const {
146 return str().find(Str, From);
147 }
148
149 /// Search for the last character \p C in the string.
150 ///
151 /// \returns The index of the last occurrence of \p C, or npos if not
152 /// found.
153 [[nodiscard]] size_t rfind(char C, size_t From = StringRef::npos) const {
154 return str().rfind(C, From);
155 }
156
157 /// Search for the last string \p Str in the string.
158 ///
159 /// \returns The index of the last occurrence of \p Str, or npos if not
160 /// found.
161 [[nodiscard]] size_t rfind(StringRef Str) const { return str().rfind(Str); }
162
163 /// Find the first character in the string that is \p C, or npos if not
164 /// found. Same as find.
165 [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
166 return str().find_first_of(C, From);
167 }
168
169 /// Find the first character in the string that is in \p Chars, or npos if
170 /// not found.
171 ///
172 /// Complexity: O(size() + Chars.size())
173 [[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const {
174 return str().find_first_of(Chars, From);
175 }
176
177 /// Find the first character in the string that is not \p C or npos if not
178 /// found.
179 [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const {
180 return str().find_first_not_of(C, From);
181 }
182
183 /// Find the first character in the string that is not in the string
184 /// \p Chars, or npos if not found.
185 ///
186 /// Complexity: O(size() + Chars.size())
187 [[nodiscard]] size_t find_first_not_of(StringRef Chars,
188 size_t From = 0) const {
189 return str().find_first_not_of(Chars, From);
190 }
191
192 /// Find the last character in the string that is \p C, or npos if not
193 /// found.
194 [[nodiscard]] size_t find_last_of(char C,
195 size_t From = StringRef::npos) const {
196 return str().find_last_of(C, From);
197 }
198
199 /// Find the last character in the string that is in \p C, or npos if not
200 /// found.
201 ///
202 /// Complexity: O(size() + Chars.size())
203 [[nodiscard]] size_t find_last_of(StringRef Chars,
204 size_t From = StringRef::npos) const {
205 return str().find_last_of(Chars, From);
206 }
207
208 /// @}
209 /// @name Helpful Algorithms
210 /// @{
211
212 /// Return the number of occurrences of \p C in the string.
213 [[nodiscard]] size_t count(char C) const { return str().count(C); }
214
215 /// Return the number of non-overlapped occurrences of \p Str in the
216 /// string.
217 [[nodiscard]] size_t count(StringRef Str) const { return str().count(Str); }
218
219 /// @}
220 /// @name Substring Operations
221 /// @{
222
223 /// Return a reference to the substring from [Start, Start + N).
224 ///
225 /// \param Start The index of the starting character in the substring; if
226 /// the index is npos or greater than the length of the string then the
227 /// empty substring will be returned.
228 ///
229 /// \param N The number of characters to included in the substring. If \p N
230 /// exceeds the number of characters remaining in the string, the string
231 /// suffix (starting with \p Start) will be returned.
232 [[nodiscard]] StringRef substr(size_t Start,
233 size_t N = StringRef::npos) const {
234 return str().substr(Start, N);
235 }
236
237 /// Return a reference to the substring from [Start, End).
238 ///
239 /// \param Start The index of the starting character in the substring; if
240 /// the index is npos or greater than the length of the string then the
241 /// empty substring will be returned.
242 ///
243 /// \param End The index following the last character to include in the
244 /// substring. If this is npos, or less than \p Start, or exceeds the
245 /// number of characters remaining in the string, the string suffix
246 /// (starting with \p Start) will be returned.
247 [[nodiscard]] StringRef slice(size_t Start, size_t End) const {
248 return str().slice(Start, End);
249 }
250
251 // Extra methods.
252
253 /// Explicit conversion to StringRef.
254 [[nodiscard]] StringRef str() const {
255 return StringRef(this->data(), this->size());
256 }
257
258 // TODO: Make this const, if it's safe...
259 const char* c_str() {
260 this->push_back(0);
261 this->pop_back();
262 return this->data();
263 }
264
265 /// Implicit conversion to StringRef.
266 operator StringRef() const { return str(); }
267
268 explicit operator std::string() const {
269 return std::string(this->data(), this->size());
270 }
271
272 // Extra operators.
274 this->assign(RHS);
275 return *this;
276 }
277
279 this->append(RHS.begin(), RHS.end());
280 return *this;
281 }
283 this->push_back(C);
284 return *this;
285 }
286};
287
288} // end namespace llvm
289
290#endif // LLVM_ADT_SMALLSTRING_H
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:480
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Value * RHS
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
Definition: SmallString.h:161
size_t find_first_of(StringRef Chars, size_t From=0) const
Find the first character in the string that is in Chars, or npos if not found.
Definition: SmallString.h:173
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:32
size_t find_last_of(StringRef Chars, size_t From=StringRef::npos) const
Find the last character in the string that is in C, or npos if not found.
Definition: SmallString.h:203
size_t find_last_of(char C, size_t From=StringRef::npos) const
Find the last character in the string that is C, or npos if not found.
Definition: SmallString.h:194
SmallString & operator=(StringRef RHS)
Definition: SmallString.h:273
void assign(std::initializer_list< StringRef > Refs)
Assign from a list of StringRefs.
Definition: SmallString.h:56
SmallString()=default
Default ctor - Initialize to empty.
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:137
bool ends_with(StringRef Suffix) const
ends_with - Check if this string ends with the given Suffix.
Definition: SmallString.h:125
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:145
bool equals(StringRef RHS) const
Check for string equality.
Definition: SmallString.h:92
size_t count(StringRef Str) const
Return the number of non-overlapped occurrences of Str in the string.
Definition: SmallString.h:217
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:247
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition: SmallString.h:153
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition: SmallString.h:73
SmallString & operator+=(char C)
Definition: SmallString.h:282
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:51
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:278
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:95
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: SmallString.h:111
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: SmallString.h:102
bool starts_with(StringRef Prefix) const
starts_with - Check if this string starts with the given Prefix.
Definition: SmallString.h:120
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition: SmallString.h:232
size_t find_first_not_of(StringRef Chars, size_t From=0) const
Find the first character in the string that is not in the string Chars, or npos if not found.
Definition: SmallString.h:187
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:42
int compare_insensitive(StringRef RHS) const
compare_insensitive - Compare two strings, ignoring case.
Definition: SmallString.h:105
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:213
const char * c_str()
Definition: SmallString.h:259
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: SmallString.h:165
SmallString(std::initializer_list< StringRef > Refs)
Initialize by concatenating a list of StringRefs.
Definition: SmallString.h:35
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: SmallString.h:179
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:654
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
iterator begin() const
Definition: StringRef.h:111
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:680
size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:396
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:61
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:373
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:343
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:164
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:293
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:447
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:271
static constexpr size_t npos
Definition: StringRef.h:52
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: StringRef.h:177
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: StringRef.h:170
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:251
int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
Definition: StringRef.cpp:37
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Ref
The access may reference the value stored in memory.
#define N