LLVM  10.0.0svn
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 with a range.
34  template<typename ItTy>
35  SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
36 
37  // Note that in order to add new overloads for append & assign, we have to
38  // duplicate the inherited versions so as not to inadvertently hide them.
39 
40  /// @}
41  /// @name String Assignment
42  /// @{
43 
44  /// Assign from a repeated element.
45  void assign(size_t NumElts, char Elt) {
46  this->SmallVectorImpl<char>::assign(NumElts, Elt);
47  }
48 
49  /// Assign from an iterator pair.
50  template<typename in_iter>
51  void assign(in_iter S, in_iter E) {
52  this->clear();
54  }
55 
56  /// Assign from a StringRef.
57  void assign(StringRef RHS) {
58  this->clear();
60  }
61 
62  /// Assign from a SmallVector.
63  void assign(const SmallVectorImpl<char> &RHS) {
64  this->clear();
66  }
67 
68  /// @}
69  /// @name String Concatenation
70  /// @{
71 
72  /// Append from an iterator pair.
73  template<typename in_iter>
74  void append(in_iter S, in_iter E) {
76  }
77 
78  void append(size_t NumInputs, char Elt) {
79  SmallVectorImpl<char>::append(NumInputs, Elt);
80  }
81 
82  /// Append from a StringRef.
83  void append(StringRef RHS) {
85  }
86 
87  /// Append from a SmallVector.
88  void append(const SmallVectorImpl<char> &RHS) {
90  }
91 
92  /// @}
93  /// @name String Comparison
94  /// @{
95 
96  /// Check for string equality. This is more efficient than compare() when
97  /// the relative ordering of inequal strings isn't needed.
98  bool equals(StringRef RHS) const {
99  return str().equals(RHS);
100  }
101 
102  /// Check for string equality, ignoring case.
103  bool equals_lower(StringRef RHS) const {
104  return str().equals_lower(RHS);
105  }
106 
107  /// Compare two strings; the result is -1, 0, or 1 if this string is
108  /// lexicographically less than, equal to, or greater than the \p RHS.
109  int compare(StringRef RHS) const {
110  return str().compare(RHS);
111  }
112 
113  /// compare_lower - Compare two strings, ignoring case.
114  int compare_lower(StringRef RHS) const {
115  return str().compare_lower(RHS);
116  }
117 
118  /// compare_numeric - Compare two strings, treating sequences of digits as
119  /// numbers.
120  int compare_numeric(StringRef RHS) const {
121  return str().compare_numeric(RHS);
122  }
123 
124  /// @}
125  /// @name String Predicates
126  /// @{
127 
128  /// startswith - Check if this string starts with the given \p Prefix.
130  return str().startswith(Prefix);
131  }
132 
133  /// endswith - Check if this string ends with the given \p Suffix.
134  bool endswith(StringRef Suffix) const {
135  return str().endswith(Suffix);
136  }
137 
138  /// @}
139  /// @name String Searching
140  /// @{
141 
142  /// find - Search for the first character \p C in the string.
143  ///
144  /// \return - The index of the first occurrence of \p C, or npos if not
145  /// found.
146  size_t find(char C, size_t From = 0) const {
147  return str().find(C, From);
148  }
149 
150  /// Search for the first string \p Str in the string.
151  ///
152  /// \returns The index of the first occurrence of \p Str, or npos if not
153  /// found.
154  size_t find(StringRef Str, size_t From = 0) const {
155  return str().find(Str, From);
156  }
157 
158  /// Search for the last character \p C in the string.
159  ///
160  /// \returns The index of the last occurrence of \p C, or npos if not
161  /// found.
162  size_t rfind(char C, size_t From = StringRef::npos) const {
163  return str().rfind(C, From);
164  }
165 
166  /// Search for the last string \p Str in the string.
167  ///
168  /// \returns The index of the last occurrence of \p Str, or npos if not
169  /// found.
170  size_t rfind(StringRef Str) const {
171  return str().rfind(Str);
172  }
173 
174  /// Find the first character in the string that is \p C, or npos if not
175  /// found. Same as find.
176  size_t find_first_of(char C, size_t From = 0) const {
177  return str().find_first_of(C, From);
178  }
179 
180  /// Find the first character in the string that is in \p Chars, or npos if
181  /// not found.
182  ///
183  /// Complexity: O(size() + Chars.size())
184  size_t find_first_of(StringRef Chars, size_t From = 0) const {
185  return str().find_first_of(Chars, From);
186  }
187 
188  /// Find the first character in the string that is not \p C or npos if not
189  /// found.
190  size_t find_first_not_of(char C, size_t From = 0) const {
191  return str().find_first_not_of(C, From);
192  }
193 
194  /// Find the first character in the string that is not in the string
195  /// \p Chars, or npos if not found.
196  ///
197  /// Complexity: O(size() + Chars.size())
198  size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
199  return str().find_first_not_of(Chars, From);
200  }
201 
202  /// Find the last character in the string that is \p C, or npos if not
203  /// found.
204  size_t find_last_of(char C, size_t From = StringRef::npos) const {
205  return str().find_last_of(C, From);
206  }
207 
208  /// Find the last character in the string that is in \p C, or npos if not
209  /// found.
210  ///
211  /// Complexity: O(size() + Chars.size())
212  size_t find_last_of(
213  StringRef Chars, size_t From = StringRef::npos) const {
214  return str().find_last_of(Chars, From);
215  }
216 
217  /// @}
218  /// @name Helpful Algorithms
219  /// @{
220 
221  /// Return the number of occurrences of \p C in the string.
222  size_t count(char C) const {
223  return str().count(C);
224  }
225 
226  /// Return the number of non-overlapped occurrences of \p Str in the
227  /// string.
228  size_t count(StringRef Str) const {
229  return str().count(Str);
230  }
231 
232  /// @}
233  /// @name Substring Operations
234  /// @{
235 
236  /// Return a reference to the substring from [Start, Start + N).
237  ///
238  /// \param Start The index of the starting character in the substring; if
239  /// the index is npos or greater than the length of the string then the
240  /// empty substring will be returned.
241  ///
242  /// \param N The number of characters to included in the substring. If \p N
243  /// exceeds the number of characters remaining in the string, the string
244  /// suffix (starting with \p Start) will be returned.
245  StringRef substr(size_t Start, size_t N = StringRef::npos) const {
246  return str().substr(Start, N);
247  }
248 
249  /// Return a reference to the substring from [Start, End).
250  ///
251  /// \param Start The index of the starting character in the substring; if
252  /// the index is npos or greater than the length of the string then the
253  /// empty substring will be returned.
254  ///
255  /// \param End The index following the last character to include in the
256  /// substring. If this is npos, or less than \p Start, or exceeds the
257  /// number of characters remaining in the string, the string suffix
258  /// (starting with \p Start) will be returned.
259  StringRef slice(size_t Start, size_t End) const {
260  return str().slice(Start, End);
261  }
262 
263  // Extra methods.
264 
265  /// Explicit conversion to StringRef.
266  StringRef str() const { return StringRef(this->begin(), this->size()); }
267 
268  // TODO: Make this const, if it's safe...
269  const char* c_str() {
270  this->push_back(0);
271  this->pop_back();
272  return this->data();
273  }
274 
275  /// Implicit conversion to StringRef.
276  operator StringRef() const { return str(); }
277 
278  // Extra operators.
280  this->clear();
281  return *this += RHS;
282  }
283 
285  this->append(RHS.begin(), RHS.end());
286  return *this;
287  }
289  this->push_back(C);
290  return *this;
291  }
292 };
293 
294 } // end namespace llvm
295 
296 #endif // LLVM_ADT_SMALLSTRING_H
uint64_t CallInst * C
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:281
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:222
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:57
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:181
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: SmallString.h:114
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:35
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:359
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:146
void push_back(const T &Elt)
Definition: SmallVector.h:211
void append(const SmallVectorImpl< char > &RHS)
Append from a SmallVector.
Definition: SmallString.h:88
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:188
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:420
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: SmallString.h:120
SmallString & operator+=(char C)
Definition: SmallString.h:288
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:190
void append(size_t NumInputs, char Elt)
Definition: SmallString.h:78
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition: SmallString.h:162
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:176
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:284
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:109
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:471
void assign(const SmallVectorImpl< char > &RHS)
Assign from a SmallVector.
Definition: SmallString.h:63
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:693
bool equals_lower(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:103
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:592
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:134
void assign(size_type NumElts, const T &Elt)
Definition: SmallVector.h:412
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:259
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:31
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
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:204
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:154
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:74
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:83
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:249
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:299
void assign(size_t NumElts, char Elt)
Assign from a repeated element.
Definition: SmallString.h:45
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:198
bool equals(StringRef RHS) const
Check for string equality.
Definition: SmallString.h:98
size_t size() const
Definition: SmallVector.h:52
LLVM_NODISCARD int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:37
BlockVerifier::State From
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
SmallString()=default
Default ctor - Initialize to empty.
iterator begin() const
Definition: StringRef.h:115
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
LLVM_NODISCARD int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:63
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:174
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:129
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
static const size_t npos
Definition: StringRef.h:50
const char * c_str()
Definition: SmallString.h:269
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:394
#define N
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition: SmallString.h:245
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:212
size_t count(StringRef Str) const
Return the number of non-overlapped occurrences of Str in the string.
Definition: SmallString.h:228
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:184
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
const SmallString & operator=(StringRef RHS)
Definition: SmallString.h:279
iterator end() const
Definition: StringRef.h:117
void assign(in_iter S, in_iter E)
Assign from an iterator pair.
Definition: SmallString.h:51
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
Definition: SmallString.h:170