SmallString.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 - Compare two strings; the result is negative, zero, or positive
  88. /// if this string is lexicographically less than, equal to, or greater than
  89. /// the \p RHS.
  90. int compare(StringRef RHS) const {
  91. return str().compare(RHS);
  92. }
  93. /// compare_insensitive - Compare two strings, ignoring case.
  94. int compare_insensitive(StringRef RHS) const {
  95. return str().compare_insensitive(RHS);
  96. }
  97. /// compare_numeric - Compare two strings, treating sequences of digits as
  98. /// numbers.
  99. int compare_numeric(StringRef RHS) const {
  100. return str().compare_numeric(RHS);
  101. }
  102. /// @}
  103. /// @name String Predicates
  104. /// @{
  105. /// startswith - Check if this string starts with the given \p Prefix.
  106. bool startswith(StringRef Prefix) const {
  107. return str().startswith(Prefix);
  108. }
  109. /// endswith - Check if this string ends with the given \p Suffix.
  110. bool endswith(StringRef Suffix) const {
  111. return str().endswith(Suffix);
  112. }
  113. /// @}
  114. /// @name String Searching
  115. /// @{
  116. /// find - Search for the first character \p C in the string.
  117. ///
  118. /// \return - The index of the first occurrence of \p C, or npos if not
  119. /// found.
  120. size_t find(char C, size_t From = 0) const {
  121. return str().find(C, From);
  122. }
  123. /// Search for the first string \p Str in the string.
  124. ///
  125. /// \returns The index of the first occurrence of \p Str, or npos if not
  126. /// found.
  127. size_t find(StringRef Str, size_t From = 0) const {
  128. return str().find(Str, From);
  129. }
  130. /// Search for the last character \p C in the string.
  131. ///
  132. /// \returns The index of the last occurrence of \p C, or npos if not
  133. /// found.
  134. size_t rfind(char C, size_t From = StringRef::npos) const {
  135. return str().rfind(C, From);
  136. }
  137. /// Search for the last string \p Str in the string.
  138. ///
  139. /// \returns The index of the last occurrence of \p Str, or npos if not
  140. /// found.
  141. size_t rfind(StringRef Str) const {
  142. return str().rfind(Str);
  143. }
  144. /// Find the first character in the string that is \p C, or npos if not
  145. /// found. Same as find.
  146. size_t find_first_of(char C, size_t From = 0) const {
  147. return str().find_first_of(C, From);
  148. }
  149. /// Find the first character in the string that is in \p Chars, or npos if
  150. /// not found.
  151. ///
  152. /// Complexity: O(size() + Chars.size())
  153. size_t find_first_of(StringRef Chars, size_t From = 0) const {
  154. return str().find_first_of(Chars, From);
  155. }
  156. /// Find the first character in the string that is not \p C or npos if not
  157. /// found.
  158. size_t find_first_not_of(char C, size_t From = 0) const {
  159. return str().find_first_not_of(C, From);
  160. }
  161. /// Find the first character in the string that is not in the string
  162. /// \p Chars, or npos if not found.
  163. ///
  164. /// Complexity: O(size() + Chars.size())
  165. size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
  166. return str().find_first_not_of(Chars, From);
  167. }
  168. /// Find the last character in the string that is \p C, or npos if not
  169. /// found.
  170. size_t find_last_of(char C, size_t From = StringRef::npos) const {
  171. return str().find_last_of(C, From);
  172. }
  173. /// Find the last character in the string that is in \p C, or npos if not
  174. /// found.
  175. ///
  176. /// Complexity: O(size() + Chars.size())
  177. size_t find_last_of(
  178. StringRef Chars, size_t From = StringRef::npos) const {
  179. return str().find_last_of(Chars, From);
  180. }
  181. /// @}
  182. /// @name Helpful Algorithms
  183. /// @{
  184. /// Return the number of occurrences of \p C in the string.
  185. size_t count(char C) const {
  186. return str().count(C);
  187. }
  188. /// Return the number of non-overlapped occurrences of \p Str in the
  189. /// string.
  190. size_t count(StringRef Str) const {
  191. return str().count(Str);
  192. }
  193. /// @}
  194. /// @name Substring Operations
  195. /// @{
  196. /// Return a reference to the substring from [Start, Start + N).
  197. ///
  198. /// \param Start The index of the starting character in the substring; if
  199. /// the index is npos or greater than the length of the string then the
  200. /// empty substring will be returned.
  201. ///
  202. /// \param N The number of characters to included in the substring. If \p N
  203. /// exceeds the number of characters remaining in the string, the string
  204. /// suffix (starting with \p Start) will be returned.
  205. StringRef substr(size_t Start, size_t N = StringRef::npos) const {
  206. return str().substr(Start, N);
  207. }
  208. /// Return a reference to the substring from [Start, End).
  209. ///
  210. /// \param Start The index of the starting character in the substring; if
  211. /// the index is npos or greater than the length of the string then the
  212. /// empty substring will be returned.
  213. ///
  214. /// \param End The index following the last character to include in the
  215. /// substring. If this is npos, or less than \p Start, or exceeds the
  216. /// number of characters remaining in the string, the string suffix
  217. /// (starting with \p Start) will be returned.
  218. StringRef slice(size_t Start, size_t End) const {
  219. return str().slice(Start, End);
  220. }
  221. // Extra methods.
  222. /// Explicit conversion to StringRef.
  223. StringRef str() const { return StringRef(this->data(), this->size()); }
  224. // TODO: Make this const, if it's safe...
  225. const char* c_str() {
  226. this->push_back(0);
  227. this->pop_back();
  228. return this->data();
  229. }
  230. /// Implicit conversion to StringRef.
  231. operator StringRef() const { return str(); }
  232. explicit operator std::string() const {
  233. return std::string(this->data(), this->size());
  234. }
  235. // Extra operators.
  236. SmallString &operator=(StringRef RHS) {
  237. this->assign(RHS);
  238. return *this;
  239. }
  240. SmallString &operator+=(StringRef RHS) {
  241. this->append(RHS.begin(), RHS.end());
  242. return *this;
  243. }
  244. SmallString &operator+=(char C) {
  245. this->push_back(C);
  246. return *this;
  247. }
  248. };
  249. } // end namespace llvm
  250. #endif // LLVM_ADT_SMALLSTRING_H
  251. #ifdef __GNUC__
  252. #pragma GCC diagnostic pop
  253. #endif