LLVM  16.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 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include <cstddef>
20 
21 namespace 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).
25 template<unsigned InternalLen>
26 class SmallString : public SmallVector<char, InternalLen> {
27 public:
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 
49 
50  /// Assign from a StringRef.
52  SmallVectorImpl<char>::assign(RHS.begin(), RHS.end());
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 
66 
67  /// Append from a StringRef.
69  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
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 two strings; the result is -1, 0, or 1 if this string is
102  /// lexicographically less than, equal to, or greater than the \p RHS.
103  int compare(StringRef RHS) const {
104  return str().compare(RHS);
105  }
106 
107  /// compare_insensitive - Compare two strings, ignoring case.
109  return str().compare_insensitive(RHS);
110  }
111 
112  /// compare_numeric - Compare two strings, treating sequences of digits as
113  /// numbers.
115  return str().compare_numeric(RHS);
116  }
117 
118  /// @}
119  /// @name String Predicates
120  /// @{
121 
122  /// startswith - Check if this string starts with the given \p Prefix.
124  return str().startswith(Prefix);
125  }
126 
127  /// endswith - Check if this string ends with the given \p Suffix.
128  bool endswith(StringRef Suffix) const {
129  return str().endswith(Suffix);
130  }
131 
132  /// @}
133  /// @name String Searching
134  /// @{
135 
136  /// find - Search for the first character \p C in the string.
137  ///
138  /// \return - The index of the first occurrence of \p C, or npos if not
139  /// found.
140  size_t find(char C, size_t From = 0) const {
141  return str().find(C, From);
142  }
143 
144  /// Search for the first string \p Str in the string.
145  ///
146  /// \returns The index of the first occurrence of \p Str, or npos if not
147  /// found.
148  size_t find(StringRef Str, size_t From = 0) const {
149  return str().find(Str, From);
150  }
151 
152  /// Search for the last character \p C in the string.
153  ///
154  /// \returns The index of the last occurrence of \p C, or npos if not
155  /// found.
156  size_t rfind(char C, size_t From = StringRef::npos) const {
157  return str().rfind(C, From);
158  }
159 
160  /// Search for the last string \p Str in the string.
161  ///
162  /// \returns The index of the last occurrence of \p Str, or npos if not
163  /// found.
164  size_t rfind(StringRef Str) const {
165  return str().rfind(Str);
166  }
167 
168  /// Find the first character in the string that is \p C, or npos if not
169  /// found. Same as find.
170  size_t find_first_of(char C, size_t From = 0) const {
171  return str().find_first_of(C, From);
172  }
173 
174  /// Find the first character in the string that is in \p Chars, or npos if
175  /// not found.
176  ///
177  /// Complexity: O(size() + Chars.size())
178  size_t find_first_of(StringRef Chars, size_t From = 0) const {
179  return str().find_first_of(Chars, From);
180  }
181 
182  /// Find the first character in the string that is not \p C or npos if not
183  /// found.
184  size_t find_first_not_of(char C, size_t From = 0) const {
185  return str().find_first_not_of(C, From);
186  }
187 
188  /// Find the first character in the string that is not in the string
189  /// \p Chars, or npos if not found.
190  ///
191  /// Complexity: O(size() + Chars.size())
192  size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
193  return str().find_first_not_of(Chars, From);
194  }
195 
196  /// Find the last character in the string that is \p C, or npos if not
197  /// found.
198  size_t find_last_of(char C, size_t From = StringRef::npos) const {
199  return str().find_last_of(C, From);
200  }
201 
202  /// Find the last character in the string that is in \p C, or npos if not
203  /// found.
204  ///
205  /// Complexity: O(size() + Chars.size())
206  size_t find_last_of(
207  StringRef Chars, size_t From = StringRef::npos) const {
208  return str().find_last_of(Chars, From);
209  }
210 
211  /// @}
212  /// @name Helpful Algorithms
213  /// @{
214 
215  /// Return the number of occurrences of \p C in the string.
216  size_t count(char C) const {
217  return str().count(C);
218  }
219 
220  /// Return the number of non-overlapped occurrences of \p Str in the
221  /// string.
222  size_t count(StringRef Str) const {
223  return str().count(Str);
224  }
225 
226  /// @}
227  /// @name Substring Operations
228  /// @{
229 
230  /// Return a reference to the substring from [Start, Start + N).
231  ///
232  /// \param Start The index of the starting character in the substring; if
233  /// the index is npos or greater than the length of the string then the
234  /// empty substring will be returned.
235  ///
236  /// \param N The number of characters to included in the substring. If \p N
237  /// exceeds the number of characters remaining in the string, the string
238  /// suffix (starting with \p Start) will be returned.
239  StringRef substr(size_t Start, size_t N = StringRef::npos) const {
240  return str().substr(Start, N);
241  }
242 
243  /// Return a reference to the substring from [Start, End).
244  ///
245  /// \param Start The index of the starting character in the substring; if
246  /// the index is npos or greater than the length of the string then the
247  /// empty substring will be returned.
248  ///
249  /// \param End The index following the last character to include in the
250  /// substring. If this is npos, or less than \p Start, or exceeds the
251  /// number of characters remaining in the string, the string suffix
252  /// (starting with \p Start) will be returned.
253  StringRef slice(size_t Start, size_t End) const {
254  return str().slice(Start, End);
255  }
256 
257  // Extra methods.
258 
259  /// Explicit conversion to StringRef.
260  StringRef str() const { return StringRef(this->data(), this->size()); }
261 
262  // TODO: Make this const, if it's safe...
263  const char* c_str() {
264  this->push_back(0);
265  this->pop_back();
266  return this->data();
267  }
268 
269  /// Implicit conversion to StringRef.
270  operator StringRef() const { return str(); }
271 
272  explicit operator std::string() const {
273  return std::string(this->data(), this->size());
274  }
275 
276  // Extra operators.
278  this->assign(RHS);
279  return *this;
280  }
281 
283  this->append(RHS.begin(), RHS.end());
284  return *this;
285  }
287  this->push_back(C);
288  return *this;
289  }
290 };
291 
292 } // end namespace llvm
293 
294 #endif // LLVM_ADT_SMALLSTRING_H
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:198
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:148
llvm::SmallString::slice
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:253
llvm::SmallString::endswith
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:128
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:140
llvm::StringRef::rfind
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:337
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:160
llvm::StringRef::compare
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:176
StringRef.h
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:52
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
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:239
llvm::SmallString::append
void append(std::initializer_list< StringRef > Refs)
Append from a list of StringRefs.
Definition: SmallString.h:73
llvm::SmallString::assign
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:51
llvm::SmallString::operator+=
SmallString & operator+=(char C)
Definition: SmallString.h:286
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:170
llvm::StringRef::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: StringRef.h:368
llvm::SmallString::compare_insensitive
int compare_insensitive(StringRef RHS) const
compare_insensitive - Compare two strings, ignoring case.
Definition: SmallString.h:108
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
llvm::StringRef::substr
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:561
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:184
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::SmallString::operator+=
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:282
llvm::SmallString::SmallString
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:32
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:156
llvm::SmallString::SmallString
SmallString(std::initializer_list< StringRef > Refs)
Initialize by concatenating a list of StringRefs.
Definition: SmallString.h:35
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:178
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:669
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:222
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:103
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
llvm::StringRef::slice
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:673
llvm::StringRef::equals
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:164
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:627
llvm::SmallString::append
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
llvm::SmallString::c_str
const char * c_str()
Definition: SmallString.h:263
llvm::SmallString::count
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:216
llvm::SmallString::assign
void assign(std::initializer_list< StringRef > Refs)
Assign from a list of StringRefs.
Definition: SmallString.h:56
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef::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: StringRef.cpp:248
llvm::StringRef::compare_numeric
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:164
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:1571
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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::StringRef::find_last_of
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:391
llvm::StringRef::count
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:442
llvm::SmallString::SmallString
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:42
llvm::SmallString::operator=
SmallString & operator=(StringRef RHS)
Definition: SmallString.h:277
llvm::SmallVectorImpl::assign
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:690
llvm::SmallString::equals_insensitive
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:97
llvm::StringRef::endswith
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:265
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:206
llvm::SmallVectorImpl< char >::clear
void clear()
Definition: SmallVector.h:596
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:260
llvm::SmallString::equals
bool equals(StringRef RHS) const
Check for string equality.
Definition: SmallString.h:92
llvm::StringRef::equals_insensitive
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition: StringRef.h:170
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:114
N
#define N
llvm::StringRef::compare_insensitive
int compare_insensitive(StringRef RHS) const
Compare two strings, ignoring case.
Definition: StringRef.cpp:37
llvm::StringRef::find
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:281
llvm::SmallString::startswith
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:123
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:192
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:111
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