StringExtras.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/StringExtras.h - Useful string functions --------*- 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 contains some functions that are useful when dealing with strings.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_STRINGEXTRAS_H
  19. #define LLVM_ADT_STRINGEXTRAS_H
  20. #include "llvm/ADT/APSInt.h"
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/Twine.h"
  25. #include <cassert>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <iterator>
  31. #include <string>
  32. #include <utility>
  33. namespace llvm {
  34. class raw_ostream;
  35. /// hexdigit - Return the hexadecimal character for the
  36. /// given number \p X (which should be less than 16).
  37. inline char hexdigit(unsigned X, bool LowerCase = false) {
  38. assert(X < 16);
  39. static const char LUT[] = "0123456789ABCDEF";
  40. const uint8_t Offset = LowerCase ? 32 : 0;
  41. return LUT[X] | Offset;
  42. }
  43. /// Given an array of c-style strings terminated by a null pointer, construct
  44. /// a vector of StringRefs representing the same strings without the terminating
  45. /// null string.
  46. inline std::vector<StringRef> toStringRefArray(const char *const *Strings) {
  47. std::vector<StringRef> Result;
  48. while (*Strings)
  49. Result.push_back(*Strings++);
  50. return Result;
  51. }
  52. /// Construct a string ref from a boolean.
  53. inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); }
  54. /// Construct a string ref from an array ref of unsigned chars.
  55. inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
  56. return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size());
  57. }
  58. /// Construct a string ref from an array ref of unsigned chars.
  59. inline ArrayRef<uint8_t> arrayRefFromStringRef(StringRef Input) {
  60. return {Input.bytes_begin(), Input.bytes_end()};
  61. }
  62. /// Interpret the given character \p C as a hexadecimal digit and return its
  63. /// value.
  64. ///
  65. /// If \p C is not a valid hex digit, -1U is returned.
  66. inline unsigned hexDigitValue(char C) {
  67. /* clang-format off */
  68. static const int16_t LUT[256] = {
  69. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  71. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  72. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // '0'..'9'
  73. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 'A'..'F'
  74. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  75. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 'a'..'f'
  76. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  79. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  80. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  81. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  82. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  83. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  84. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  85. };
  86. /* clang-format on */
  87. return LUT[static_cast<unsigned char>(C)];
  88. }
  89. /// Checks if character \p C is one of the 10 decimal digits.
  90. inline bool isDigit(char C) { return C >= '0' && C <= '9'; }
  91. /// Checks if character \p C is a hexadecimal numeric character.
  92. inline bool isHexDigit(char C) { return hexDigitValue(C) != ~0U; }
  93. /// Checks if character \p C is a valid letter as classified by "C" locale.
  94. inline bool isAlpha(char C) {
  95. return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z');
  96. }
  97. /// Checks whether character \p C is either a decimal digit or an uppercase or
  98. /// lowercase letter as classified by "C" locale.
  99. inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); }
  100. /// Checks whether character \p C is valid ASCII (high bit is zero).
  101. inline bool isASCII(char C) { return static_cast<unsigned char>(C) <= 127; }
  102. /// Checks whether all characters in S are ASCII.
  103. inline bool isASCII(llvm::StringRef S) {
  104. for (char C : S)
  105. if (LLVM_UNLIKELY(!isASCII(C)))
  106. return false;
  107. return true;
  108. }
  109. /// Checks whether character \p C is printable.
  110. ///
  111. /// Locale-independent version of the C standard library isprint whose results
  112. /// may differ on different platforms.
  113. inline bool isPrint(char C) {
  114. unsigned char UC = static_cast<unsigned char>(C);
  115. return (0x20 <= UC) && (UC <= 0x7E);
  116. }
  117. /// Checks whether character \p C is whitespace in the "C" locale.
  118. ///
  119. /// Locale-independent version of the C standard library isspace.
  120. inline bool isSpace(char C) {
  121. return C == ' ' || C == '\f' || C == '\n' || C == '\r' || C == '\t' ||
  122. C == '\v';
  123. }
  124. /// Returns the corresponding lowercase character if \p x is uppercase.
  125. inline char toLower(char x) {
  126. if (x >= 'A' && x <= 'Z')
  127. return x - 'A' + 'a';
  128. return x;
  129. }
  130. /// Returns the corresponding uppercase character if \p x is lowercase.
  131. inline char toUpper(char x) {
  132. if (x >= 'a' && x <= 'z')
  133. return x - 'a' + 'A';
  134. return x;
  135. }
  136. inline std::string utohexstr(uint64_t X, bool LowerCase = false,
  137. unsigned Width = 0) {
  138. char Buffer[17];
  139. char *BufPtr = std::end(Buffer);
  140. if (X == 0) *--BufPtr = '0';
  141. for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
  142. unsigned char Mod = static_cast<unsigned char>(X) & 15;
  143. *--BufPtr = hexdigit(Mod, LowerCase);
  144. X >>= 4;
  145. }
  146. return std::string(BufPtr, std::end(Buffer));
  147. }
  148. /// Convert buffer \p Input to its hexadecimal representation.
  149. /// The returned string is double the size of \p Input.
  150. inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
  151. SmallVectorImpl<char> &Output) {
  152. const size_t Length = Input.size();
  153. Output.resize_for_overwrite(Length * 2);
  154. for (size_t i = 0; i < Length; i++) {
  155. const uint8_t c = Input[i];
  156. Output[i * 2 ] = hexdigit(c >> 4, LowerCase);
  157. Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);
  158. }
  159. }
  160. inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
  161. SmallString<16> Output;
  162. toHex(Input, LowerCase, Output);
  163. return std::string(Output);
  164. }
  165. inline std::string toHex(StringRef Input, bool LowerCase = false) {
  166. return toHex(arrayRefFromStringRef(Input), LowerCase);
  167. }
  168. /// Store the binary representation of the two provided values, \p MSB and
  169. /// \p LSB, that make up the nibbles of a hexadecimal digit. If \p MSB or \p LSB
  170. /// do not correspond to proper nibbles of a hexadecimal digit, this method
  171. /// returns false. Otherwise, returns true.
  172. inline bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex) {
  173. unsigned U1 = hexDigitValue(MSB);
  174. unsigned U2 = hexDigitValue(LSB);
  175. if (U1 == ~0U || U2 == ~0U)
  176. return false;
  177. Hex = static_cast<uint8_t>((U1 << 4) | U2);
  178. return true;
  179. }
  180. /// Return the binary representation of the two provided values, \p MSB and
  181. /// \p LSB, that make up the nibbles of a hexadecimal digit.
  182. inline uint8_t hexFromNibbles(char MSB, char LSB) {
  183. uint8_t Hex = 0;
  184. bool GotHex = tryGetHexFromNibbles(MSB, LSB, Hex);
  185. (void)GotHex;
  186. assert(GotHex && "MSB and/or LSB do not correspond to hex digits");
  187. return Hex;
  188. }
  189. /// Convert hexadecimal string \p Input to its binary representation and store
  190. /// the result in \p Output. Returns true if the binary representation could be
  191. /// converted from the hexadecimal string. Returns false if \p Input contains
  192. /// non-hexadecimal digits. The output string is half the size of \p Input.
  193. inline bool tryGetFromHex(StringRef Input, std::string &Output) {
  194. if (Input.empty())
  195. return true;
  196. // If the input string is not properly aligned on 2 nibbles we pad out the
  197. // front with a 0 prefix; e.g. `ABC` -> `0ABC`.
  198. Output.resize((Input.size() + 1) / 2);
  199. char *OutputPtr = const_cast<char *>(Output.data());
  200. if (Input.size() % 2 == 1) {
  201. uint8_t Hex = 0;
  202. if (!tryGetHexFromNibbles('0', Input.front(), Hex))
  203. return false;
  204. *OutputPtr++ = Hex;
  205. Input = Input.drop_front();
  206. }
  207. // Convert the nibble pairs (e.g. `9C`) into bytes (0x9C).
  208. // With the padding above we know the input is aligned and the output expects
  209. // exactly half as many bytes as nibbles in the input.
  210. size_t InputSize = Input.size();
  211. assert(InputSize % 2 == 0);
  212. const char *InputPtr = Input.data();
  213. for (size_t OutputIndex = 0; OutputIndex < InputSize / 2; ++OutputIndex) {
  214. uint8_t Hex = 0;
  215. if (!tryGetHexFromNibbles(InputPtr[OutputIndex * 2 + 0], // MSB
  216. InputPtr[OutputIndex * 2 + 1], // LSB
  217. Hex))
  218. return false;
  219. OutputPtr[OutputIndex] = Hex;
  220. }
  221. return true;
  222. }
  223. /// Convert hexadecimal string \p Input to its binary representation.
  224. /// The return string is half the size of \p Input.
  225. inline std::string fromHex(StringRef Input) {
  226. std::string Hex;
  227. bool GotHex = tryGetFromHex(Input, Hex);
  228. (void)GotHex;
  229. assert(GotHex && "Input contains non hex digits");
  230. return Hex;
  231. }
  232. /// Convert the string \p S to an integer of the specified type using
  233. /// the radix \p Base. If \p Base is 0, auto-detects the radix.
  234. /// Returns true if the number was successfully converted, false otherwise.
  235. template <typename N> bool to_integer(StringRef S, N &Num, unsigned Base = 0) {
  236. return !S.getAsInteger(Base, Num);
  237. }
  238. namespace detail {
  239. template <typename N>
  240. inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) {
  241. SmallString<32> Storage;
  242. StringRef S = T.toNullTerminatedStringRef(Storage);
  243. char *End;
  244. N Temp = StrTo(S.data(), &End);
  245. if (*End != '\0')
  246. return false;
  247. Num = Temp;
  248. return true;
  249. }
  250. }
  251. inline bool to_float(const Twine &T, float &Num) {
  252. return detail::to_float(T, Num, strtof);
  253. }
  254. inline bool to_float(const Twine &T, double &Num) {
  255. return detail::to_float(T, Num, strtod);
  256. }
  257. inline bool to_float(const Twine &T, long double &Num) {
  258. return detail::to_float(T, Num, strtold);
  259. }
  260. inline std::string utostr(uint64_t X, bool isNeg = false) {
  261. char Buffer[21];
  262. char *BufPtr = std::end(Buffer);
  263. if (X == 0) *--BufPtr = '0'; // Handle special case...
  264. while (X) {
  265. *--BufPtr = '0' + char(X % 10);
  266. X /= 10;
  267. }
  268. if (isNeg) *--BufPtr = '-'; // Add negative sign...
  269. return std::string(BufPtr, std::end(Buffer));
  270. }
  271. inline std::string itostr(int64_t X) {
  272. if (X < 0)
  273. return utostr(static_cast<uint64_t>(1) + ~static_cast<uint64_t>(X), true);
  274. else
  275. return utostr(static_cast<uint64_t>(X));
  276. }
  277. inline std::string toString(const APInt &I, unsigned Radix, bool Signed,
  278. bool formatAsCLiteral = false) {
  279. SmallString<40> S;
  280. I.toString(S, Radix, Signed, formatAsCLiteral);
  281. return std::string(S.str());
  282. }
  283. inline std::string toString(const APSInt &I, unsigned Radix) {
  284. return toString(I, Radix, I.isSigned());
  285. }
  286. /// StrInStrNoCase - Portable version of strcasestr. Locates the first
  287. /// occurrence of string 's1' in string 's2', ignoring case. Returns
  288. /// the offset of s2 in s1 or npos if s2 cannot be found.
  289. StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
  290. /// getToken - This function extracts one token from source, ignoring any
  291. /// leading characters that appear in the Delimiters string, and ending the
  292. /// token at any of the characters that appear in the Delimiters string. If
  293. /// there are no tokens in the source string, an empty string is returned.
  294. /// The function returns a pair containing the extracted token and the
  295. /// remaining tail string.
  296. std::pair<StringRef, StringRef> getToken(StringRef Source,
  297. StringRef Delimiters = " \t\n\v\f\r");
  298. /// SplitString - Split up the specified string according to the specified
  299. /// delimiters, appending the result fragments to the output list.
  300. void SplitString(StringRef Source,
  301. SmallVectorImpl<StringRef> &OutFragments,
  302. StringRef Delimiters = " \t\n\v\f\r");
  303. /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
  304. inline StringRef getOrdinalSuffix(unsigned Val) {
  305. // It is critically important that we do this perfectly for
  306. // user-written sequences with over 100 elements.
  307. switch (Val % 100) {
  308. case 11:
  309. case 12:
  310. case 13:
  311. return "th";
  312. default:
  313. switch (Val % 10) {
  314. case 1: return "st";
  315. case 2: return "nd";
  316. case 3: return "rd";
  317. default: return "th";
  318. }
  319. }
  320. }
  321. /// Print each character of the specified string, escaping it if it is not
  322. /// printable or if it is an escape char.
  323. void printEscapedString(StringRef Name, raw_ostream &Out);
  324. /// Print each character of the specified string, escaping HTML special
  325. /// characters.
  326. void printHTMLEscaped(StringRef String, raw_ostream &Out);
  327. /// printLowerCase - Print each character as lowercase if it is uppercase.
  328. void printLowerCase(StringRef String, raw_ostream &Out);
  329. /// Converts a string from camel-case to snake-case by replacing all uppercase
  330. /// letters with '_' followed by the letter in lowercase, except if the
  331. /// uppercase letter is the first character of the string.
  332. std::string convertToSnakeFromCamelCase(StringRef input);
  333. /// Converts a string from snake-case to camel-case by replacing all occurrences
  334. /// of '_' followed by a lowercase letter with the letter in uppercase.
  335. /// Optionally allow capitalization of the first letter (if it is a lowercase
  336. /// letter)
  337. std::string convertToCamelFromSnakeCase(StringRef input,
  338. bool capitalizeFirst = false);
  339. namespace detail {
  340. template <typename IteratorT>
  341. inline std::string join_impl(IteratorT Begin, IteratorT End,
  342. StringRef Separator, std::input_iterator_tag) {
  343. std::string S;
  344. if (Begin == End)
  345. return S;
  346. S += (*Begin);
  347. while (++Begin != End) {
  348. S += Separator;
  349. S += (*Begin);
  350. }
  351. return S;
  352. }
  353. template <typename IteratorT>
  354. inline std::string join_impl(IteratorT Begin, IteratorT End,
  355. StringRef Separator, std::forward_iterator_tag) {
  356. std::string S;
  357. if (Begin == End)
  358. return S;
  359. size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
  360. for (IteratorT I = Begin; I != End; ++I)
  361. Len += (*I).size();
  362. S.reserve(Len);
  363. size_t PrevCapacity = S.capacity();
  364. (void)PrevCapacity;
  365. S += (*Begin);
  366. while (++Begin != End) {
  367. S += Separator;
  368. S += (*Begin);
  369. }
  370. assert(PrevCapacity == S.capacity() && "String grew during building");
  371. return S;
  372. }
  373. template <typename Sep>
  374. inline void join_items_impl(std::string &Result, Sep Separator) {}
  375. template <typename Sep, typename Arg>
  376. inline void join_items_impl(std::string &Result, Sep Separator,
  377. const Arg &Item) {
  378. Result += Item;
  379. }
  380. template <typename Sep, typename Arg1, typename... Args>
  381. inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1,
  382. Args &&... Items) {
  383. Result += A1;
  384. Result += Separator;
  385. join_items_impl(Result, Separator, std::forward<Args>(Items)...);
  386. }
  387. inline size_t join_one_item_size(char) { return 1; }
  388. inline size_t join_one_item_size(const char *S) { return S ? ::strlen(S) : 0; }
  389. template <typename T> inline size_t join_one_item_size(const T &Str) {
  390. return Str.size();
  391. }
  392. template <typename... Args> inline size_t join_items_size(Args &&...Items) {
  393. return (0 + ... + join_one_item_size(std::forward<Args>(Items)));
  394. }
  395. } // end namespace detail
  396. /// Joins the strings in the range [Begin, End), adding Separator between
  397. /// the elements.
  398. template <typename IteratorT>
  399. inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
  400. using tag = typename std::iterator_traits<IteratorT>::iterator_category;
  401. return detail::join_impl(Begin, End, Separator, tag());
  402. }
  403. /// Joins the strings in the range [R.begin(), R.end()), adding Separator
  404. /// between the elements.
  405. template <typename Range>
  406. inline std::string join(Range &&R, StringRef Separator) {
  407. return join(R.begin(), R.end(), Separator);
  408. }
  409. /// Joins the strings in the parameter pack \p Items, adding \p Separator
  410. /// between the elements. All arguments must be implicitly convertible to
  411. /// std::string, or there should be an overload of std::string::operator+=()
  412. /// that accepts the argument explicitly.
  413. template <typename Sep, typename... Args>
  414. inline std::string join_items(Sep Separator, Args &&... Items) {
  415. std::string Result;
  416. if (sizeof...(Items) == 0)
  417. return Result;
  418. size_t NS = detail::join_one_item_size(Separator);
  419. size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
  420. Result.reserve(NI + (sizeof...(Items) - 1) * NS + 1);
  421. detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
  422. return Result;
  423. }
  424. /// A helper class to return the specified delimiter string after the first
  425. /// invocation of operator StringRef(). Used to generate a comma-separated
  426. /// list from a loop like so:
  427. ///
  428. /// \code
  429. /// ListSeparator LS;
  430. /// for (auto &I : C)
  431. /// OS << LS << I.getName();
  432. /// \end
  433. class ListSeparator {
  434. bool First = true;
  435. StringRef Separator;
  436. public:
  437. ListSeparator(StringRef Separator = ", ") : Separator(Separator) {}
  438. operator StringRef() {
  439. if (First) {
  440. First = false;
  441. return {};
  442. }
  443. return Separator;
  444. }
  445. };
  446. /// A forward iterator over partitions of string over a separator.
  447. class SplittingIterator
  448. : public iterator_facade_base<SplittingIterator, std::forward_iterator_tag,
  449. StringRef> {
  450. char SeparatorStorage;
  451. StringRef Current;
  452. StringRef Next;
  453. StringRef Separator;
  454. public:
  455. SplittingIterator(StringRef Str, StringRef Separator)
  456. : Next(Str), Separator(Separator) {
  457. ++*this;
  458. }
  459. SplittingIterator(StringRef Str, char Separator)
  460. : SeparatorStorage(Separator), Next(Str),
  461. Separator(&SeparatorStorage, 1) {
  462. ++*this;
  463. }
  464. SplittingIterator(const SplittingIterator &R)
  465. : SeparatorStorage(R.SeparatorStorage), Current(R.Current), Next(R.Next),
  466. Separator(R.Separator) {
  467. if (R.Separator.data() == &R.SeparatorStorage)
  468. Separator = StringRef(&SeparatorStorage, 1);
  469. }
  470. SplittingIterator &operator=(const SplittingIterator &R) {
  471. if (this == &R)
  472. return *this;
  473. SeparatorStorage = R.SeparatorStorage;
  474. Current = R.Current;
  475. Next = R.Next;
  476. Separator = R.Separator;
  477. if (R.Separator.data() == &R.SeparatorStorage)
  478. Separator = StringRef(&SeparatorStorage, 1);
  479. return *this;
  480. }
  481. bool operator==(const SplittingIterator &R) const {
  482. assert(Separator == R.Separator);
  483. return Current.data() == R.Current.data();
  484. }
  485. const StringRef &operator*() const { return Current; }
  486. StringRef &operator*() { return Current; }
  487. SplittingIterator &operator++() {
  488. std::tie(Current, Next) = Next.split(Separator);
  489. return *this;
  490. }
  491. };
  492. /// Split the specified string over a separator and return a range-compatible
  493. /// iterable over its partitions. Used to permit conveniently iterating
  494. /// over separated strings like so:
  495. ///
  496. /// \code
  497. /// for (StringRef x : llvm::split("foo,bar,baz", ","))
  498. /// ...;
  499. /// \end
  500. ///
  501. /// Note that the passed string must remain valid throuhgout lifetime
  502. /// of the iterators.
  503. inline iterator_range<SplittingIterator> split(StringRef Str, StringRef Separator) {
  504. return {SplittingIterator(Str, Separator),
  505. SplittingIterator(StringRef(), Separator)};
  506. }
  507. inline iterator_range<SplittingIterator> split(StringRef Str, char Separator) {
  508. return {SplittingIterator(Str, Separator),
  509. SplittingIterator(StringRef(), Separator)};
  510. }
  511. } // end namespace llvm
  512. #endif // LLVM_ADT_STRINGEXTRAS_H
  513. #ifdef __GNUC__
  514. #pragma GCC diagnostic pop
  515. #endif