LLVM  14.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 // This file defines the SmallString class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ADT_SMALLSTRING_H
14 #define LLVM_ADT_SMALLSTRING_H
15 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <cstddef>
19 
20 namespace llvm {
21 
22 /// SmallString - A SmallString is just a SmallVector with methods and accessors
23 /// that make it work better as a string (e.g. operator+ etc).
24 template<unsigned InternalLen>
25 class SmallString : public SmallVector<char, InternalLen> {
26 public:
27  /// Default ctor - Initialize to empty.
28  SmallString() = default;
29 
30  /// Initialize from a StringRef.
31  SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
32 
33  /// Initialize by concatenating a list of StringRefs.
34  SmallString(std::initializer_list<StringRef> Refs)
35  : SmallVector<char, InternalLen>() {
36  this->append(Refs);
37  }
38 
39  /// Initialize with a range.
40  template<typename ItTy>
41  SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
42 
43  /// @}
44  /// @name String Assignment
45  /// @{
46 
48 
49  /// Assign from a StringRef.
51  SmallVectorImpl<char>::assign(RHS.begin(), RHS.end());
52  }
53 
54  /// Assign from a list of StringRefs.
55  void assign(std::initializer_list<StringRef> Refs) {
56  this->clear();
57  append(Refs);
58  }
59 
60  /// @}
61  /// @name String Concatenation
62  /// @{
63 
65 
66  /// Append from a StringRef.
68  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
69  }
70 
71  /// Append from a list of StringRefs.
72  void append(std::initializer_list<StringRef> Refs) {
73  size_t CurrentSize = this->size();
74  size_t SizeNeeded = CurrentSize;
75  for (const StringRef &Ref : Refs)
76  SizeNeeded += Ref.size();
77  this->resize_for_overwrite(SizeNeeded);
78  for (const StringRef &Ref : Refs) {
79  std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize);
80  CurrentSize += Ref.size();
81  }
82  assert(CurrentSize == this->size());
83  }
84 
85  /// @}
86  /// @name String Comparison
87  /// @{
88 
89  /// Check for string equality. This is more efficient than compare() when
90  /// the relative ordering of inequal strings isn't needed.
91  bool equals(StringRef RHS) const {
92  return str().equals(RHS);
93  }
94 
95  /// Check for string equality, ignoring case.
97  return str().equals_insensitive(RHS);
98  }
99 
100  /// Compare two strings; the result is -1, 0, or 1 if this string is
101  /// lexicographically less than, equal to, or greater than the \p RHS.
102  int compare(StringRef RHS) const {
103  return str().compare(RHS);
104  }
105 
106  /// compare_insensitive - Compare two strings, ignoring case.
108  return str().compare_insensitive(RHS);
109  }
110 
111  /// compare_numeric - Compare two strings, treating sequences of digits as
112  /// numbers.
114  return str().compare_numeric(RHS);
115  }
116 
117  /// @}
118  /// @name String Predicates
119  /// @{
120 
121  /// startswith - Check if this string starts with the given \p Prefix.
123  return str().startswith(Prefix);
124  }
125 
126  /// endswith - Check if this string ends with the given \p Suffix.
127  bool endswith(StringRef Suffix) const {
128  return str().endswith(Suffix);
129  }
130 
131  /// @}
132  /// @name String Searching
133  /// @{
134 
135  /// find - Search for the first character \p C in the string.
136  ///
137  /// \return - The index of the first occurrence of \p C, or npos if not
138  /// found.
139  size_t find(char C, size_t From = 0) const {
140  return str().find(C, From);
141  }
142 
143  /// Search for the first string \p Str in the string.
144  ///
145  /// \returns The index of the first occurrence of \p Str, or npos if not
146  /// found.
147  size_t find(StringRef Str, size_t From = 0) const {
148  return str().find(Str, From);
149  }
150 
151  /// Search for the last character \p C in the string.
152  ///
153  /// \returns The index of the last occurrence of \p C, or npos if not
154  /// found.
155  size_t rfind(char C, size_t From = StringRef::npos) const {
156  return str().rfind(C, From);
157  }
158 
159  /// Search for the last string \p Str in the string.
160  ///
161  /// \returns The index of the last occurrence of \p Str, or npos if not
162  /// found.
163  size_t rfind(StringRef Str) const {
164  return str().rfind(Str);
165  }
166 
167  /// Find the first character in the string that is \p C, or npos if not
168  /// found. Same as find.
169  size_t find_first_of(char C, size_t From = 0) const {
170  return str().find_first_of(C, From);
171  }
172 
173  /// Find the first character in the string that is in \p Chars, or npos if
174  /// not found.
175  ///
176  /// Complexity: O(size() + Chars.size())
177  size_t find_first_of(StringRef Chars, size_t From = 0) const {
178  return str().find_first_of(Chars, From);
179  }
180 
181  /// Find the first character in the string that is not \p C or npos if not
182  /// found.
183  size_t find_first_not_of(char C, size_t From = 0) const {
184  return str().find_first_not_of(C, From);
185  }
186 
187  /// Find the first character in the string that is not in the string
188  /// \p Chars, or npos if not found.
189  ///
190  /// Complexity: O(size() + Chars.size())
191  size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
192  return str().find_first_not_of(Chars, From);
193  }
194 
195  /// Find the last character in the string that is \p C, or npos if not
196  /// found.
197  size_t find_last_of(char C, size_t From = StringRef::npos) const {
198  return str().find_last_of(C, From);
199  }
200 
201  /// Find the last character in the string that is in \p C, or npos if not
202  /// found.
203  ///
204  /// Complexity: O(size() + Chars.size())
205  size_t find_last_of(
206  StringRef Chars, size_t From = StringRef::npos) const {
207  return str().find_last_of(Chars, From);
208  }
209 
210  /// @}
211  /// @name Helpful Algorithms
212  /// @{
213 
214  /// Return the number of occurrences of \p C in the string.
215  size_t count(char C) const {
216  return str().count(C);
217  }
218 
219  /// Return the number of non-overlapped occurrences of \p Str in the
220  /// string.
221  size_t count(StringRef Str) const {
222  return str().count(Str);
223  }
224 
225  /// @}
226  /// @name Substring Operations
227  /// @{
228 
229  /// Return a reference to the substring from [Start, Start + N).
230  ///
231  /// \param Start The index of the starting character in the substring; if
232  /// the index is npos or greater than the length of the string then the
233  /// empty substring will be returned.
234  ///
235  /// \param N The number of characters to included in the substring. If \p N
236  /// exceeds the number of characters remaining in the string, the string
237  /// suffix (starting with \p Start) will be returned.
238  StringRef substr(size_t Start, size_t N = StringRef::npos) const {
239  return str().substr(Start, N);
240  }
241 
242  /// Return a reference to the substring from [Start, End).
243  ///
244  /// \param Start The index of the starting character in the substring; if
245  /// the index is npos or greater than the length of the string then the
246  /// empty substring will be returned.
247  ///
248  /// \param End The index following the last character to include in the
249  /// substring. If this is npos, or less than \p Start, or exceeds the
250  /// number of characters remaining in the string, the string suffix
251  /// (starting with \p Start) will be returned.
252  StringRef slice(size_t Start, size_t End) const {
253  return str().slice(Start, End);
254  }
255 
256  // Extra methods.
257 
258  /// Explicit conversion to StringRef.
259  StringRef str() const { return StringRef(this->data(), this->size()); }
260 
261  // TODO: Make this const, if it's safe...
262  const char* c_str() {
263  this->push_back(0);
264  this->pop_back();
265  return this->data();
266  }
267 
268  /// Implicit conversion to StringRef.
269  operator StringRef() const { return str(); }
270 
271  explicit operator std::string() const {
272  return std::string(this->data(), this->size());
273  }
274 
275  // Extra operators.
277  this->assign(RHS);
278  return *this;
279  }
280 
282  this->append(RHS.begin(), RHS.end());
283  return *this;
284  }
286  this->push_back(C);
287  return *this;
288  }
289 };
290 
291 } // end namespace llvm
292 
293 #endif // LLVM_ADT_SMALLSTRING_H
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:285
llvm::SmallString::find_last_of
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:197
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::SmallString::find
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:147
llvm::SmallString::slice
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:252
llvm::SmallString::endswith
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:127
llvm::SmallString::find
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:139
llvm::StringRef::count
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:488
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::StringRef::endswith
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:296
llvm::StringRef::rfind
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:374
StringRef.h
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:59
llvm::StringRef::find
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:314
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1177
llvm::SmallString::substr
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition: SmallString.h:238
llvm::SmallString::append
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition: SmallString.h:72
llvm::SmallString::assign
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:50
llvm::SmallString::operator+=
SmallString & operator+=(char C)
Definition: SmallString.h:285
llvm::SmallString::find_first_of
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:169
llvm::SmallString::compare_insensitive
int compare_insensitive(StringRef RHS) const
compare_insensitive - Compare two strings, ignoring case.
Definition: SmallString.h:107
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:236
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:227
llvm::SmallString::find_first_not_of
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:183
RHS
Value * RHS
Definition: X86PartialReduction.cpp:74
llvm::StringRef::slice
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:731
llvm::SmallString::operator+=
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:281
llvm::SmallString::SmallString
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:31
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:610
llvm::SmallString::rfind
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition: SmallString.h:155
llvm::SmallString::SmallString
SmallString(std::initializer_list< StringRef > Refs)
Initialize by concatenating a list of StringRefs.
Definition: SmallString.h:34
llvm::StringRef::find_last_of
LLVM_NODISCARD 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:435
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::SmallVectorImpl::append
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:657
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
ItTy
llvm::SmallString::find_first_of
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:177
llvm::SmallString::SmallString
SmallString()=default
Default ctor - Initialize to empty.
llvm::SmallString::count
size_t count(StringRef Str) const
Return the number of non-overlapped occurrences of Str in the string.
Definition: SmallString.h:221
llvm::SmallString::compare
int compare(StringRef RHS) const
Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less than,...
Definition: SmallString.h:102
llvm::StringRef::equals
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:186
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
llvm::SmallVectorImpl< char >::resize_for_overwrite
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:612
llvm::SmallString::append
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:67
llvm::SmallString::c_str
const char * c_str()
Definition: SmallString.h:262
llvm::SmallString::count
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:215
llvm::StringRef::equals_insensitive
LLVM_NODISCARD bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: StringRef.h:193
llvm::SmallString::assign
void assign(std::initializer_list< StringRef > Refs)
Assign from a list of StringRefs.
Definition: SmallString.h:55
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef::find_first_not_of
LLVM_NODISCARD 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:241
llvm::StringRef::compare_numeric
LLVM_NODISCARD int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:61
llvm::SmallString::rfind
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
Definition: SmallString.h:163
llvm::size
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:1630
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::SmallString::SmallString
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:41
llvm::SmallString::operator=
SmallString & operator=(StringRef RHS)
Definition: SmallString.h:276
llvm::SmallVectorImpl::assign
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:678
llvm::SmallString::equals_insensitive
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:96
llvm::SmallString::find_last_of
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:205
llvm::SmallVectorImpl< char >::clear
void clear()
Definition: SmallVector.h:581
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:259
llvm::SmallString::equals
bool equals(StringRef RHS) const
Check for string equality.
Definition: SmallString.h:91
SmallVector.h
llvm::SmallString::compare_numeric
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: SmallString.h:113
N
#define N
llvm::StringRef::compare_insensitive
LLVM_NODISCARD int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
Definition: StringRef.cpp:37
llvm::SmallString::startswith
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:122
llvm::SmallString::find_first_not_of
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:191
llvm::StringRef::find_first_of
LLVM_NODISCARD 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:409
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:127
copy
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same we currently get code like const It could be done with a smaller encoding like local tee $pop5 local copy
Definition: README.txt:101
llvm::StringRef::compare
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:200