LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SmallString.h
Go to the documentation of this file.
1 //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 
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.
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  /// Copy ctor.
38  SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
39 
40  // Note that in order to add new overloads for append & assign, we have to
41  // duplicate the inherited versions so as not to inadvertently hide them.
42 
43  /// @}
44  /// @name String Assignment
45  /// @{
46 
47  /// Assign from a repeated element.
48  void assign(size_t NumElts, char Elt) {
49  this->SmallVectorImpl<char>::assign(NumElts, Elt);
50  }
51 
52  /// Assign from an iterator pair.
53  template<typename in_iter>
54  void assign(in_iter S, in_iter E) {
55  this->clear();
57  }
58 
59  /// Assign from a StringRef.
60  void assign(StringRef RHS) {
61  this->clear();
63  }
64 
65  /// Assign from a SmallVector.
66  void assign(const SmallVectorImpl<char> &RHS) {
67  this->clear();
69  }
70 
71  /// @}
72  /// @name String Concatenation
73  /// @{
74 
75  /// Append from an iterator pair.
76  template<typename in_iter>
77  void append(in_iter S, in_iter E) {
79  }
80 
81  void append(size_t NumInputs, char Elt) {
82  SmallVectorImpl<char>::append(NumInputs, Elt);
83  }
84 
85 
86  /// Append from a StringRef.
87  void append(StringRef RHS) {
89  }
90 
91  /// Append from a SmallVector.
92  void append(const SmallVectorImpl<char> &RHS) {
94  }
95 
96  /// @}
97  /// @name String Comparison
98  /// @{
99 
100  /// Check for string equality. This is more efficient than compare() when
101  /// the relative ordering of inequal strings isn't needed.
102  bool equals(StringRef RHS) const {
103  return str().equals(RHS);
104  }
105 
106  /// Check for string equality, ignoring case.
107  bool equals_lower(StringRef RHS) const {
108  return str().equals_lower(RHS);
109  }
110 
111  /// Compare two strings; the result is -1, 0, or 1 if this string is
112  /// lexicographically less than, equal to, or greater than the \p RHS.
113  int compare(StringRef RHS) const {
114  return str().compare(RHS);
115  }
116 
117  /// compare_lower - Compare two strings, ignoring case.
118  int compare_lower(StringRef RHS) const {
119  return str().compare_lower(RHS);
120  }
121 
122  /// compare_numeric - Compare two strings, treating sequences of digits as
123  /// numbers.
124  int compare_numeric(StringRef RHS) const {
125  return str().compare_numeric(RHS);
126  }
127 
128  /// @}
129  /// @name String Predicates
130  /// @{
131 
132  /// startswith - Check if this string starts with the given \p Prefix.
134  return str().startswith(Prefix);
135  }
136 
137  /// endswith - Check if this string ends with the given \p Suffix.
138  bool endswith(StringRef Suffix) const {
139  return str().endswith(Suffix);
140  }
141 
142  /// @}
143  /// @name String Searching
144  /// @{
145 
146  /// find - Search for the first character \p C in the string.
147  ///
148  /// \return - The index of the first occurrence of \p C, or npos if not
149  /// found.
150  size_t find(char C, size_t From = 0) const {
151  return str().find(C, From);
152  }
153 
154  /// Search for the first string \p Str in the string.
155  ///
156  /// \returns The index of the first occurrence of \p Str, or npos if not
157  /// found.
158  size_t find(StringRef Str, size_t From = 0) const {
159  return str().find(Str, From);
160  }
161 
162  /// Search for the last character \p C in the string.
163  ///
164  /// \returns The index of the last occurrence of \p C, or npos if not
165  /// found.
166  size_t rfind(char C, size_t From = StringRef::npos) const {
167  return str().rfind(C, From);
168  }
169 
170  /// Search for the last string \p Str in the string.
171  ///
172  /// \returns The index of the last occurrence of \p Str, or npos if not
173  /// found.
174  size_t rfind(StringRef Str) const {
175  return str().rfind(Str);
176  }
177 
178  /// Find the first character in the string that is \p C, or npos if not
179  /// found. Same as find.
180  size_t find_first_of(char C, size_t From = 0) const {
181  return str().find_first_of(C, From);
182  }
183 
184  /// Find the first character in the string that is in \p Chars, or npos if
185  /// not found.
186  ///
187  /// Complexity: O(size() + Chars.size())
188  size_t find_first_of(StringRef Chars, size_t From = 0) const {
189  return str().find_first_of(Chars, From);
190  }
191 
192  /// Find the first character in the string that is not \p C or npos if not
193  /// found.
194  size_t find_first_not_of(char C, size_t From = 0) const {
195  return str().find_first_not_of(C, From);
196  }
197 
198  /// Find the first character in the string that is not in the string
199  /// \p Chars, or npos if not found.
200  ///
201  /// Complexity: O(size() + Chars.size())
202  size_t find_first_not_of(StringRef Chars, 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  size_t find_last_of(char C, size_t From = StringRef::npos) const {
209  return str().find_last_of(C, From);
210  }
211 
212  /// Find the last character in the string that is in \p C, or npos if not
213  /// found.
214  ///
215  /// Complexity: O(size() + Chars.size())
216  size_t find_last_of(
217  StringRef Chars, size_t From = StringRef::npos) const {
218  return str().find_last_of(Chars, From);
219  }
220 
221  /// @}
222  /// @name Helpful Algorithms
223  /// @{
224 
225  /// Return the number of occurrences of \p C in the string.
226  size_t count(char C) const {
227  return str().count(C);
228  }
229 
230  /// Return the number of non-overlapped occurrences of \p Str in the
231  /// string.
232  size_t count(StringRef Str) const {
233  return str().count(Str);
234  }
235 
236  /// @}
237  /// @name Substring Operations
238  /// @{
239 
240  /// Return a reference to the substring from [Start, Start + N).
241  ///
242  /// \param Start The index of the starting character in the substring; if
243  /// the index is npos or greater than the length of the string then the
244  /// empty substring will be returned.
245  ///
246  /// \param N The number of characters to included in the substring. If \p N
247  /// exceeds the number of characters remaining in the string, the string
248  /// suffix (starting with \p Start) will be returned.
249  StringRef substr(size_t Start, 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  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  StringRef str() const { return StringRef(this->begin(), this->size()); }
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  // Extra operators.
284  this->clear();
285  return *this += RHS;
286  }
287 
289  this->append(RHS.begin(), RHS.end());
290  return *this;
291  }
293  this->push_back(C);
294  return *this;
295  }
296 };
297 
298 }
299 
300 #endif
StringRef slice(size_t Start, size_t End) const
Definition: SmallString.h:263
void push_back(const T &Elt)
Definition: SmallVector.h:236
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:53
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:60
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:35
size_t find(StringRef Str, size_t From=0) const
Definition: SmallString.h:158
size_t find(char C, size_t From=0) const
Definition: StringRef.h:233
void append(const SmallVectorImpl< char > &RHS)
Append from a SmallVector.
Definition: SmallString.h:92
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:217
bool equals_lower(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:107
size_t rfind(char C, size_t From=npos) const
Definition: StringRef.h:250
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
int compare_numeric(StringRef RHS) const
Definition: SmallString.h:124
size_t find_last_of(char C, size_t From=StringRef::npos) const
Definition: SmallString.h:208
SmallString(const SmallString &RHS)
Copy ctor.
Definition: SmallString.h:38
int compare(StringRef RHS) const
Definition: SmallString.h:113
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Definition: SmallString.h:249
SmallString & operator+=(char C)
Definition: SmallString.h:292
void append(size_t NumInputs, char Elt)
Definition: SmallString.h:81
int compare(StringRef RHS) const
Definition: StringRef.h:141
SmallString & operator+=(StringRef RHS)
Definition: SmallString.h:288
size_t rfind(StringRef Str) const
Definition: SmallString.h:174
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:226
size_t find_first_not_of(StringRef Chars, size_t From=0) const
Definition: SmallString.h:202
void assign(unsigned NumElts, const T &Elt)
Definition: SmallVector.h:470
void assign(const SmallVectorImpl< char > &RHS)
Assign from a SmallVector.
Definition: SmallString.h:66
bool equals(StringRef RHS) const
Definition: SmallString.h:102
size_t rfind(char C, size_t From=StringRef::npos) const
Definition: SmallString.h:166
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:31
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: SmallString.h:118
iterator begin() const
Definition: StringRef.h:97
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:77
size_t find_last_of(StringRef Chars, size_t From=StringRef::npos) const
Definition: SmallString.h:216
size_t count(StringRef Str) const
Definition: SmallString.h:232
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:87
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:316
SmallString()
Default ctor - Initialize to empty.
Definition: SmallString.h:28
size_t find(char C, size_t From=0) const
Definition: SmallString.h:150
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:133
void assign(size_t NumElts, char Elt)
Assign from a repeated element.
Definition: SmallString.h:48
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
size_t find_first_not_of(char C, size_t From=0) const
Definition: StringRef.cpp:213
size_t find_last_of(char C, size_t From=npos) const
Definition: StringRef.h:291
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:208
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:138
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
bool equals(StringRef RHS) const
Definition: StringRef.h:129
size_t find_first_of(StringRef Chars, size_t From=0) const
Definition: SmallString.h:188
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:135
static const size_t npos
Definition: StringRef.h:45
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:135
const char * c_str()
Definition: SmallString.h:273
size_t find_first_not_of(char C, size_t From=0) const
Definition: SmallString.h:194
#define N
size_t find_first_of(char C, size_t From=0) const
Definition: StringRef.h:269
int compare_numeric(StringRef RHS) const
compare_numeric - Compare strings, handle embedded numbers.
Definition: StringRef.cpp:74
iterator end() const
Definition: StringRef.h:99
StringRef slice(size_t Start, size_t End) const
Definition: StringRef.h:421
size_t find_first_of(char C, size_t From=0) const
Definition: SmallString.h:180
const SmallString & operator=(StringRef RHS)
Definition: SmallString.h:283
void assign(in_iter S, in_iter E)
Assign from an iterator pair.
Definition: SmallString.h:54