Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:09

0001 //===- llvm/ADT/StringExtras.h - Useful string functions --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains some functions that are useful when dealing with strings.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ADT_STRINGEXTRAS_H
0015 #define LLVM_ADT_STRINGEXTRAS_H
0016 
0017 #include "llvm/ADT/APSInt.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/SmallString.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/ADT/Twine.h"
0022 #include <cassert>
0023 #include <cstddef>
0024 #include <cstdint>
0025 #include <cstdlib>
0026 #include <cstring>
0027 #include <iterator>
0028 #include <string>
0029 #include <utility>
0030 
0031 namespace llvm {
0032 
0033 class raw_ostream;
0034 
0035 /// hexdigit - Return the hexadecimal character for the
0036 /// given number \p X (which should be less than 16).
0037 inline char hexdigit(unsigned X, bool LowerCase = false) {
0038   assert(X < 16);
0039   static const char LUT[] = "0123456789ABCDEF";
0040   const uint8_t Offset = LowerCase ? 32 : 0;
0041   return LUT[X] | Offset;
0042 }
0043 
0044 /// Given an array of c-style strings terminated by a null pointer, construct
0045 /// a vector of StringRefs representing the same strings without the terminating
0046 /// null string.
0047 inline std::vector<StringRef> toStringRefArray(const char *const *Strings) {
0048   std::vector<StringRef> Result;
0049   while (*Strings)
0050     Result.push_back(*Strings++);
0051   return Result;
0052 }
0053 
0054 /// Construct a string ref from a boolean.
0055 inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); }
0056 
0057 /// Construct a string ref from an array ref of unsigned chars.
0058 inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
0059   return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size());
0060 }
0061 inline StringRef toStringRef(ArrayRef<char> Input) {
0062   return StringRef(Input.begin(), Input.size());
0063 }
0064 
0065 /// Construct a string ref from an array ref of unsigned chars.
0066 template <class CharT = uint8_t>
0067 inline ArrayRef<CharT> arrayRefFromStringRef(StringRef Input) {
0068   static_assert(std::is_same<CharT, char>::value ||
0069                     std::is_same<CharT, unsigned char>::value ||
0070                     std::is_same<CharT, signed char>::value,
0071                 "Expected byte type");
0072   return ArrayRef<CharT>(reinterpret_cast<const CharT *>(Input.data()),
0073                          Input.size());
0074 }
0075 
0076 /// Interpret the given character \p C as a hexadecimal digit and return its
0077 /// value.
0078 ///
0079 /// If \p C is not a valid hex digit, -1U is returned.
0080 inline unsigned hexDigitValue(char C) {
0081   /* clang-format off */
0082   static const int16_t LUT[256] = {
0083     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0084     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0085     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0086      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,  // '0'..'9'
0087     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 'A'..'F'
0088     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0089     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 'a'..'f'
0090     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0091     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0092     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0093     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0094     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0095     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0096     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0097     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0098     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0099   };
0100   /* clang-format on */
0101   return LUT[static_cast<unsigned char>(C)];
0102 }
0103 
0104 /// Checks if character \p C is one of the 10 decimal digits.
0105 inline bool isDigit(char C) { return C >= '0' && C <= '9'; }
0106 
0107 /// Checks if character \p C is a hexadecimal numeric character.
0108 inline bool isHexDigit(char C) { return hexDigitValue(C) != ~0U; }
0109 
0110 /// Checks if character \p C is a lowercase letter as classified by "C" locale.
0111 inline bool isLower(char C) { return 'a' <= C && C <= 'z'; }
0112 
0113 /// Checks if character \p C is a uppercase letter as classified by "C" locale.
0114 inline bool isUpper(char C) { return 'A' <= C && C <= 'Z'; }
0115 
0116 /// Checks if character \p C is a valid letter as classified by "C" locale.
0117 inline bool isAlpha(char C) { return isLower(C) || isUpper(C); }
0118 
0119 /// Checks whether character \p C is either a decimal digit or an uppercase or
0120 /// lowercase letter as classified by "C" locale.
0121 inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); }
0122 
0123 /// Checks whether character \p C is valid ASCII (high bit is zero).
0124 inline bool isASCII(char C) { return static_cast<unsigned char>(C) <= 127; }
0125 
0126 /// Checks whether all characters in S are ASCII.
0127 inline bool isASCII(llvm::StringRef S) {
0128   for (char C : S)
0129     if (LLVM_UNLIKELY(!isASCII(C)))
0130       return false;
0131   return true;
0132 }
0133 
0134 /// Checks whether character \p C is printable.
0135 ///
0136 /// Locale-independent version of the C standard library isprint whose results
0137 /// may differ on different platforms.
0138 inline bool isPrint(char C) {
0139   unsigned char UC = static_cast<unsigned char>(C);
0140   return (0x20 <= UC) && (UC <= 0x7E);
0141 }
0142 
0143 /// Checks whether character \p C is a punctuation character.
0144 ///
0145 /// Locale-independent version of the C standard library ispunct. The list of
0146 /// punctuation characters can be found in the documentation of std::ispunct:
0147 /// https://en.cppreference.com/w/cpp/string/byte/ispunct.
0148 inline bool isPunct(char C) {
0149   static constexpr StringLiteral Punctuations =
0150       R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)";
0151   return Punctuations.contains(C);
0152 }
0153 
0154 /// Checks whether character \p C is whitespace in the "C" locale.
0155 ///
0156 /// Locale-independent version of the C standard library isspace.
0157 inline bool isSpace(char C) {
0158   return C == ' ' || C == '\f' || C == '\n' || C == '\r' || C == '\t' ||
0159          C == '\v';
0160 }
0161 
0162 /// Returns the corresponding lowercase character if \p x is uppercase.
0163 inline char toLower(char x) {
0164   if (isUpper(x))
0165     return x - 'A' + 'a';
0166   return x;
0167 }
0168 
0169 /// Returns the corresponding uppercase character if \p x is lowercase.
0170 inline char toUpper(char x) {
0171   if (isLower(x))
0172     return x - 'a' + 'A';
0173   return x;
0174 }
0175 
0176 inline std::string utohexstr(uint64_t X, bool LowerCase = false,
0177                              unsigned Width = 0) {
0178   char Buffer[17];
0179   char *BufPtr = std::end(Buffer);
0180 
0181   if (X == 0) *--BufPtr = '0';
0182 
0183   for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
0184     unsigned char Mod = static_cast<unsigned char>(X) & 15;
0185     *--BufPtr = hexdigit(Mod, LowerCase);
0186     X >>= 4;
0187   }
0188 
0189   return std::string(BufPtr, std::end(Buffer));
0190 }
0191 
0192 /// Convert buffer \p Input to its hexadecimal representation.
0193 /// The returned string is double the size of \p Input.
0194 inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
0195                   SmallVectorImpl<char> &Output) {
0196   const size_t Length = Input.size();
0197   Output.resize_for_overwrite(Length * 2);
0198 
0199   for (size_t i = 0; i < Length; i++) {
0200     const uint8_t c = Input[i];
0201     Output[i * 2    ] = hexdigit(c >> 4, LowerCase);
0202     Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);
0203   }
0204 }
0205 
0206 inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
0207   SmallString<16> Output;
0208   toHex(Input, LowerCase, Output);
0209   return std::string(Output);
0210 }
0211 
0212 inline std::string toHex(StringRef Input, bool LowerCase = false) {
0213   return toHex(arrayRefFromStringRef(Input), LowerCase);
0214 }
0215 
0216 /// Store the binary representation of the two provided values, \p MSB and
0217 /// \p LSB, that make up the nibbles of a hexadecimal digit. If \p MSB or \p LSB
0218 /// do not correspond to proper nibbles of a hexadecimal digit, this method
0219 /// returns false. Otherwise, returns true.
0220 inline bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex) {
0221   unsigned U1 = hexDigitValue(MSB);
0222   unsigned U2 = hexDigitValue(LSB);
0223   if (U1 == ~0U || U2 == ~0U)
0224     return false;
0225 
0226   Hex = static_cast<uint8_t>((U1 << 4) | U2);
0227   return true;
0228 }
0229 
0230 /// Return the binary representation of the two provided values, \p MSB and
0231 /// \p LSB, that make up the nibbles of a hexadecimal digit.
0232 inline uint8_t hexFromNibbles(char MSB, char LSB) {
0233   uint8_t Hex = 0;
0234   bool GotHex = tryGetHexFromNibbles(MSB, LSB, Hex);
0235   (void)GotHex;
0236   assert(GotHex && "MSB and/or LSB do not correspond to hex digits");
0237   return Hex;
0238 }
0239 
0240 /// Convert hexadecimal string \p Input to its binary representation and store
0241 /// the result in \p Output. Returns true if the binary representation could be
0242 /// converted from the hexadecimal string. Returns false if \p Input contains
0243 /// non-hexadecimal digits. The output string is half the size of \p Input.
0244 inline bool tryGetFromHex(StringRef Input, std::string &Output) {
0245   if (Input.empty())
0246     return true;
0247 
0248   // If the input string is not properly aligned on 2 nibbles we pad out the
0249   // front with a 0 prefix; e.g. `ABC` -> `0ABC`.
0250   Output.resize((Input.size() + 1) / 2);
0251   char *OutputPtr = const_cast<char *>(Output.data());
0252   if (Input.size() % 2 == 1) {
0253     uint8_t Hex = 0;
0254     if (!tryGetHexFromNibbles('0', Input.front(), Hex))
0255       return false;
0256     *OutputPtr++ = Hex;
0257     Input = Input.drop_front();
0258   }
0259 
0260   // Convert the nibble pairs (e.g. `9C`) into bytes (0x9C).
0261   // With the padding above we know the input is aligned and the output expects
0262   // exactly half as many bytes as nibbles in the input.
0263   size_t InputSize = Input.size();
0264   assert(InputSize % 2 == 0);
0265   const char *InputPtr = Input.data();
0266   for (size_t OutputIndex = 0; OutputIndex < InputSize / 2; ++OutputIndex) {
0267     uint8_t Hex = 0;
0268     if (!tryGetHexFromNibbles(InputPtr[OutputIndex * 2 + 0], // MSB
0269                               InputPtr[OutputIndex * 2 + 1], // LSB
0270                               Hex))
0271       return false;
0272     OutputPtr[OutputIndex] = Hex;
0273   }
0274   return true;
0275 }
0276 
0277 /// Convert hexadecimal string \p Input to its binary representation.
0278 /// The return string is half the size of \p Input.
0279 inline std::string fromHex(StringRef Input) {
0280   std::string Hex;
0281   bool GotHex = tryGetFromHex(Input, Hex);
0282   (void)GotHex;
0283   assert(GotHex && "Input contains non hex digits");
0284   return Hex;
0285 }
0286 
0287 /// Convert the string \p S to an integer of the specified type using
0288 /// the radix \p Base.  If \p Base is 0, auto-detects the radix.
0289 /// Returns true if the number was successfully converted, false otherwise.
0290 template <typename N> bool to_integer(StringRef S, N &Num, unsigned Base = 0) {
0291   return !S.getAsInteger(Base, Num);
0292 }
0293 
0294 namespace detail {
0295 template <typename N>
0296 inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) {
0297   SmallString<32> Storage;
0298   StringRef S = T.toNullTerminatedStringRef(Storage);
0299   char *End;
0300   N Temp = StrTo(S.data(), &End);
0301   if (*End != '\0')
0302     return false;
0303   Num = Temp;
0304   return true;
0305 }
0306 }
0307 
0308 inline bool to_float(const Twine &T, float &Num) {
0309   return detail::to_float(T, Num, strtof);
0310 }
0311 
0312 inline bool to_float(const Twine &T, double &Num) {
0313   return detail::to_float(T, Num, strtod);
0314 }
0315 
0316 inline bool to_float(const Twine &T, long double &Num) {
0317   return detail::to_float(T, Num, strtold);
0318 }
0319 
0320 inline std::string utostr(uint64_t X, bool isNeg = false) {
0321   char Buffer[21];
0322   char *BufPtr = std::end(Buffer);
0323 
0324   if (X == 0) *--BufPtr = '0';  // Handle special case...
0325 
0326   while (X) {
0327     *--BufPtr = '0' + char(X % 10);
0328     X /= 10;
0329   }
0330 
0331   if (isNeg) *--BufPtr = '-';   // Add negative sign...
0332   return std::string(BufPtr, std::end(Buffer));
0333 }
0334 
0335 inline std::string itostr(int64_t X) {
0336   if (X < 0)
0337     return utostr(static_cast<uint64_t>(1) + ~static_cast<uint64_t>(X), true);
0338   else
0339     return utostr(static_cast<uint64_t>(X));
0340 }
0341 
0342 inline std::string toString(const APInt &I, unsigned Radix, bool Signed,
0343                             bool formatAsCLiteral = false,
0344                             bool UpperCase = true,
0345                             bool InsertSeparators = false) {
0346   SmallString<40> S;
0347   I.toString(S, Radix, Signed, formatAsCLiteral, UpperCase, InsertSeparators);
0348   return std::string(S);
0349 }
0350 
0351 inline std::string toString(const APSInt &I, unsigned Radix) {
0352   return toString(I, Radix, I.isSigned());
0353 }
0354 
0355 /// StrInStrNoCase - Portable version of strcasestr.  Locates the first
0356 /// occurrence of string 's1' in string 's2', ignoring case.  Returns
0357 /// the offset of s2 in s1 or npos if s2 cannot be found.
0358 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
0359 
0360 /// getToken - This function extracts one token from source, ignoring any
0361 /// leading characters that appear in the Delimiters string, and ending the
0362 /// token at any of the characters that appear in the Delimiters string.  If
0363 /// there are no tokens in the source string, an empty string is returned.
0364 /// The function returns a pair containing the extracted token and the
0365 /// remaining tail string.
0366 std::pair<StringRef, StringRef> getToken(StringRef Source,
0367                                          StringRef Delimiters = " \t\n\v\f\r");
0368 
0369 /// SplitString - Split up the specified string according to the specified
0370 /// delimiters, appending the result fragments to the output list.
0371 void SplitString(StringRef Source,
0372                  SmallVectorImpl<StringRef> &OutFragments,
0373                  StringRef Delimiters = " \t\n\v\f\r");
0374 
0375 /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
0376 inline StringRef getOrdinalSuffix(unsigned Val) {
0377   // It is critically important that we do this perfectly for
0378   // user-written sequences with over 100 elements.
0379   switch (Val % 100) {
0380   case 11:
0381   case 12:
0382   case 13:
0383     return "th";
0384   default:
0385     switch (Val % 10) {
0386       case 1: return "st";
0387       case 2: return "nd";
0388       case 3: return "rd";
0389       default: return "th";
0390     }
0391   }
0392 }
0393 
0394 /// Print each character of the specified string, escaping it if it is not
0395 /// printable or if it is an escape char.
0396 void printEscapedString(StringRef Name, raw_ostream &Out);
0397 
0398 /// Print each character of the specified string, escaping HTML special
0399 /// characters.
0400 void printHTMLEscaped(StringRef String, raw_ostream &Out);
0401 
0402 /// printLowerCase - Print each character as lowercase if it is uppercase.
0403 void printLowerCase(StringRef String, raw_ostream &Out);
0404 
0405 /// Converts a string from camel-case to snake-case by replacing all uppercase
0406 /// letters with '_' followed by the letter in lowercase, except if the
0407 /// uppercase letter is the first character of the string.
0408 std::string convertToSnakeFromCamelCase(StringRef input);
0409 
0410 /// Converts a string from snake-case to camel-case by replacing all occurrences
0411 /// of '_' followed by a lowercase letter with the letter in uppercase.
0412 /// Optionally allow capitalization of the first letter (if it is a lowercase
0413 /// letter)
0414 std::string convertToCamelFromSnakeCase(StringRef input,
0415                                         bool capitalizeFirst = false);
0416 
0417 namespace detail {
0418 
0419 template <typename IteratorT>
0420 inline std::string join_impl(IteratorT Begin, IteratorT End,
0421                              StringRef Separator, std::input_iterator_tag) {
0422   std::string S;
0423   if (Begin == End)
0424     return S;
0425 
0426   S += (*Begin);
0427   while (++Begin != End) {
0428     S += Separator;
0429     S += (*Begin);
0430   }
0431   return S;
0432 }
0433 
0434 template <typename IteratorT>
0435 inline std::string join_impl(IteratorT Begin, IteratorT End,
0436                              StringRef Separator, std::forward_iterator_tag) {
0437   std::string S;
0438   if (Begin == End)
0439     return S;
0440 
0441   size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
0442   for (IteratorT I = Begin; I != End; ++I)
0443     Len += StringRef(*I).size();
0444   S.reserve(Len);
0445   size_t PrevCapacity = S.capacity();
0446   (void)PrevCapacity;
0447   S += (*Begin);
0448   while (++Begin != End) {
0449     S += Separator;
0450     S += (*Begin);
0451   }
0452   assert(PrevCapacity == S.capacity() && "String grew during building");
0453   return S;
0454 }
0455 
0456 template <typename Sep>
0457 inline void join_items_impl(std::string &Result, Sep Separator) {}
0458 
0459 template <typename Sep, typename Arg>
0460 inline void join_items_impl(std::string &Result, Sep Separator,
0461                             const Arg &Item) {
0462   Result += Item;
0463 }
0464 
0465 template <typename Sep, typename Arg1, typename... Args>
0466 inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1,
0467                             Args &&... Items) {
0468   Result += A1;
0469   Result += Separator;
0470   join_items_impl(Result, Separator, std::forward<Args>(Items)...);
0471 }
0472 
0473 inline size_t join_one_item_size(char) { return 1; }
0474 inline size_t join_one_item_size(const char *S) { return S ? ::strlen(S) : 0; }
0475 
0476 template <typename T> inline size_t join_one_item_size(const T &Str) {
0477   return Str.size();
0478 }
0479 
0480 template <typename... Args> inline size_t join_items_size(Args &&...Items) {
0481   return (0 + ... + join_one_item_size(std::forward<Args>(Items)));
0482 }
0483 
0484 } // end namespace detail
0485 
0486 /// Joins the strings in the range [Begin, End), adding Separator between
0487 /// the elements.
0488 template <typename IteratorT>
0489 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
0490   using tag = typename std::iterator_traits<IteratorT>::iterator_category;
0491   return detail::join_impl(Begin, End, Separator, tag());
0492 }
0493 
0494 /// Joins the strings in the range [R.begin(), R.end()), adding Separator
0495 /// between the elements.
0496 template <typename Range>
0497 inline std::string join(Range &&R, StringRef Separator) {
0498   return join(R.begin(), R.end(), Separator);
0499 }
0500 
0501 /// Joins the strings in the parameter pack \p Items, adding \p Separator
0502 /// between the elements.  All arguments must be implicitly convertible to
0503 /// std::string, or there should be an overload of std::string::operator+=()
0504 /// that accepts the argument explicitly.
0505 template <typename Sep, typename... Args>
0506 inline std::string join_items(Sep Separator, Args &&... Items) {
0507   std::string Result;
0508   if (sizeof...(Items) == 0)
0509     return Result;
0510 
0511   size_t NS = detail::join_one_item_size(Separator);
0512   size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
0513   Result.reserve(NI + (sizeof...(Items) - 1) * NS + 1);
0514   detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
0515   return Result;
0516 }
0517 
0518 /// A helper class to return the specified delimiter string after the first
0519 /// invocation of operator StringRef().  Used to generate a comma-separated
0520 /// list from a loop like so:
0521 ///
0522 /// \code
0523 ///   ListSeparator LS;
0524 ///   for (auto &I : C)
0525 ///     OS << LS << I.getName();
0526 /// \end
0527 class ListSeparator {
0528   bool First = true;
0529   StringRef Separator;
0530 
0531 public:
0532   ListSeparator(StringRef Separator = ", ") : Separator(Separator) {}
0533   operator StringRef() {
0534     if (First) {
0535       First = false;
0536       return {};
0537     }
0538     return Separator;
0539   }
0540 };
0541 
0542 /// A forward iterator over partitions of string over a separator.
0543 class SplittingIterator
0544     : public iterator_facade_base<SplittingIterator, std::forward_iterator_tag,
0545                                   StringRef> {
0546   char SeparatorStorage;
0547   StringRef Current;
0548   StringRef Next;
0549   StringRef Separator;
0550 
0551 public:
0552   SplittingIterator(StringRef Str, StringRef Separator)
0553       : Next(Str), Separator(Separator) {
0554     ++*this;
0555   }
0556 
0557   SplittingIterator(StringRef Str, char Separator)
0558       : SeparatorStorage(Separator), Next(Str),
0559         Separator(&SeparatorStorage, 1) {
0560     ++*this;
0561   }
0562 
0563   SplittingIterator(const SplittingIterator &R)
0564       : SeparatorStorage(R.SeparatorStorage), Current(R.Current), Next(R.Next),
0565         Separator(R.Separator) {
0566     if (R.Separator.data() == &R.SeparatorStorage)
0567       Separator = StringRef(&SeparatorStorage, 1);
0568   }
0569 
0570   SplittingIterator &operator=(const SplittingIterator &R) {
0571     if (this == &R)
0572       return *this;
0573 
0574     SeparatorStorage = R.SeparatorStorage;
0575     Current = R.Current;
0576     Next = R.Next;
0577     Separator = R.Separator;
0578     if (R.Separator.data() == &R.SeparatorStorage)
0579       Separator = StringRef(&SeparatorStorage, 1);
0580     return *this;
0581   }
0582 
0583   bool operator==(const SplittingIterator &R) const {
0584     assert(Separator == R.Separator);
0585     return Current.data() == R.Current.data();
0586   }
0587 
0588   const StringRef &operator*() const { return Current; }
0589 
0590   StringRef &operator*() { return Current; }
0591 
0592   SplittingIterator &operator++() {
0593     std::tie(Current, Next) = Next.split(Separator);
0594     return *this;
0595   }
0596 };
0597 
0598 /// Split the specified string over a separator and return a range-compatible
0599 /// iterable over its partitions.  Used to permit conveniently iterating
0600 /// over separated strings like so:
0601 ///
0602 /// \code
0603 ///   for (StringRef x : llvm::split("foo,bar,baz", ","))
0604 ///     ...;
0605 /// \end
0606 ///
0607 /// Note that the passed string must remain valid throuhgout lifetime
0608 /// of the iterators.
0609 inline iterator_range<SplittingIterator> split(StringRef Str, StringRef Separator) {
0610   return {SplittingIterator(Str, Separator),
0611           SplittingIterator(StringRef(), Separator)};
0612 }
0613 
0614 inline iterator_range<SplittingIterator> split(StringRef Str, char Separator) {
0615   return {SplittingIterator(Str, Separator),
0616           SplittingIterator(StringRef(), Separator)};
0617 }
0618 
0619 } // end namespace llvm
0620 
0621 #endif // LLVM_ADT_STRINGEXTRAS_H