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