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(unsigned 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 /// Append from a StringRef
82 void append(StringRef RHS) {
83 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
86 /// Append from a SmallVector
87 void append(const SmallVectorImpl<char> &RHS) {
88 SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
92 /// @name String Comparison
95 /// equals - Check for string equality, this is more efficient than
96 /// compare() when the relative ordering of inequal strings isn't needed.
97 bool equals(StringRef RHS) const {
98 return str().equals(RHS);
101 /// equals_lower - Check for string equality, ignoring case.
102 bool equals_lower(StringRef RHS) const {
103 return str().equals_lower(RHS);
106 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
107 /// is lexicographically less than, equal to, or greater than the \arg RHS.
108 int compare(StringRef RHS) const {
109 return str().compare(RHS);
112 /// compare_lower - Compare two strings, ignoring case.
113 int compare_lower(StringRef RHS) const {
114 return str().compare_lower(RHS);
117 /// compare_numeric - Compare two strings, treating sequences of digits as
119 int compare_numeric(StringRef RHS) const {
120 return str().compare_numeric(RHS);
124 /// @name String Predicates
127 /// startswith - Check if this string starts with the given \arg Prefix.
128 bool startswith(StringRef Prefix) const {
129 return str().startswith(Prefix);
132 /// endswith - Check if this string ends with the given \arg Suffix.
133 bool endswith(StringRef Suffix) const {
134 return str().endswith(Suffix);
138 /// @name String Searching
141 /// find - Search for the first character \arg C in the string.
143 /// \return - The index of the first occurrence of \arg C, or npos if not
145 size_t find(char C, size_t From = 0) const {
146 return str().find(C, From);
149 /// find - Search for the first string \arg Str in the string.
151 /// \return - The index of the first occurrence of \arg Str, or npos if not
153 size_t find(StringRef Str, size_t From = 0) const {
154 return str().find(Str, From);
157 /// rfind - Search for the last character \arg C in the string.
159 /// \return - The index of the last occurrence of \arg C, or npos if not
161 size_t rfind(char C, size_t From = StringRef::npos) const {
162 return str().rfind(C, From);
165 /// rfind - Search for the last string \arg Str in the string.
167 /// \return - The index of the last occurrence of \arg Str, or npos if not
169 size_t rfind(StringRef Str) const {
170 return str().rfind(Str);
173 /// find_first_of - Find the first character in the string that is \arg C,
174 /// or npos if not found. Same as find.
175 size_t find_first_of(char C, size_t From = 0) const {
176 return str().find_first_of(C, From);
179 /// find_first_of - Find the first character in the string that is in \arg
180 /// Chars, or npos if not found.
182 /// Note: O(size() + Chars.size())
183 size_t find_first_of(StringRef Chars, size_t From = 0) const {
184 return str().find_first_of(Chars, From);
187 /// find_first_not_of - Find the first character in the string that is not
188 /// \arg C or npos if not found.
189 size_t find_first_not_of(char C, size_t From = 0) const {
190 return str().find_first_not_of(C, From);
193 /// find_first_not_of - Find the first character in the string that is not
194 /// in the string \arg Chars, or npos if not found.
196 /// Note: O(size() + Chars.size())
197 size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
198 return str().find_first_not_of(Chars, From);
201 /// find_last_of - Find the last character in the string that is \arg C, or
202 /// npos if not found.
203 size_t find_last_of(char C, size_t From = StringRef::npos) const {
204 return str().find_last_of(C, From);
207 /// find_last_of - Find the last character in the string that is in \arg C,
208 /// or npos if not found.
210 /// Note: O(size() + Chars.size())
212 StringRef Chars, size_t From = StringRef::npos) const {
213 return str().find_last_of(Chars, From);
217 /// @name Helpful Algorithms
220 /// count - Return the number of occurrences of \arg C in the string.
221 size_t count(char C) const {
222 return str().count(C);
225 /// count - Return the number of non-overlapped occurrences of \arg Str in
227 size_t count(StringRef Str) const {
228 return str().count(Str);
232 /// @name Substring Operations
235 /// substr - Return a reference to the substring from [Start, Start + N).
237 /// \param Start - The index of the starting character in the substring; if
238 /// the index is npos or greater than the length of the string then the
239 /// empty substring will be returned.
241 /// \param N - The number of characters to included in the substring. If N
242 /// exceeds the number of characters remaining in the string, the string
243 /// suffix (starting with \arg Start) will be returned.
244 StringRef substr(size_t Start, size_t N = StringRef::npos) const {
245 return str().substr(Start, N);
248 /// slice - Return a reference to the substring from [Start, End).
250 /// \param Start - The index of the starting character in the substring; if
251 /// the index is npos or greater than the length of the string then the
252 /// empty substring will be returned.
254 /// \param End - The index following the last character to include in the
255 /// substring. If this is npos, or less than \arg Start, or exceeds the
256 /// number of characters remaining in the string, the string suffix
257 /// (starting with \arg Start) will be returned.
258 StringRef slice(size_t Start, size_t End) const {
259 return str().slice(Start, End);
264 /// Explicit conversion to StringRef
265 StringRef str() const { return StringRef(this->begin(), this->size()); }
267 // TODO: Make this const, if it's safe...
268 const char* c_str() {
274 /// Implicit conversion to StringRef.
275 operator StringRef() const { return str(); }
278 const SmallString &operator=(StringRef RHS) {
283 SmallString &operator+=(StringRef RHS) {
284 this->append(RHS.begin(), RHS.end());
287 SmallString &operator+=(char C) {