LLVM 18.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 bool equals(StringRef RHS) const {
93 return str().equals(RHS);
94 }
95
96 /// Check for string equality, ignoring case.
98 return str().equals_insensitive(RHS);
99 }
100
101 /// compare - Compare two strings; the result is negative, zero, or positive
102 /// if this string is lexicographically less than, equal to, or greater than
103 /// the \p RHS.
104 int compare(StringRef RHS) const {
105 return str().compare(RHS);
106 }
107
108 /// compare_insensitive - Compare two strings, ignoring case.
110 return str().compare_insensitive(RHS);
111 }
112
113 /// compare_numeric - Compare two strings, treating sequences of digits as
114 /// numbers.
116 return str().compare_numeric(RHS);
117 }
118
119 /// @}
120 /// @name String Predicates
121 /// @{
122
123 /// startswith - Check if this string starts with the given \p Prefix.
124 bool startswith(StringRef Prefix) const {
125 return str().startswith(Prefix);
126 }
127
128 /// endswith - Check if this string ends with the given \p Suffix.
129 bool endswith(StringRef Suffix) const {
130 return str().endswith(Suffix);
131 }
132
133 /// @}
134 /// @name String Searching
135 /// @{
136
137 /// find - Search for the first character \p C in the string.
138 ///
139 /// \return - The index of the first occurrence of \p C, or npos if not
140 /// found.
141 size_t find(char C, size_t From = 0) const {
142 return str().find(C, From);
143 }
144
145 /// Search for the first string \p Str in the string.
146 ///
147 /// \returns The index of the first occurrence of \p Str, or npos if not
148 /// found.
149 size_t find(StringRef Str, size_t From = 0) const {
150 return str().find(Str, From);
151 }
152
153 /// Search for the last character \p C in the string.
154 ///
155 /// \returns The index of the last occurrence of \p C, or npos if not
156 /// found.
157 size_t rfind(char C, size_t From = StringRef::npos) const {
158 return str().rfind(C, From);
159 }
160
161 /// Search for the last string \p Str in the string.
162 ///
163 /// \returns The index of the last occurrence of \p Str, or npos if not
164 /// found.
165 size_t rfind(StringRef Str) const {
166 return str().rfind(Str);
167 }
168
169 /// Find the first character in the string that is \p C, or npos if not
170 /// found. Same as find.
171 size_t find_first_of(char C, size_t From = 0) const {
172 return str().find_first_of(C, From);
173 }
174
175 /// Find the first character in the string that is in \p Chars, or npos if
176 /// not found.
177 ///
178 /// Complexity: O(size() + Chars.size())
179 size_t find_first_of(StringRef Chars, size_t From = 0) const {
180 return str().find_first_of(Chars, From);
181 }
182
183 /// Find the first character in the string that is not \p C or npos if not
184 /// found.
185 size_t find_first_not_of(char C, size_t From = 0) const {
186 return str().find_first_not_of(C, From);
187 }
188
189 /// Find the first character in the string that is not in the string
190 /// \p Chars, or npos if not found.
191 ///
192 /// Complexity: O(size() + Chars.size())
193 size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
194 return str().find_first_not_of(Chars, From);
195 }
196
197 /// Find the last character in the string that is \p C, or npos if not
198 /// found.
199 size_t find_last_of(char C, size_t From = StringRef::npos) const {
200 return str().find_last_of(C, From);
201 }
202
203 /// Find the last character in the string that is in \p C, or npos if not
204 /// found.
205 ///
206 /// Complexity: O(size() + Chars.size())
208 StringRef Chars, size_t From = StringRef::npos) const {
209 return str().find_last_of(Chars, From);
210 }
211
212 /// @}
213 /// @name Helpful Algorithms
214 /// @{
215
216 /// Return the number of occurrences of \p C in the string.
217 size_t count(char C) const {
218 return str().count(C);
219 }
220
221 /// Return the number of non-overlapped occurrences of \p Str in the
222 /// string.
223 size_t count(StringRef Str) const {
224 return str().count(Str);
225 }
226
227 /// @}
228 /// @name Substring Operations
229 /// @{
230
231 /// Return a reference to the substring from [Start, Start + N).
232 ///
233 /// \param Start The index of the starting character in the substring; if
234 /// the index is npos or greater than the length of the string then the
235 /// empty substring will be returned.
236 ///
237 /// \param N The number of characters to included in the substring. If \p N
238 /// exceeds the number of characters remaining in the string, the string
239 /// suffix (starting with \p Start) will be returned.
240 StringRef substr(size_t Start, size_t N = StringRef::npos) const {
241 return str().substr(Start, N);
242 }
243
244 /// Return a reference to the substring from [Start, End).
245 ///
246 /// \param Start The index of the starting character in the substring; if
247 /// the index is npos or greater than the length of the string then the
248 /// empty substring will be returned.
249 ///
250 /// \param End The index following the last character to include in the
251 /// substring. If this is npos, or less than \p Start, or exceeds the
252 /// number of characters remaining in the string, the string suffix
253 /// (starting with \p Start) will be returned.
254 StringRef slice(size_t Start, size_t End) const {
255 return str().slice(Start, End);
256 }
257
258 // Extra methods.
259
260 /// Explicit conversion to StringRef.
261 StringRef str() const { return StringRef(this->data(), this->size()); }
262
263 // TODO: Make this const, if it's safe...
264 const char* c_str() {
265 this->push_back(0);
266 this->pop_back();
267 return this->data();
268 }
269
270 /// Implicit conversion to StringRef.
271 operator StringRef() const { return str(); }
272
273 explicit operator std::string() const {
274 return std::string(this->data(), this->size());
275 }
276
277 // Extra operators.
279 this->assign(RHS);
280 return *this;
281 }
282
284 this->append(RHS.begin(), RHS.end());
285 return *this;
286 }
288 this->push_back(C);
289 return *this;
290 }
291};
292
293} // end namespace llvm
294
295#endif // LLVM_ADT_SMALLSTRING_H
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:478
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:165
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:129
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:179
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:207
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:199
SmallString & operator=(StringRef RHS)
Definition: SmallString.h:278
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:124
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:141
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:149
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:223
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:254
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition: SmallString.h:157
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition: SmallString.h:73
SmallString & operator+=(char C)
Definition: SmallString.h:287
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:51
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:283
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:97
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: SmallString.h:115
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: SmallString.h:104
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition: SmallString.h:240
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:193
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:109
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:217
const char * c_str()
Definition: SmallString.h:264
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:171
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:261
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:185
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:645
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:708
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
void push_back(const T &Elt)
Definition: SmallVector.h:416
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:289
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
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:575
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:688
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:404
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
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:381
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:351
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:301
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:455
bool endswith(StringRef Suffix) const
Definition: StringRef.h:280
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