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(unsigned 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 /// Append from a StringRef 82 void append(StringRef RHS) { 83 SmallVectorImpl<char>::append(RHS.begin(), RHS.end()); 84 } 85 86 /// Append from a SmallVector 87 void append(const SmallVectorImpl<char> &RHS) { 88 SmallVectorImpl<char>::append(RHS.begin(), RHS.end()); 89 } 90 91 /// @} 92 /// @name String Comparison 93 /// @{ 94 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); 99 } 100 101 /// equals_lower - Check for string equality, ignoring case. 102 bool equals_lower(StringRef RHS) const { 103 return str().equals_lower(RHS); 104 } 105 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); 110 } 111 112 /// compare_lower - Compare two strings, ignoring case. 113 int compare_lower(StringRef RHS) const { 114 return str().compare_lower(RHS); 115 } 116 117 /// compare_numeric - Compare two strings, treating sequences of digits as 118 /// numbers. 119 int compare_numeric(StringRef RHS) const { 120 return str().compare_numeric(RHS); 121 } 122 123 /// @} 124 /// @name String Predicates 125 /// @{ 126 127 /// startswith - Check if this string starts with the given \arg Prefix. 128 bool startswith(StringRef Prefix) const { 129 return str().startswith(Prefix); 130 } 131 132 /// endswith - Check if this string ends with the given \arg Suffix. 133 bool endswith(StringRef Suffix) const { 134 return str().endswith(Suffix); 135 } 136 137 /// @} 138 /// @name String Searching 139 /// @{ 140 141 /// find - Search for the first character \arg C in the string. 142 /// 143 /// \return - The index of the first occurrence of \arg C, or npos if not 144 /// found. 145 size_t find(char C, size_t From = 0) const { 146 return str().find(C, From); 147 } 148 149 /// find - Search for the first string \arg Str in the string. 150 /// 151 /// \return - The index of the first occurrence of \arg Str, or npos if not 152 /// found. 153 size_t find(StringRef Str, size_t From = 0) const { 154 return str().find(Str, From); 155 } 156 157 /// rfind - Search for the last character \arg C in the string. 158 /// 159 /// \return - The index of the last occurrence of \arg C, or npos if not 160 /// found. 161 size_t rfind(char C, size_t From = StringRef::npos) const { 162 return str().rfind(C, From); 163 } 164 165 /// rfind - Search for the last string \arg Str in the string. 166 /// 167 /// \return - The index of the last occurrence of \arg Str, or npos if not 168 /// found. 169 size_t rfind(StringRef Str) const { 170 return str().rfind(Str); 171 } 172 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); 177 } 178 179 /// find_first_of - Find the first character in the string that is in \arg 180 /// Chars, or npos if not found. 181 /// 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); 185 } 186 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); 191 } 192 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. 195 /// 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); 199 } 200 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); 205 } 206 207 /// find_last_of - Find the last character in the string that is in \arg C, 208 /// or npos if not found. 209 /// 210 /// Note: O(size() + Chars.size()) 211 size_t find_last_of( 212 StringRef Chars, size_t From = StringRef::npos) const { 213 return str().find_last_of(Chars, From); 214 } 215 216 /// @} 217 /// @name Helpful Algorithms 218 /// @{ 219 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); 223 } 224 225 /// count - Return the number of non-overlapped occurrences of \arg Str in 226 /// the string. 227 size_t count(StringRef Str) const { 228 return str().count(Str); 229 } 230 231 /// @} 232 /// @name Substring Operations 233 /// @{ 234 235 /// substr - Return a reference to the substring from [Start, Start + N). 236 /// 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. 240 /// 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); 246 } 247 248 /// slice - Return a reference to the substring from [Start, End). 249 /// 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. 253 /// 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); 260 } 261 262 // Extra methods. 263 264 /// Explicit conversion to StringRef 265 StringRef str() const { return StringRef(this->begin(), this->size()); } 266 267 // TODO: Make this const, if it's safe... 268 const char* c_str() { 269 this->push_back(0); 270 this->pop_back(); 271 return this->data(); 272 } 273 274 /// Implicit conversion to StringRef. 275 operator StringRef() const { return str(); } 276 277 // Extra operators. 278 const SmallString &operator=(StringRef RHS) { 279 this->clear(); 280 return *this += RHS; 281 } 282 283 SmallString &operator+=(StringRef RHS) { 284 this->append(RHS.begin(), RHS.end()); 285 return *this; 286 } 287 SmallString &operator+=(char C) { 288 this->push_back(C); 289 return *this; 290 } 291}; 292 293} 294 295#endif 296