SmallString.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the SmallString class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ADT_SMALLSTRING_H
  18. #define LLVM_ADT_SMALLSTRING_H
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include <cstddef>
  22. namespace llvm {
  23. /// SmallString - A SmallString is just a SmallVector with methods and accessors
  24. /// that make it work better as a string (e.g. operator+ etc).
  25. template<unsigned InternalLen>
  26. class SmallString : public SmallVector<char, InternalLen> {
  27. public:
  28. /// Default ctor - Initialize to empty.
  29. SmallString() = default;
  30. /// Initialize from a StringRef.
  31. SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
  32. /// Initialize by concatenating a list of StringRefs.
  33. SmallString(std::initializer_list<StringRef> Refs)
  34. : SmallVector<char, InternalLen>() {
  35. this->append(Refs);
  36. }
  37. /// Initialize with a range.
  38. template<typename ItTy>
  39. SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
  40. /// @}
  41. /// @name String Assignment
  42. /// @{
  43. using SmallVector<char, InternalLen>::assign;
  44. /// Assign from a StringRef.
  45. void assign(StringRef RHS) {
  46. SmallVectorImpl<char>::assign(RHS.begin(), RHS.end());
  47. }
  48. /// Assign from a list of StringRefs.
  49. void assign(std::initializer_list<StringRef> Refs) {
  50. this->clear();
  51. append(Refs);
  52. }
  53. /// @}
  54. /// @name String Concatenation
  55. /// @{
  56. using SmallVector<char, InternalLen>::append;
  57. /// Append from a StringRef.
  58. void append(StringRef RHS) {
  59. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  60. }
  61. /// Append from a list of StringRefs.
  62. void append(std::initializer_list<StringRef> Refs) {
  63. size_t SizeNeeded = this->size();
  64. for (const StringRef &Ref : Refs)
  65. SizeNeeded += Ref.size();
  66. this->reserve(SizeNeeded);
  67. auto CurEnd = this->end();
  68. for (const StringRef &Ref : Refs) {
  69. this->uninitialized_copy(Ref.begin(), Ref.end(), CurEnd);
  70. CurEnd += Ref.size();
  71. }
  72. this->set_size(SizeNeeded);
  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->data(), 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. explicit operator std::string() const {
  231. return std::string(this->data(), this->size());
  232. }
  233. // Extra operators.
  234. SmallString &operator=(StringRef RHS) {
  235. this->assign(RHS);
  236. return *this;
  237. }
  238. SmallString &operator+=(StringRef RHS) {
  239. this->append(RHS.begin(), RHS.end());
  240. return *this;
  241. }
  242. SmallString &operator+=(char C) {
  243. this->push_back(C);
  244. return *this;
  245. }
  246. };
  247. } // end namespace llvm
  248. #endif // LLVM_ADT_SMALLSTRING_H
  249. #ifdef __GNUC__
  250. #pragma GCC diagnostic pop
  251. #endif