WPILibC++  2018.4.1-20180728210220-1136-g75a6720
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SmallString.h
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 WPIUTIL_WPI_SMALLSTRING_H
15 #define WPIUTIL_WPI_SMALLSTRING_H
16 
17 #include "wpi/SmallVector.h"
18 #include "wpi/StringRef.h"
19 #include <cstddef>
20 
21 namespace wpi {
22 
25 template<unsigned InternalLen>
26 class SmallString : public SmallVector<char, InternalLen> {
27 public:
29  SmallString() = default;
30 
32  SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
33 
35  template<typename ItTy>
36  SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
37 
38  // Note that in order to add new overloads for append & assign, we have to
39  // duplicate the inherited versions so as not to inadvertently hide them.
40 
44 
46  void assign(size_t NumElts, char Elt) {
47  this->SmallVectorImpl<char>::assign(NumElts, Elt);
48  }
49 
51  template<typename in_iter>
52  void assign(in_iter S, in_iter E) {
53  this->clear();
55  }
56 
58  void assign(StringRef RHS) {
59  this->clear();
60  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
61  }
62 
64  void assign(const SmallVectorImpl<char> &RHS) {
65  this->clear();
66  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
67  }
68 
72 
74  template<typename in_iter>
75  void append(in_iter S, in_iter E) {
77  }
78 
79  void append(size_t NumInputs, char Elt) {
80  SmallVectorImpl<char>::append(NumInputs, Elt);
81  }
82 
84  void append(StringRef RHS) {
85  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
86  }
87 
89  void append(const SmallVectorImpl<char> &RHS) {
90  SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
91  }
92 
96 
99  bool equals(StringRef RHS) const {
100  return str().equals(RHS);
101  }
102 
104  bool equals_lower(StringRef RHS) const {
105  return str().equals_lower(RHS);
106  }
107 
110  int compare(StringRef RHS) const {
111  return str().compare(RHS);
112  }
113 
115  int compare_lower(StringRef RHS) const {
116  return str().compare_lower(RHS);
117  }
118 
121  int compare_numeric(StringRef RHS) const {
122  return str().compare_numeric(RHS);
123  }
124 
128 
130  bool startswith(StringRef Prefix) const {
131  return str().startswith(Prefix);
132  }
133 
135  bool endswith(StringRef Suffix) const {
136  return str().endswith(Suffix);
137  }
138 
142 
147  size_t find(char C, size_t From = 0) const {
148  return str().find(C, From);
149  }
150 
155  size_t find(StringRef Str, size_t From = 0) const {
156  return str().find(Str, From);
157  }
158 
163  size_t rfind(char C, size_t From = StringRef::npos) const {
164  return str().rfind(C, From);
165  }
166 
171  size_t rfind(StringRef Str) const {
172  return str().rfind(Str);
173  }
174 
177  size_t find_first_of(char C, size_t From = 0) const {
178  return str().find_first_of(C, From);
179  }
180 
185  size_t find_first_of(StringRef Chars, size_t From = 0) const {
186  return str().find_first_of(Chars, From);
187  }
188 
191  size_t find_first_not_of(char C, size_t From = 0) const {
192  return str().find_first_not_of(C, From);
193  }
194 
199  size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
200  return str().find_first_not_of(Chars, From);
201  }
202 
205  size_t find_last_of(char C, size_t From = StringRef::npos) const {
206  return str().find_last_of(C, From);
207  }
208 
213  size_t find_last_of(
214  StringRef Chars, size_t From = StringRef::npos) const {
215  return str().find_last_of(Chars, From);
216  }
217 
221 
223  size_t count(char C) const {
224  return str().count(C);
225  }
226 
229  size_t count(StringRef Str) const {
230  return str().count(Str);
231  }
232 
236 
246  StringRef substr(size_t Start, size_t N = StringRef::npos) const {
247  return str().substr(Start, N);
248  }
249 
260  StringRef slice(size_t Start, size_t End) const {
261  return str().slice(Start, End);
262  }
263 
264  // Extra methods.
265 
267  StringRef str() const { return StringRef(this->begin(), this->size()); }
268 
269  // TODO: Make this const, if it's safe...
270  const char* c_str() {
271  this->push_back(0);
272  this->pop_back();
273  return this->data();
274  }
275 
277  operator StringRef() const { return str(); }
278 
279  // Extra operators.
280  const SmallString &operator=(StringRef RHS) {
281  this->clear();
282  return *this += RHS;
283  }
284 
285  SmallString &operator+=(StringRef RHS) {
286  this->append(RHS.begin(), RHS.end());
287  return *this;
288  }
289  SmallString &operator+=(char C) {
290  this->push_back(C);
291  return *this;
292  }
293 };
294 
295 } // end namespace wpi
296 
297 #endif // LLVM_ADT_SMALLSTRING_H
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:185
StringRef substr(size_t Start, size_t N=StringRef::npos) const
Return a reference to the substring from [Start, Start + N).
Definition: SmallString.h:246
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const noexcept
Check if this string ends with the given Suffix.
Definition: StringRef.h:257
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:177
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const noexcept
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:399
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const noexcept
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: hostname.h:17
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:868
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const noexcept
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:373
LLVM_NODISCARD bool equals_lower(StringRef RHS) const noexcept
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:176
size_t rfind(StringRef Str) const
Search for the last string Str in the string.
Definition: SmallString.h:171
size_t find(StringRef Str, size_t From=0) const
Search for the first string Str in the string.
Definition: SmallString.h:155
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:58
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const noexcept
Search for the first character C in the string.
Definition: StringRef.h:276
void assign(size_t NumElts, char Elt)
Assign from a repeated element.
Definition: SmallString.h:46
LLVM_NODISCARD int compare_lower(StringRef RHS) const noexcept
compare_lower - Compare two strings, ignoring case.
size_t find(char C, size_t From=0) const
find - Search for the first character C in the string.
Definition: SmallString.h:147
bool equals(StringRef RHS) const
Check for string equality.
Definition: SmallString.h:99
SmallString(StringRef S)
Initialize from a StringRef.
Definition: SmallString.h:32
namespace to hold default to_json function
Definition: SmallString.h:21
void append(const SmallVectorImpl< char > &RHS)
Append from a SmallVector.
Definition: SmallString.h:89
void assign(const SmallVectorImpl< char > &RHS)
Assign from a SmallVector.
Definition: SmallString.h:64
SmallString(ItTy S, ItTy E)
Initialize with a range.
Definition: SmallString.h:36
size_t rfind(char C, size_t From=StringRef::npos) const
Search for the last character C in the string.
Definition: SmallString.h:163
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE int compare(StringRef RHS) const noexcept
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:184
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:110
size_t count(StringRef Str) const
Return the number of non-overlapped occurrences of Str in the string.
Definition: SmallString.h:229
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:205
LLVM_NODISCARD size_t count(char C) const noexcept
Return the number of occurrences of C in the string.
Definition: StringRef.h:454
bool equals_lower(StringRef RHS) const
Check for string equality, ignoring case.
Definition: SmallString.h:104
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const noexcept
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const noexcept
Search for the last character C in the string.
Definition: StringRef.h:338
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: SmallString.h:223
bool startswith(StringRef Prefix) const
startswith - Check if this string starts with the given Prefix.
Definition: SmallString.h:130
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: SmallString.h:260
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:199
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const noexcept
Return a reference to the substring from [Start, End).
Definition: StringRef.h:668
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
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:213
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const noexcept
Find the first character in the string that is not C or npos if not found.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:402
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: SmallString.h:115
void assign(in_iter S, in_iter E)
Assign from an iterator pair.
Definition: SmallString.h:52
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:191
bool endswith(StringRef Suffix) const
endswith - Check if this string ends with the given Suffix.
Definition: SmallString.h:135
LLVM_NODISCARD int compare_numeric(StringRef RHS) const noexcept
compare_numeric - Compare two strings, treating sequences of digits as numbers.
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: SmallString.h:121
SmallString()=default
Default ctor - Initialize to empty.
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:151
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:84
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const noexcept
Check if this string starts with the given Prefix.
Definition: StringRef.h:245