SmallString.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #ifndef LLVM_ADT_SMALLSTRING_H
  14. #define LLVM_ADT_SMALLSTRING_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringRef.h"
  17. namespace llvm {
  18. /// SmallString - A SmallString is just a SmallVector with methods and accessors
  19. /// that make it work better as a string (e.g. operator+ etc).
  20. template<unsigned InternalLen>
  21. class SmallString : public SmallVector<char, InternalLen> {
  22. public:
  23. /// Default ctor - Initialize to empty.
  24. SmallString() {}
  25. /// Initialize from a StringRef.
  26. SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
  27. /// Initialize with a range.
  28. template<typename ItTy>
  29. SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
  30. // Note that in order to add new overloads for append & assign, we have to
  31. // duplicate the inherited versions so as not to inadvertently hide them.
  32. /// @}
  33. /// @name String Assignment
  34. /// @{
  35. /// Assign from a repeated element.
  36. void assign(size_t NumElts, char Elt) {
  37. this->SmallVectorImpl<char>::assign(NumElts, Elt);
  38. }
  39. /// Assign from an iterator pair.
  40. template<typename in_iter>
  41. void assign(in_iter S, in_iter E) {
  42. this->clear();
  43. SmallVectorImpl<char>::append(S, E);
  44. }
  45. /// Assign from a StringRef.
  46. void assign(StringRef RHS) {
  47. this->clear();
  48. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  49. }
  50. /// Assign from a SmallVector.
  51. void assign(const SmallVectorImpl<char> &RHS) {
  52. this->clear();
  53. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  54. }
  55. /// @}
  56. /// @name String Concatenation
  57. /// @{
  58. /// Append from an iterator pair.
  59. template<typename in_iter>
  60. void append(in_iter S, in_iter E) {
  61. SmallVectorImpl<char>::append(S, E);
  62. }
  63. void append(size_t NumInputs, char Elt) {
  64. SmallVectorImpl<char>::append(NumInputs, Elt);
  65. }
  66. /// Append from a StringRef.
  67. void append(StringRef RHS) {
  68. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  69. }
  70. /// Append from a SmallVector.
  71. void append(const SmallVectorImpl<char> &RHS) {
  72. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  73. }
  74. /// @}
  75. /// @name String Comparison
  76. /// @{
  77. /// Check for string equality. This is more efficient than compare() when
  78. /// the relative ordering of inequal strings isn't needed.
  79. bool equals(StringRef RHS) const {
  80. return str().equals(RHS);
  81. }
  82. /// Check for string equality, ignoring case.
  83. bool equals_lower(StringRef RHS) const {
  84. return str().equals_lower(RHS);
  85. }
  86. /// Compare two strings; the result is -1, 0, or 1 if this string is
  87. /// lexicographically less than, equal to, or greater than the \p RHS.
  88. int compare(StringRef RHS) const {
  89. return str().compare(RHS);
  90. }
  91. /// compare_lower - Compare two strings, ignoring case.
  92. int compare_lower(StringRef RHS) const {
  93. return str().compare_lower(RHS);
  94. }
  95. /// compare_numeric - Compare two strings, treating sequences of digits as
  96. /// numbers.
  97. int compare_numeric(StringRef RHS) const {
  98. return str().compare_numeric(RHS);
  99. }
  100. /// @}
  101. /// @name String Predicates
  102. /// @{
  103. /// startswith - Check if this string starts with the given \p Prefix.
  104. bool startswith(StringRef Prefix) const {
  105. return str().startswith(Prefix);
  106. }
  107. /// endswith - Check if this string ends with the given \p Suffix.
  108. bool endswith(StringRef Suffix) const {
  109. return str().endswith(Suffix);
  110. }
  111. /// @}
  112. /// @name String Searching
  113. /// @{
  114. /// find - Search for the first character \p C in the string.
  115. ///
  116. /// \return - The index of the first occurrence of \p C, or npos if not
  117. /// found.
  118. size_t find(char C, size_t From = 0) const {
  119. return str().find(C, From);
  120. }
  121. /// Search for the first string \p Str in the string.
  122. ///
  123. /// \returns The index of the first occurrence of \p Str, or npos if not
  124. /// found.
  125. size_t find(StringRef Str, size_t From = 0) const {
  126. return str().find(Str, From);
  127. }
  128. /// Search for the last character \p C in the string.
  129. ///
  130. /// \returns The index of the last occurrence of \p C, or npos if not
  131. /// found.
  132. size_t rfind(char C, size_t From = StringRef::npos) const {
  133. return str().rfind(C, From);
  134. }
  135. /// Search for the last string \p Str in the string.
  136. ///
  137. /// \returns The index of the last occurrence of \p Str, or npos if not
  138. /// found.
  139. size_t rfind(StringRef Str) const {
  140. return str().rfind(Str);
  141. }
  142. /// Find the first character in the string that is \p C, or npos if not
  143. /// found. Same as find.
  144. size_t find_first_of(char C, size_t From = 0) const {
  145. return str().find_first_of(C, From);
  146. }
  147. /// Find the first character in the string that is in \p Chars, or npos if
  148. /// not found.
  149. ///
  150. /// Complexity: O(size() + Chars.size())
  151. size_t find_first_of(StringRef Chars, size_t From = 0) const {
  152. return str().find_first_of(Chars, From);
  153. }
  154. /// Find the first character in the string that is not \p C or npos if not
  155. /// found.
  156. size_t find_first_not_of(char C, size_t From = 0) const {
  157. return str().find_first_not_of(C, From);
  158. }
  159. /// Find the first character in the string that is not in the string
  160. /// \p Chars, or npos if not found.
  161. ///
  162. /// Complexity: O(size() + Chars.size())
  163. size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
  164. return str().find_first_not_of(Chars, From);
  165. }
  166. /// Find the last character in the string that is \p C, or npos if not
  167. /// found.
  168. size_t find_last_of(char C, size_t From = StringRef::npos) const {
  169. return str().find_last_of(C, From);
  170. }
  171. /// Find the last character in the string that is in \p C, or npos if not
  172. /// found.
  173. ///
  174. /// Complexity: O(size() + Chars.size())
  175. size_t find_last_of(
  176. StringRef Chars, size_t From = StringRef::npos) const {
  177. return str().find_last_of(Chars, From);
  178. }
  179. /// @}
  180. /// @name Helpful Algorithms
  181. /// @{
  182. /// Return the number of occurrences of \p C in the string.
  183. size_t count(char C) const {
  184. return str().count(C);
  185. }
  186. /// Return the number of non-overlapped occurrences of \p Str in the
  187. /// string.
  188. size_t count(StringRef Str) const {
  189. return str().count(Str);
  190. }
  191. /// @}
  192. /// @name Substring Operations
  193. /// @{
  194. /// Return a reference to the substring from [Start, Start + N).
  195. ///
  196. /// \param Start The index of the starting character in the substring; if
  197. /// the index is npos or greater than the length of the string then the
  198. /// empty substring will be returned.
  199. ///
  200. /// \param N The number of characters to included in the substring. If \p N
  201. /// exceeds the number of characters remaining in the string, the string
  202. /// suffix (starting with \p Start) will be returned.
  203. StringRef substr(size_t Start, size_t N = StringRef::npos) const {
  204. return str().substr(Start, N);
  205. }
  206. /// Return a reference to the substring from [Start, End).
  207. ///
  208. /// \param Start The index of the starting character in the substring; if
  209. /// the index is npos or greater than the length of the string then the
  210. /// empty substring will be returned.
  211. ///
  212. /// \param End The index following the last character to include in the
  213. /// substring. If this is npos, or less than \p Start, or exceeds the
  214. /// number of characters remaining in the string, the string suffix
  215. /// (starting with \p Start) will be returned.
  216. StringRef slice(size_t Start, size_t End) const {
  217. return str().slice(Start, End);
  218. }
  219. // Extra methods.
  220. /// Explicit conversion to StringRef.
  221. StringRef str() const { return StringRef(this->begin(), this->size()); }
  222. // TODO: Make this const, if it's safe...
  223. const char* c_str() {
  224. this->push_back(0);
  225. this->pop_back();
  226. return this->data();
  227. }
  228. /// Implicit conversion to StringRef.
  229. operator StringRef() const { return str(); }
  230. // Extra operators.
  231. const SmallString &operator=(StringRef RHS) {
  232. this->clear();
  233. return *this += RHS;
  234. }
  235. SmallString &operator+=(StringRef RHS) {
  236. this->append(RHS.begin(), RHS.end());
  237. return *this;
  238. }
  239. SmallString &operator+=(char C) {
  240. this->push_back(C);
  241. return *this;
  242. }
  243. };
  244. }
  245. #endif