SmallString.h revision 2d9eb72178af8e79dc6432cd1b7d29bde16da1b9
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
20namespace 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).
24template<unsigned InternalLen>
25class SmallString : public SmallVector<char, InternalLen> {
26public:
27  /// Default ctor - Initialize to empty.
28  SmallString() {}
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();
56    SmallVectorImpl<char>::append(S, E);
57  }
58
59  /// Assign from a StringRef.
60  void assign(StringRef RHS) {
61    this->clear();
62    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
63  }
64
65  /// Assign from a SmallVector.
66  void assign(const SmallVectorImpl<char> &RHS) {
67    this->clear();
68    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
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) {
78    SmallVectorImpl<char>::append(S, 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) {
88    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
89  }
90
91  /// Append from a SmallVector.
92  void append(const SmallVectorImpl<char> &RHS) {
93    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
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.
133  bool startswith(StringRef Prefix) const {
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.
283  const SmallString &operator=(StringRef RHS) {
284    this->clear();
285    return *this += RHS;
286  }
287
288  SmallString &operator+=(StringRef RHS) {
289    this->append(RHS.begin(), RHS.end());
290    return *this;
291  }
292  SmallString &operator+=(char C) {
293    this->push_back(C);
294    return *this;
295  }
296};
297
298}
299
300#endif
301