LLVM 24.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"
20#include <cstddef>
21
22namespace llvm {
23
24/// SmallString - A SmallString is just a SmallVector with methods and accessors
25/// that make it work better as a string (e.g. operator+ etc).
26template <unsigned InternalLen>
28 : public SmallVector<char, InternalLen> {
29public:
30 /// Default ctor - Initialize to empty.
31 SmallString() = default;
32
33 /// Initialize from a StringRef.
34 SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
35
36 /// Initialize by concatenating a list of StringRefs.
37 SmallString(std::initializer_list<StringRef> Refs)
38 : SmallVector<char, InternalLen>() {
39 this->append(Refs);
40 }
41
42 /// Initialize with a range.
43 template<typename ItTy>
44 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
45
46 /// @}
47 /// @name String Assignment
48 /// @{
49
50 using SmallVector<char, InternalLen>::assign;
51
52 /// Assign from a StringRef.
55 }
56
57 /// Assign from a list of StringRefs.
58 void assign(std::initializer_list<StringRef> Refs) {
59 this->clear();
60 append(Refs);
61 }
62
63 /// @}
64 /// @name String Concatenation
65 /// @{
66
67 using SmallVector<char, InternalLen>::append;
68
69 /// Append from a StringRef.
72 }
73
74 /// Append from a list of StringRefs.
75 void append(std::initializer_list<StringRef> Refs) {
76 size_t CurrentSize = this->size();
77 size_t SizeNeeded = CurrentSize;
78 for (const StringRef &Ref : Refs)
79 SizeNeeded += Ref.size();
80 this->resize_for_overwrite(SizeNeeded);
81 for (const StringRef &Ref : Refs) {
82 std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize);
83 CurrentSize += Ref.size();
84 }
85 assert(CurrentSize == this->size());
86 }
87
88 /// @}
89 /// @name String Comparison
90 /// @{
91
92 /// Check for string equality. This is more efficient than compare() when
93 /// the relative ordering of inequal strings isn't needed.
94 [[nodiscard]] bool equals(StringRef RHS) const { return str() == RHS; }
95
96 /// Check for string equality, ignoring case.
97 [[nodiscard]] bool equals_insensitive(StringRef RHS) const {
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 [[nodiscard]] int compare(StringRef RHS) const { return str().compare(RHS); }
105
106 /// compare_insensitive - Compare two strings, ignoring case.
107 [[nodiscard]] int compare_insensitive(StringRef RHS) const {
108 return str().compare_insensitive(RHS);
109 }
110
111 /// compare_numeric - Compare two strings, treating sequences of digits as
112 /// numbers.
113 [[nodiscard]] int compare_numeric(StringRef RHS) const {
114 return str().compare_numeric(RHS);
115 }
116
117 /// @}
118 /// @name String Predicates
119 /// @{
120
121 /// starts_with - Check if this string starts with the given \p Prefix.
122 [[nodiscard]] bool starts_with(StringRef Prefix) const {
123 return str().starts_with(Prefix);
124 }
125
126 /// starts_with - Check if this string starts with the given character \p C.
127 [[nodiscard]] bool starts_with(char C) const { return str().starts_with(C); }
128
129 /// ends_with - Check if this string ends with the given \p Suffix.
130 [[nodiscard]] bool ends_with(StringRef Suffix) const {
131 return str().ends_with(Suffix);
132 }
133
134 /// ends_with - Check if this string ends with the given character \p C.
135 [[nodiscard]] bool ends_with(char C) const { return str().ends_with(C); }
136
137 /// contains - Check if \p Other is a substring of this string.
138 [[nodiscard]] bool contains(StringRef Other) const {
139 return str().contains(Other);
140 }
141
142 /// contains - Check if this string contains the character \p C.
143 [[nodiscard]] bool contains(char C) const { return str().contains(C); }
144
145 /// @}
146 /// @name String Searching
147 /// @{
148
149 /// find - Search for the first character \p C in the string.
150 ///
151 /// \return - The index of the first occurrence of \p C, or npos if not
152 /// found.
153 [[nodiscard]] size_t find(char C, size_t From = 0) const {
154 return str().find(C, From);
155 }
156
157 /// Search for the first string \p Str in the string.
158 ///
159 /// \returns The index of the first occurrence of \p Str, or npos if not
160 /// found.
161 [[nodiscard]] size_t find(StringRef Str, size_t From = 0) const {
162 return str().find(Str, From);
163 }
164
165 /// Search for the last character \p C in the string.
166 ///
167 /// \returns The index of the last occurrence of \p C, or npos if not
168 /// found.
169 [[nodiscard]] size_t rfind(char C, size_t From = StringRef::npos) const {
170 return str().rfind(C, From);
171 }
172
173 /// Search for the last string \p Str in the string.
174 ///
175 /// \returns The index of the last occurrence of \p Str, or npos if not
176 /// found.
177 [[nodiscard]] size_t rfind(StringRef Str) const { return str().rfind(Str); }
178
179 /// Find the first character in the string that is \p C, or npos if not
180 /// found. Same as find.
181 [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
182 return str().find_first_of(C, From);
183 }
184
185 /// Find the first character in the string that is in \p Chars, or npos if
186 /// not found.
187 ///
188 /// Complexity: O(size() + Chars.size())
189 [[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const {
190 return str().find_first_of(Chars, From);
191 }
192
193 /// Find the first character in the string that is not \p C or npos if not
194 /// found.
195 [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const {
196 return str().find_first_not_of(C, From);
197 }
198
199 /// Find the first character in the string that is not in the string
200 /// \p Chars, or npos if not found.
201 ///
202 /// Complexity: O(size() + Chars.size())
203 [[nodiscard]] size_t find_first_not_of(StringRef Chars,
204 size_t From = 0) const {
205 return str().find_first_not_of(Chars, From);
206 }
207
208 /// Find the last character in the string that is \p C, or npos if not
209 /// found.
210 [[nodiscard]] size_t find_last_of(char C,
211 size_t From = StringRef::npos) const {
212 return str().find_last_of(C, From);
213 }
214
215 /// Find the last character in the string that is in \p C, or npos if not
216 /// found.
217 ///
218 /// Complexity: O(size() + Chars.size())
219 [[nodiscard]] size_t find_last_of(StringRef Chars,
220 size_t From = StringRef::npos) const {
221 return str().find_last_of(Chars, From);
222 }
223
224 /// @}
225 /// @name Helpful Algorithms
226 /// @{
227
228 /// Return the number of occurrences of \p C in the string.
229 [[nodiscard]] size_t count(char C) const { return str().count(C); }
230
231 /// Return the number of non-overlapped occurrences of \p Str in the
232 /// string.
233 [[nodiscard]] size_t count(StringRef Str) const { return str().count(Str); }
234
235 /// @}
236 /// @name Substring Operations
237 /// @{
238
239 /// Return a reference to the substring from [Start, Start + N).
240 ///
241 /// \param Start The index of the starting character in the substring; if
242 /// the index is npos or greater than the length of the string then the
243 /// empty substring will be returned.
244 ///
245 /// \param N The number of characters to included in the substring. If \p N
246 /// exceeds the number of characters remaining in the string, the string
247 /// suffix (starting with \p Start) will be returned.
248 [[nodiscard]] StringRef substr(size_t Start,
249 size_t N = StringRef::npos) const {
250 return str().substr(Start, N);
251 }
252
253 /// Return a reference to the substring from [Start, End).
254 ///
255 /// \param Start The index of the starting character in the substring; if
256 /// the index is npos or greater than the length of the string then the
257 /// empty substring will be returned.
258 ///
259 /// \param End The index following the last character to include in the
260 /// substring. If this is npos, or less than \p Start, or exceeds the
261 /// number of characters remaining in the string, the string suffix
262 /// (starting with \p Start) will be returned.
263 [[nodiscard]] StringRef slice(size_t Start, size_t End) const {
264 return str().slice(Start, End);
265 }
266
267 // Extra methods.
268
269 /// Explicit conversion to StringRef.
270 [[nodiscard]] StringRef str() const {
271 return StringRef(this->data(), this->size());
272 }
273
274 // TODO: Make this const, if it's safe...
275 const char* c_str() {
276 this->push_back(0);
277 this->pop_back();
278 return this->data();
279 }
280
281 /// Implicit conversion to StringRef.
282 operator StringRef() const { return str(); }
283
284 explicit operator std::string() const {
285 return std::string(this->data(), this->size());
286 }
287
288 // Extra operators.
290 this->assign(RHS);
291 return *this;
292 }
293
295 this->append(RHS.begin(), RHS.end());
296 return *this;
297 }
299 this->push_back(C);
300 return *this;
301 }
302};
303
304} // end namespace llvm
305
306#endif // LLVM_ADT_SMALLSTRING_H
Ops append({Chain, DAG.getConstant(SVEStores[NumParts - 2], DL, MVT::i32)})
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
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:34
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:58
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:94
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:75
SmallString & operator+=(char C)
void assign(StringRef RHS)
Assign from a StringRef.
Definition SmallString.h:53
SmallString & operator+=(StringRef RHS)
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.
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:70
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition SmallString.h:44
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:37
StringRef str() const
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
iterator begin() const
Definition StringRef.h:114
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
static void assign(DXContainerYAML::SourceInfo::SectionHeader &Dst, const dxbc::SourceInfo::SectionHeader &Src)
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ Other
Any other memory.
Definition ModRef.h:68
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
#define N