StringRef.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //===--- StringRef.h - Constant String Reference Wrapper --------*- 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. #ifndef LLVM_ADT_STRINGREF_H
  10. #define LLVM_ADT_STRINGREF_H
  11. #include "Compiler.h"
  12. #include <algorithm>
  13. #include <cassert>
  14. #include <cstring>
  15. #include <limits>
  16. #include <string>
  17. #include <utility>
  18. namespace llvm {
  19. template <typename T>
  20. class SmallVectorImpl;
  21. class APInt;
  22. class hash_code;
  23. class StringRef;
  24. /// Helper functions for StringRef::getAsInteger.
  25. bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
  26. unsigned long long &Result);
  27. bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
  28. /// StringRef - Represent a constant reference to a string, i.e. a character
  29. /// array and a length, which need not be null terminated.
  30. ///
  31. /// This class does not own the string data, it is expected to be used in
  32. /// situations where the character data resides in some other buffer, whose
  33. /// lifetime extends past that of the StringRef. For this reason, it is not in
  34. /// general safe to store a StringRef.
  35. class StringRef {
  36. public:
  37. typedef const char *iterator;
  38. typedef const char *const_iterator;
  39. static const size_t npos = ~size_t(0);
  40. typedef size_t size_type;
  41. private:
  42. /// The start of the string, in an external buffer.
  43. const char *Data;
  44. /// The length of the string.
  45. size_t Length;
  46. // Workaround memcmp issue with null pointers (undefined behavior)
  47. // by providing a specialized version
  48. LLVM_ATTRIBUTE_ALWAYS_INLINE
  49. static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
  50. if (Length == 0) { return 0; }
  51. return ::memcmp(Lhs,Rhs,Length);
  52. }
  53. public:
  54. /// @name Constructors
  55. /// @{
  56. /// Construct an empty string ref.
  57. /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
  58. /// Construct a string ref from a cstring.
  59. /*implicit*/ StringRef(const char *Str)
  60. : Data(Str) {
  61. assert(Str && "StringRef cannot be built from a NULL argument");
  62. Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
  63. }
  64. /// Construct a string ref from a pointer and length.
  65. LLVM_ATTRIBUTE_ALWAYS_INLINE
  66. /*implicit*/ StringRef(const char *data, size_t length)
  67. : Data(data), Length(length) {
  68. assert((data || length == 0) &&
  69. "StringRef cannot be built from a NULL argument with non-null length");
  70. }
  71. /// Construct a string ref from an std::string.
  72. LLVM_ATTRIBUTE_ALWAYS_INLINE
  73. /*implicit*/ StringRef(const std::string &Str)
  74. : Data(Str.data()), Length(Str.length()) {}
  75. /// @}
  76. /// @name Iterators
  77. /// @{
  78. iterator begin() const { return Data; }
  79. iterator end() const { return Data + Length; }
  80. const unsigned char *bytes_begin() const {
  81. return reinterpret_cast<const unsigned char *>(begin());
  82. }
  83. const unsigned char *bytes_end() const {
  84. return reinterpret_cast<const unsigned char *>(end());
  85. }
  86. /// @}
  87. /// @name String Operations
  88. /// @{
  89. /// data - Get a pointer to the start of the string (which may not be null
  90. /// terminated).
  91. LLVM_ATTRIBUTE_ALWAYS_INLINE
  92. const char *data() const { return Data; }
  93. /// empty - Check if the string is empty.
  94. LLVM_ATTRIBUTE_ALWAYS_INLINE
  95. bool empty() const { return Length == 0; }
  96. /// size - Get the string size.
  97. LLVM_ATTRIBUTE_ALWAYS_INLINE
  98. size_t size() const { return Length; }
  99. /// front - Get the first character in the string.
  100. char front() const {
  101. assert(!empty());
  102. return Data[0];
  103. }
  104. /// back - Get the last character in the string.
  105. char back() const {
  106. assert(!empty());
  107. return Data[Length-1];
  108. }
  109. // copy - Allocate copy in Allocator and return StringRef to it.
  110. template <typename Allocator> StringRef copy(Allocator &A) const {
  111. char *S = A.template Allocate<char>(Length);
  112. std::copy(begin(), end(), S);
  113. return StringRef(S, Length);
  114. }
  115. /// equals - Check for string equality, this is more efficient than
  116. /// compare() when the relative ordering of inequal strings isn't needed.
  117. LLVM_ATTRIBUTE_ALWAYS_INLINE
  118. bool equals(StringRef RHS) const {
  119. return (Length == RHS.Length &&
  120. compareMemory(Data, RHS.Data, RHS.Length) == 0);
  121. }
  122. /// equals_lower - Check for string equality, ignoring case.
  123. bool equals_lower(StringRef RHS) const {
  124. return Length == RHS.Length && compare_lower(RHS) == 0;
  125. }
  126. /// compare - Compare two strings; the result is -1, 0, or 1 if this string
  127. /// is lexicographically less than, equal to, or greater than the \p RHS.
  128. LLVM_ATTRIBUTE_ALWAYS_INLINE
  129. int compare(StringRef RHS) const {
  130. // Check the prefix for a mismatch.
  131. if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
  132. return Res < 0 ? -1 : 1;
  133. // Otherwise the prefixes match, so we only need to check the lengths.
  134. if (Length == RHS.Length)
  135. return 0;
  136. return Length < RHS.Length ? -1 : 1;
  137. }
  138. /// compare_lower - Compare two strings, ignoring case.
  139. int compare_lower(StringRef RHS) const;
  140. /// compare_numeric - Compare two strings, treating sequences of digits as
  141. /// numbers.
  142. int compare_numeric(StringRef RHS) const;
  143. /// \brief Determine the edit distance between this string and another
  144. /// string.
  145. ///
  146. /// \param Other the string to compare this string against.
  147. ///
  148. /// \param AllowReplacements whether to allow character
  149. /// replacements (change one character into another) as a single
  150. /// operation, rather than as two operations (an insertion and a
  151. /// removal).
  152. ///
  153. /// \param MaxEditDistance If non-zero, the maximum edit distance that
  154. /// this routine is allowed to compute. If the edit distance will exceed
  155. /// that maximum, returns \c MaxEditDistance+1.
  156. ///
  157. /// \returns the minimum number of character insertions, removals,
  158. /// or (if \p AllowReplacements is \c true) replacements needed to
  159. /// transform one of the given strings into the other. If zero,
  160. /// the strings are identical.
  161. unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
  162. unsigned MaxEditDistance = 0) const;
  163. /// str - Get the contents as an std::string.
  164. std::string str() const {
  165. if (!Data) return std::string();
  166. return std::string(Data, Length);
  167. }
  168. /// @}
  169. /// @name Operator Overloads
  170. /// @{
  171. char operator[](size_t Index) const {
  172. assert(Index < Length && "Invalid index!");
  173. return Data[Index];
  174. }
  175. /// @}
  176. /// @name Type Conversions
  177. /// @{
  178. operator std::string() const {
  179. return str();
  180. }
  181. /// @}
  182. /// @name String Predicates
  183. /// @{
  184. /// Check if this string starts with the given \p Prefix.
  185. LLVM_ATTRIBUTE_ALWAYS_INLINE
  186. bool startswith(StringRef Prefix) const {
  187. return Length >= Prefix.Length &&
  188. compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
  189. }
  190. /// Check if this string starts with the given \p Prefix, ignoring case.
  191. bool startswith_lower(StringRef Prefix) const;
  192. /// Check if this string ends with the given \p Suffix.
  193. LLVM_ATTRIBUTE_ALWAYS_INLINE
  194. bool endswith(StringRef Suffix) const {
  195. return Length >= Suffix.Length &&
  196. compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
  197. }
  198. /// Check if this string ends with the given \p Suffix, ignoring case.
  199. bool endswith_lower(StringRef Suffix) const;
  200. /// @}
  201. /// @name String Searching
  202. /// @{
  203. /// Search for the first character \p C in the string.
  204. ///
  205. /// \returns The index of the first occurrence of \p C, or npos if not
  206. /// found.
  207. LLVM_ATTRIBUTE_ALWAYS_INLINE
  208. size_t find(char C, size_t From = 0) const {
  209. size_t FindBegin = std::min(From, Length);
  210. if (FindBegin < Length) { // Avoid calling memchr with nullptr.
  211. // Just forward to memchr, which is faster than a hand-rolled loop.
  212. if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
  213. return (size_t)(static_cast<const char *>(P) - Data);
  214. }
  215. return npos;
  216. }
  217. /// Search for the first string \p Str in the string.
  218. ///
  219. /// \returns The index of the first occurrence of \p Str, or npos if not
  220. /// found.
  221. size_t find(StringRef Str, size_t From = 0) const;
  222. /// Search for the last character \p C in the string.
  223. ///
  224. /// \returns The index of the last occurrence of \p C, or npos if not
  225. /// found.
  226. size_t rfind(char C, size_t From = npos) const {
  227. From = std::min(From, Length);
  228. size_t i = From;
  229. while (i != 0) {
  230. --i;
  231. if (Data[i] == C)
  232. return i;
  233. }
  234. return npos;
  235. }
  236. /// Search for the last string \p Str in the string.
  237. ///
  238. /// \returns The index of the last occurrence of \p Str, or npos if not
  239. /// found.
  240. size_t rfind(StringRef Str) const;
  241. /// Find the first character in the string that is \p C, or npos if not
  242. /// found. Same as find.
  243. size_t find_first_of(char C, size_t From = 0) const {
  244. return find(C, From);
  245. }
  246. /// Find the first character in the string that is in \p Chars, or npos if
  247. /// not found.
  248. ///
  249. /// Complexity: O(size() + Chars.size())
  250. size_t find_first_of(StringRef Chars, size_t From = 0) const;
  251. /// Find the first character in the string that is not \p C or npos if not
  252. /// found.
  253. size_t find_first_not_of(char C, size_t From = 0) const;
  254. /// Find the first character in the string that is not in the string
  255. /// \p Chars, or npos if not found.
  256. ///
  257. /// Complexity: O(size() + Chars.size())
  258. size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
  259. /// Find the last character in the string that is \p C, or npos if not
  260. /// found.
  261. size_t find_last_of(char C, size_t From = npos) const {
  262. return rfind(C, From);
  263. }
  264. /// Find the last character in the string that is in \p C, or npos if not
  265. /// found.
  266. ///
  267. /// Complexity: O(size() + Chars.size())
  268. size_t find_last_of(StringRef Chars, size_t From = npos) const;
  269. /// Find the last character in the string that is not \p C, or npos if not
  270. /// found.
  271. size_t find_last_not_of(char C, size_t From = npos) const;
  272. /// Find the last character in the string that is not in \p Chars, or
  273. /// npos if not found.
  274. ///
  275. /// Complexity: O(size() + Chars.size())
  276. size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
  277. /// @}
  278. /// @name Helpful Algorithms
  279. /// @{
  280. /// Return the number of occurrences of \p C in the string.
  281. size_t count(char C) const {
  282. size_t Count = 0;
  283. for (size_t i = 0, e = Length; i != e; ++i)
  284. if (Data[i] == C)
  285. ++Count;
  286. return Count;
  287. }
  288. /// Return the number of non-overlapped occurrences of \p Str in
  289. /// the string.
  290. size_t count(StringRef Str) const;
  291. /// Parse the current string as an integer of the specified radix. If
  292. /// \p Radix is specified as zero, this does radix autosensing using
  293. /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
  294. ///
  295. /// If the string is invalid or if only a subset of the string is valid,
  296. /// this returns true to signify the error. The string is considered
  297. /// erroneous if empty or if it overflows T.
  298. template <typename T>
  299. typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
  300. getAsInteger(unsigned Radix, T &Result) const {
  301. long long LLVal;
  302. if (getAsSignedInteger(*this, Radix, LLVal) ||
  303. static_cast<T>(LLVal) != LLVal)
  304. return true;
  305. Result = LLVal;
  306. return false;
  307. }
  308. template <typename T>
  309. typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
  310. getAsInteger(unsigned Radix, T &Result) const {
  311. unsigned long long ULLVal;
  312. // The additional cast to unsigned long long is required to avoid the
  313. // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
  314. // 'unsigned __int64' when instantiating getAsInteger with T = bool.
  315. if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
  316. static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
  317. return true;
  318. Result = ULLVal;
  319. return false;
  320. }
  321. /// Parse the current string as an integer of the specified \p Radix, or of
  322. /// an autosensed radix if the \p Radix given is 0. The current value in
  323. /// \p Result is discarded, and the storage is changed to be wide enough to
  324. /// store the parsed integer.
  325. ///
  326. /// \returns true if the string does not solely consist of a valid
  327. /// non-empty number in the appropriate base.
  328. ///
  329. /// APInt::fromString is superficially similar but assumes the
  330. /// string is well-formed in the given radix.
  331. bool getAsInteger(unsigned Radix, APInt &Result) const;
  332. /// @}
  333. /// @name String Operations
  334. /// @{
  335. // Convert the given ASCII string to lowercase.
  336. std::string lower() const;
  337. /// Convert the given ASCII string to uppercase.
  338. std::string upper() const;
  339. /// @}
  340. /// @name Substring Operations
  341. /// @{
  342. /// Return a reference to the substring from [Start, Start + N).
  343. ///
  344. /// \param Start The index of the starting character in the substring; if
  345. /// the index is npos or greater than the length of the string then the
  346. /// empty substring will be returned.
  347. ///
  348. /// \param N The number of characters to included in the substring. If N
  349. /// exceeds the number of characters remaining in the string, the string
  350. /// suffix (starting with \p Start) will be returned.
  351. LLVM_ATTRIBUTE_ALWAYS_INLINE
  352. StringRef substr(size_t Start, size_t N = npos) const {
  353. Start = std::min(Start, Length);
  354. return StringRef(Data + Start, std::min(N, Length - Start));
  355. }
  356. /// Return a StringRef equal to 'this' but with the first \p N elements
  357. /// dropped.
  358. LLVM_ATTRIBUTE_ALWAYS_INLINE
  359. StringRef drop_front(size_t N = 1) const {
  360. assert(size() >= N && "Dropping more elements than exist");
  361. return substr(N);
  362. }
  363. /// Return a StringRef equal to 'this' but with the last \p N elements
  364. /// dropped.
  365. LLVM_ATTRIBUTE_ALWAYS_INLINE
  366. StringRef drop_back(size_t N = 1) const {
  367. assert(size() >= N && "Dropping more elements than exist");
  368. return substr(0, size()-N);
  369. }
  370. /// Return a reference to the substring from [Start, End).
  371. ///
  372. /// \param Start The index of the starting character in the substring; if
  373. /// the index is npos or greater than the length of the string then the
  374. /// empty substring will be returned.
  375. ///
  376. /// \param End The index following the last character to include in the
  377. /// substring. If this is npos, or less than \p Start, or exceeds the
  378. /// number of characters remaining in the string, the string suffix
  379. /// (starting with \p Start) will be returned.
  380. LLVM_ATTRIBUTE_ALWAYS_INLINE
  381. StringRef slice(size_t Start, size_t End) const {
  382. Start = std::min(Start, Length);
  383. End = std::min(std::max(Start, End), Length);
  384. return StringRef(Data + Start, End - Start);
  385. }
  386. /// Split into two substrings around the first occurrence of a separator
  387. /// character.
  388. ///
  389. /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
  390. /// such that (*this == LHS + Separator + RHS) is true and RHS is
  391. /// maximal. If \p Separator is not in the string, then the result is a
  392. /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
  393. ///
  394. /// \param Separator The character to split on.
  395. /// \returns The split substrings.
  396. std::pair<StringRef, StringRef> split(char Separator) const {
  397. size_t Idx = find(Separator);
  398. if (Idx == npos)
  399. return std::make_pair(*this, StringRef());
  400. return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
  401. }
  402. /// Split into two substrings around the first occurrence of a separator
  403. /// string.
  404. ///
  405. /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
  406. /// such that (*this == LHS + Separator + RHS) is true and RHS is
  407. /// maximal. If \p Separator is not in the string, then the result is a
  408. /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
  409. ///
  410. /// \param Separator - The string to split on.
  411. /// \return - The split substrings.
  412. std::pair<StringRef, StringRef> split(StringRef Separator) const {
  413. size_t Idx = find(Separator);
  414. if (Idx == npos)
  415. return std::make_pair(*this, StringRef());
  416. return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
  417. }
  418. /// Split into substrings around the occurrences of a separator string.
  419. ///
  420. /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
  421. /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
  422. /// elements are added to A.
  423. /// If \p KeepEmpty is false, empty strings are not added to \p A. They
  424. /// still count when considering \p MaxSplit
  425. /// An useful invariant is that
  426. /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
  427. ///
  428. /// \param A - Where to put the substrings.
  429. /// \param Separator - The string to split on.
  430. /// \param MaxSplit - The maximum number of times the string is split.
  431. /// \param KeepEmpty - True if empty substring should be added.
  432. void split(SmallVectorImpl<StringRef> &A,
  433. StringRef Separator, int MaxSplit = -1,
  434. bool KeepEmpty = true) const;
  435. /// Split into substrings around the occurrences of a separator character.
  436. ///
  437. /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
  438. /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
  439. /// elements are added to A.
  440. /// If \p KeepEmpty is false, empty strings are not added to \p A. They
  441. /// still count when considering \p MaxSplit
  442. /// An useful invariant is that
  443. /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
  444. ///
  445. /// \param A - Where to put the substrings.
  446. /// \param Separator - The string to split on.
  447. /// \param MaxSplit - The maximum number of times the string is split.
  448. /// \param KeepEmpty - True if empty substring should be added.
  449. void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
  450. bool KeepEmpty = true) const;
  451. /// Split into two substrings around the last occurrence of a separator
  452. /// character.
  453. ///
  454. /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
  455. /// such that (*this == LHS + Separator + RHS) is true and RHS is
  456. /// minimal. If \p Separator is not in the string, then the result is a
  457. /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
  458. ///
  459. /// \param Separator - The character to split on.
  460. /// \return - The split substrings.
  461. std::pair<StringRef, StringRef> rsplit(char Separator) const {
  462. size_t Idx = rfind(Separator);
  463. if (Idx == npos)
  464. return std::make_pair(*this, StringRef());
  465. return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
  466. }
  467. /// Return string with consecutive characters in \p Chars starting from
  468. /// the left removed.
  469. StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
  470. return drop_front(std::min(Length, find_first_not_of(Chars)));
  471. }
  472. /// Return string with consecutive characters in \p Chars starting from
  473. /// the right removed.
  474. StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
  475. return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
  476. }
  477. /// Return string with consecutive characters in \p Chars starting from
  478. /// the left and right removed.
  479. StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
  480. return ltrim(Chars).rtrim(Chars);
  481. }
  482. /// @}
  483. };
  484. /// @name StringRef Comparison Operators
  485. /// @{
  486. LLVM_ATTRIBUTE_ALWAYS_INLINE
  487. inline bool operator==(StringRef LHS, StringRef RHS) {
  488. return LHS.equals(RHS);
  489. }
  490. LLVM_ATTRIBUTE_ALWAYS_INLINE
  491. inline bool operator!=(StringRef LHS, StringRef RHS) {
  492. return !(LHS == RHS);
  493. }
  494. inline bool operator<(StringRef LHS, StringRef RHS) {
  495. return LHS.compare(RHS) == -1;
  496. }
  497. inline bool operator<=(StringRef LHS, StringRef RHS) {
  498. return LHS.compare(RHS) != 1;
  499. }
  500. inline bool operator>(StringRef LHS, StringRef RHS) {
  501. return LHS.compare(RHS) == 1;
  502. }
  503. inline bool operator>=(StringRef LHS, StringRef RHS) {
  504. return LHS.compare(RHS) != -1;
  505. }
  506. inline std::string &operator+=(std::string &buffer, StringRef string) {
  507. return buffer.append(string.data(), string.size());
  508. }
  509. /// @}
  510. /// \brief Compute a hash_code for a StringRef.
  511. hash_code hash_value(StringRef S);
  512. // StringRefs can be treated like a POD type.
  513. template <typename T> struct isPodLike;
  514. template <> struct isPodLike<StringRef> { static const bool value = true; };
  515. }
  516. #endif