1 //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SmallString class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLSTRING_H
15 #define LLVM_ADT_SMALLSTRING_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
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> {
27 /// Default ctor - Initialize to empty.
30 /// Initialize from a StringRef.
31 SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
33 /// Initialize with a range.
34 template<typename ItTy>
35 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
38 SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
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.
44 /// @name String Assignment
47 /// Assign from a repeated element.
48 void assign(size_t NumElts, char Elt) {
49 this->SmallVectorImpl<char>::assign(NumElts, Elt);
52 /// Assign from an iterator pair.
53 template<typename in_iter>
54 void assign(in_iter S, in_iter E) {
56 SmallVectorImpl<char>::append(S, E);
59 /// Assign from a StringRef.
60 void assign(StringRef RHS) {
62 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
65 /// Assign from a SmallVector.
66 void assign(const SmallVectorImpl<char> &RHS) {
68 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
72 /// @name String Concatenation
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);
81 void append(size_t NumInputs, char Elt) {
82 SmallVectorImpl<char>::append(NumInputs, Elt);
86 /// Append from a StringRef.
87 void append(StringRef RHS) {
88 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
91 /// Append from a SmallVector.
92 void append(const SmallVectorImpl<char> &RHS) {
93 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
97 /// @name String Comparison
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);
106 /// Check for string equality, ignoring case.
107 bool equals_lower(StringRef RHS) const {
108 return str().equals_lower(RHS);
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);
117 /// compare_lower - Compare two strings, ignoring case.
118 int compare_lower(StringRef RHS) const {
119 return str().compare_lower(RHS);
122 /// compare_numeric - Compare two strings, treating sequences of digits as
124 int compare_numeric(StringRef RHS) const {
125 return str().compare_numeric(RHS);
129 /// @name String Predicates
132 /// startswith - Check if this string starts with the given \p Prefix.
133 bool startswith(StringRef Prefix) const {
134 return str().startswith(Prefix);
137 /// endswith - Check if this string ends with the given \p Suffix.
138 bool endswith(StringRef Suffix) const {
139 return str().endswith(Suffix);
143 /// @name String Searching
146 /// find - Search for the first character \p C in the string.
148 /// \return - The index of the first occurrence of \p C, or npos if not
150 size_t find(char C, size_t From = 0) const {
151 return str().find(C, From);
154 /// Search for the first string \p Str in the string.
156 /// \returns The index of the first occurrence of \p Str, or npos if not
158 size_t find(StringRef Str, size_t From = 0) const {
159 return str().find(Str, From);
162 /// Search for the last character \p C in the string.
164 /// \returns The index of the last occurrence of \p C, or npos if not
166 size_t rfind(char C, size_t From = StringRef::npos) const {
167 return str().rfind(C, From);
170 /// Search for the last string \p Str in the string.
172 /// \returns The index of the last occurrence of \p Str, or npos if not
174 size_t rfind(StringRef Str) const {
175 return str().rfind(Str);
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);
184 /// Find the first character in the string that is in \p Chars, or npos if
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);
192 /// Find the first character in the string that is not \p C or npos if not
194 size_t find_first_not_of(char C, size_t From = 0) const {
195 return str().find_first_not_of(C, From);
198 /// Find the first character in the string that is not in the string
199 /// \p Chars, or npos if not found.
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);
206 /// Find the last character in the string that is \p C, or npos if not
208 size_t find_last_of(char C, size_t From = StringRef::npos) const {
209 return str().find_last_of(C, From);
212 /// Find the last character in the string that is in \p C, or npos if not
215 /// Complexity: O(size() + Chars.size())
217 StringRef Chars, size_t From = StringRef::npos) const {
218 return str().find_last_of(Chars, From);
222 /// @name Helpful Algorithms
225 /// Return the number of occurrences of \p C in the string.
226 size_t count(char C) const {
227 return str().count(C);
230 /// Return the number of non-overlapped occurrences of \p Str in the
232 size_t count(StringRef Str) const {
233 return str().count(Str);
237 /// @name Substring Operations
240 /// Return a reference to the substring from [Start, Start + N).
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.
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);
253 /// Return a reference to the substring from [Start, End).
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.
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);
269 /// Explicit conversion to StringRef.
270 StringRef str() const { return StringRef(this->begin(), this->size()); }
272 // TODO: Make this const, if it's safe...
273 const char* c_str() {
279 /// Implicit conversion to StringRef.
280 operator StringRef() const { return str(); }
283 const SmallString &operator=(StringRef RHS) {
288 SmallString &operator+=(StringRef RHS) {
289 this->append(RHS.begin(), RHS.end());
292 SmallString &operator+=(char C) {