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