Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
8.5 KiB

  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. /// Copy ctor.
  31. SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
  32. // Note that in order to add new overloads for append & assign, we have to
  33. // duplicate the inherited versions so as not to inadvertently hide them.
  34. /// @}
  35. /// @name String Assignment
  36. /// @{
  37. /// Assign from a repeated element.
  38. void assign(size_t NumElts, char Elt) {
  39. this->SmallVectorImpl<char>::assign(NumElts, Elt);
  40. }
  41. /// Assign from an iterator pair.
  42. template<typename in_iter>
  43. void assign(in_iter S, in_iter E) {
  44. this->clear();
  45. SmallVectorImpl<char>::append(S, E);
  46. }
  47. /// Assign from a StringRef.
  48. void assign(StringRef RHS) {
  49. this->clear();
  50. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  51. }
  52. /// Assign from a SmallVector.
  53. void assign(const SmallVectorImpl<char> &RHS) {
  54. this->clear();
  55. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  56. }
  57. /// @}
  58. /// @name String Concatenation
  59. /// @{
  60. /// Append from an iterator pair.
  61. template<typename in_iter>
  62. void append(in_iter S, in_iter E) {
  63. SmallVectorImpl<char>::append(S, E);
  64. }
  65. void append(size_t NumInputs, char Elt) {
  66. SmallVectorImpl<char>::append(NumInputs, Elt);
  67. }
  68. /// Append from a StringRef.
  69. void append(StringRef RHS) {
  70. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  71. }
  72. /// Append from a SmallVector.
  73. void append(const SmallVectorImpl<char> &RHS) {
  74. SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
  75. }
  76. /// @}
  77. /// @name String Comparison
  78. /// @{
  79. /// Check for string equality. This is more efficient than compare() when
  80. /// the relative ordering of inequal strings isn't needed.
  81. bool equals(StringRef RHS) const {
  82. return str().equals(RHS);
  83. }
  84. /// Check for string equality, ignoring case.
  85. bool equals_lower(StringRef RHS) const {
  86. return str().equals_lower(RHS);
  87. }
  88. /// Compare two strings; the result is -1, 0, or 1 if this string is
  89. /// lexicographically less than, equal to, or greater than the \p RHS.
  90. int compare(StringRef RHS) const {
  91. return str().compare(RHS);
  92. }
  93. /// compare_lower - Compare two strings, ignoring case.
  94. int compare_lower(StringRef RHS) const {
  95. return str().compare_lower(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->begin(), 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. // Extra operators.
  233. const SmallString &operator=(StringRef RHS) {
  234. this->clear();
  235. return *this += RHS;
  236. }
  237. SmallString &operator+=(StringRef RHS) {
  238. this->append(RHS.begin(), RHS.end());
  239. return *this;
  240. }
  241. SmallString &operator+=(char C) {
  242. this->push_back(C);
  243. return *this;
  244. }
  245. };
  246. }
  247. #endif