LLVM 23.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() == 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 /// starts_with - Check if this string starts with the given character \p C.
125 [[nodiscard]] bool starts_with(char C) const { return str().starts_with(C); }
126
127 /// ends_with - Check if this string ends with the given \p Suffix.
128 [[nodiscard]] bool ends_with(StringRef Suffix) const {
129 return str().ends_with(Suffix);
130 }
131
132 /// ends_with - Check if this string ends with the given character \p C.
133 [[nodiscard]] bool ends_with(char C) const { return str().ends_with(C); }
134
135 /// contains - Check if \p Other is a substring of this string.
136 [[nodiscard]] bool contains(StringRef Other) const {
137 return str().contains(Other);
138 }
139
140 /// contains - Check if this string contains the character \p C.
141 [[nodiscard]] bool contains(char C) const { return str().contains(C); }
142
143 /// @}
144 /// @name String Searching
145 /// @{
146
147 /// find - Search for the first character \p C in the string.
148 ///
149 /// \return - The index of the first occurrence of \p C, or npos if not
150 /// found.
151 [[nodiscard]] size_t find(char C, size_t From = 0) const {
152 return str().find(C, From);
153 }
154
155 /// Search for the first string \p Str in the string.
156 ///
157 /// \returns The index of the first occurrence of \p Str, or npos if not
158 /// found.
159 [[nodiscard]] size_t find(StringRef Str, size_t From = 0) const {
160 return str().find(Str, From);
161 }
162
163 /// Search for the last character \p C in the string.
164 ///
165 /// \returns The index of the last occurrence of \p C, or npos if not
166 /// found.
167 [[nodiscard]] size_t rfind(char C, size_t From = StringRef::npos) const {
168 return str().rfind(C, From);
169 }
170
171 /// Search for the last string \p Str in the string.
172 ///
173 /// \returns The index of the last occurrence of \p Str, or npos if not
174 /// found.
175 [[nodiscard]] size_t rfind(StringRef Str) const { return str().rfind(Str); }
176
177 /// Find the first character in the string that is \p C, or npos if not
178 /// found. Same as find.
179 [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
180 return str().find_first_of(C, From);
181 }
182
183 /// Find the first character in the string that is in \p Chars, or npos if
184 /// not found.
185 ///
186 /// Complexity: O(size() + Chars.size())
187 [[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const {
188 return str().find_first_of(Chars, From);
189 }
190
191 /// Find the first character in the string that is not \p C or npos if not
192 /// found.
193 [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const {
194 return str().find_first_not_of(C, From);
195 }
196
197 /// Find the first character in the string that is not in the string
198 /// \p Chars, or npos if not found.
199 ///
200 /// Complexity: O(size() + Chars.size())
201 [[nodiscard]] size_t find_first_not_of(StringRef Chars,
202 size_t From = 0) const {
203 return str().find_first_not_of(Chars, From);
204 }
205
206 /// Find the last character in the string that is \p C, or npos if not
207 /// found.
208 [[nodiscard]] size_t find_last_of(char C,
209 size_t From = StringRef::npos) const {
210 return str().find_last_of(C, From);
211 }
212
213 /// Find the last character in the string that is in \p C, or npos if not
214 /// found.
215 ///
216 /// Complexity: O(size() + Chars.size())
217 [[nodiscard]] size_t find_last_of(StringRef Chars,
218 size_t From = StringRef::npos) const {
219 return str().find_last_of(Chars, From);
220 }
221
222 /// @}
223 /// @name Helpful Algorithms
224 /// @{
225
226 /// Return the number of occurrences of \p C in the string.
227 [[nodiscard]] size_t count(char C) const { return str().count(C); }
228
229 /// Return the number of non-overlapped occurrences of \p Str in the
230 /// string.
231 [[nodiscard]] size_t count(StringRef Str) const { return str().count(Str); }
232
233 /// @}
234 /// @name Substring Operations
235 /// @{
236
237 /// Return a reference to the substring from [Start, Start + N).
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 N The number of characters to included in the substring. If \p N
244 /// exceeds the number of characters remaining in the string, the string
245 /// suffix (starting with \p Start) will be returned.
246 [[nodiscard]] StringRef substr(size_t Start,
247 size_t N = StringRef::npos) const {
248 return str().substr(Start, N);
249 }
250
251 /// Return a reference to the substring from [Start, End).
252 ///
253 /// \param Start The index of the starting character in the substring; if
254 /// the index is npos or greater than the length of the string then the
255 /// empty substring will be returned.
256 ///
257 /// \param End The index following the last character to include in the
258 /// substring. If this is npos, or less than \p Start, or exceeds the
259 /// number of characters remaining in the string, the string suffix
260 /// (starting with \p Start) will be returned.
261 [[nodiscard]] StringRef slice(size_t Start, size_t End) const {
262 return str().slice(Start, End);
263 }
264
265 // Extra methods.
266
267 /// Explicit conversion to StringRef.
268 [[nodiscard]] StringRef str() const {
269 return StringRef(this->data(), this->size());
270 }
271
272 // TODO: Make this const, if it's safe...
273 const char* c_str() {
274 this->push_back(0);
275 this->pop_back();
276 return this->data();
277 }
278
279 /// Implicit conversion to StringRef.
280 operator StringRef() const { return str(); }
281
282 explicit operator std::string() const {
283 return std::string(this->data(), this->size());
284 }
285
286 // Extra operators.
288 this->assign(RHS);
289 return *this;
290 }
291
293 this->append(RHS.begin(), RHS.end());
294 return *this;
295 }
297 this->push_back(C);
298 return *this;
299 }
300};
301
302} // end namespace llvm
303
304#endif // LLVM_ADT_SMALLSTRING_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the SmallVector class.
static Split data
Value * RHS
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
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.
bool ends_with(char C) const
ends_with - Check if this string ends with the given character C.
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.
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.
SmallString & operator=(StringRef RHS)
bool contains(StringRef Other) const
contains - Check if Other is a substring of this string.
bool contains(char C) const
contains - Check if this string contains the character C.
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.
bool ends_with(StringRef Suffix) const
ends_with - Check if this string ends with the given Suffix.
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
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.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition SmallString.h:73
SmallString & operator+=(char C)
void assign(StringRef RHS)
Assign from a StringRef.
Definition SmallString.h:51
SmallString & operator+=(StringRef RHS)
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.
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
bool starts_with(StringRef Prefix) const
starts_with - Check if this string starts with the given Prefix.
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
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.
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.
size_t count(char C) const
Return the number of occurrences of C in the string.
bool starts_with(char C) const
starts_with - Check if this string starts with the given character C.
const char * c_str()
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.
SmallString(std::initializer_list< StringRef > Refs)
Initialize by concatenating a list of StringRefs.
Definition SmallString.h:35
StringRef str() const
Explicit conversion to StringRef.
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.
void resize_for_overwrite(size_type N)
void assign(size_type NumElts, ValueParamT Elt)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static constexpr size_t npos
Definition StringRef.h:57
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
iterator begin() const
Definition StringRef.h:113
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:714
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:421
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:446
LLVM_ABI int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition StringRef.cpp:57
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:396
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition StringRef.h:365
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:290
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition StringRef.h:471
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition StringRef.h:270
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition StringRef.h:176
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition StringRef.h:169
LLVM_ABI 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.
LLVM_ABI int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
Definition StringRef.cpp:32
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ Other
Any other memory.
Definition ModRef.h:68
#define N