SmallString.h 8.6 KB

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