StringExtras.h 16 KB

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