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
32namespace llvm {
33
34class raw_ostream;
35
36/// hexdigit - Return the hexadecimal character for the
37/// given number \p X (which should be less than 16).
38inline 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.
48inline std::vector<StringRef> toStringRefArray(const char *const *Strings) {
49 std::vector<StringRef> Result;
50 while (*Strings)
51 Result.push_back(x: *Strings++);
52 return Result;
53}
54
55/// Construct a string ref from a boolean.
56inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); }
57
58/// Construct a string ref from an array ref of unsigned chars.
59inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
60 return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size());
61}
62inline 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.
67template <class CharT = uint8_t>
68inline 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.
81inline 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.
106inline bool isDigit(char C) { return C >= '0' && C <= '9'; }
107
108/// Checks if character \p C is a hexadecimal numeric character.
109inline bool isHexDigit(char C) { return hexDigitValue(C) != ~0U; }
110
111/// Checks if character \p C is a lowercase letter as classified by "C" locale.
112inline 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.
115inline 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.
118inline 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.
122inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); }
123
124/// Checks whether character \p C is valid ASCII (high bit is zero).
125inline bool isASCII(char C) { return static_cast<unsigned char>(C) <= 127; }
126
127/// Checks whether all characters in S are ASCII.
128inline 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.
139inline 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.
149inline 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.
158inline 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.
164inline char toLower(char x) {
165 if (isUpper(C: x))
166 return x - 'A' + 'a';
167 return x;
168}
169
170/// Returns the corresponding uppercase character if \p x is lowercase.
171inline char toUpper(char x) {
172 if (isLower(C: x))
173 return x - 'a' + 'A';
174 return x;
175}
176
177inline std::string utohexstr(uint64_t X, bool LowerCase = false,
178 unsigned Width = 0) {
179 char Buffer[17];
180 char *BufPtr = std::end(arr&: 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(X: Mod, LowerCase);
188 X >>= 4;
189 }
190
191 return std::string(BufPtr, std::end(arr&: Buffer));
192}
193
194/// Convert buffer \p Input to its hexadecimal representation.
195/// The returned string is double the size of \p Input.
196inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
197 SmallVectorImpl<char> &Output) {
198 const size_t Length = Input.size();
199 Output.resize_for_overwrite(N: Length * 2);
200
201 for (size_t i = 0; i < Length; i++) {
202 const uint8_t c = Input[i];
203 Output[i * 2 ] = hexdigit(X: c >> 4, LowerCase);
204 Output[i * 2 + 1] = hexdigit(X: c & 15, LowerCase);
205 }
206}
207
208inline 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
214inline std::string toHex(StringRef Input, bool LowerCase = false) {
215 return toHex(Input: 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.
222inline bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex) {
223 unsigned U1 = hexDigitValue(C: MSB);
224 unsigned U2 = hexDigitValue(C: 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.
234inline 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.
246inline 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(n: (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(MSB: '0', LSB: 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(MSB: InputPtr[OutputIndex * 2 + 0], // MSB
271 LSB: 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.
281inline std::string fromHex(StringRef Input) {
282 std::string Hex;
283 bool GotHex = tryGetFromHex(Input, Output&: 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.
292template <typename N> bool to_integer(StringRef S, N &Num, unsigned Base = 0) {
293 return !S.getAsInteger(Base, Num);
294}
295
296namespace detail {
297template <typename N>
298inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) {
299 SmallString<32> Storage;
300 StringRef S = T.toNullTerminatedStringRef(Out&: 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
310inline bool to_float(const Twine &T, float &Num) {
311 return detail::to_float(T, Num, StrTo: strtof);
312}
313
314inline bool to_float(const Twine &T, double &Num) {
315 return detail::to_float(T, Num, StrTo: strtod);
316}
317
318inline bool to_float(const Twine &T, long double &Num) {
319 return detail::to_float(T, Num, StrTo: strtold);
320}
321
322inline std::string utostr(uint64_t X, bool isNeg = false) {
323 char Buffer[21];
324 char *BufPtr = std::end(arr&: 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(arr&: Buffer));
335}
336
337inline std::string itostr(int64_t X) {
338 if (X < 0)
339 return utostr(X: static_cast<uint64_t>(1) + ~static_cast<uint64_t>(X), isNeg: true);
340 else
341 return utostr(X: static_cast<uint64_t>(X));
342}
343
344inline 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(Str&: S, Radix, Signed, formatAsCLiteral, UpperCase, InsertSeparators);
350 return std::string(S);
351}
352
353inline std::string toString(const APSInt &I, unsigned Radix) {
354 return toString(I, Radix, Signed: I.isSigned());
355}
356
357/// getToken - This function extracts one token from source, ignoring any
358/// leading characters that appear in the Delimiters string, and ending the
359/// token at any of the characters that appear in the Delimiters string. If
360/// there are no tokens in the source string, an empty string is returned.
361/// The function returns a pair containing the extracted token and the
362/// remaining tail string.
363LLVM_ABI std::pair<StringRef, StringRef>
364getToken(StringRef Source, StringRef Delimiters = " \t\n\v\f\r");
365
366/// SplitString - Split up the specified string according to the specified
367/// delimiters, appending the result fragments to the output list.
368LLVM_ABI void SplitString(StringRef Source,
369 SmallVectorImpl<StringRef> &OutFragments,
370 StringRef Delimiters = " \t\n\v\f\r");
371
372/// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
373inline StringRef getOrdinalSuffix(unsigned Val) {
374 // It is critically important that we do this perfectly for
375 // user-written sequences with over 100 elements.
376 switch (Val % 100) {
377 case 11:
378 case 12:
379 case 13:
380 return "th";
381 default:
382 switch (Val % 10) {
383 case 1: return "st";
384 case 2: return "nd";
385 case 3: return "rd";
386 default: return "th";
387 }
388 }
389}
390
391/// Print each character of the specified string, escaping it if it is not
392/// printable or if it is an escape char.
393LLVM_ABI void printEscapedString(StringRef Name, raw_ostream &Out);
394
395/// Print each character of the specified string, escaping HTML special
396/// characters.
397LLVM_ABI void printHTMLEscaped(StringRef String, raw_ostream &Out);
398
399/// printLowerCase - Print each character as lowercase if it is uppercase.
400LLVM_ABI void printLowerCase(StringRef String, raw_ostream &Out);
401
402/// Converts a string from camel-case to snake-case by replacing all uppercase
403/// letters with '_' followed by the letter in lowercase, except if the
404/// uppercase letter is the first character of the string.
405LLVM_ABI std::string convertToSnakeFromCamelCase(StringRef input);
406
407/// Converts a string from snake-case to camel-case by replacing all occurrences
408/// of '_' followed by a lowercase letter with the letter in uppercase.
409/// Optionally allow capitalization of the first letter (if it is a lowercase
410/// letter)
411LLVM_ABI std::string convertToCamelFromSnakeCase(StringRef input,
412 bool capitalizeFirst = false);
413
414namespace detail {
415
416template <typename IteratorT>
417inline std::string join_impl(IteratorT Begin, IteratorT End,
418 StringRef Separator, std::input_iterator_tag) {
419 std::string S;
420 if (Begin == End)
421 return S;
422
423 S += (*Begin);
424 while (++Begin != End) {
425 S += Separator;
426 S += (*Begin);
427 }
428 return S;
429}
430
431template <typename IteratorT>
432inline std::string join_impl(IteratorT Begin, IteratorT End,
433 StringRef Separator, std::forward_iterator_tag) {
434 std::string S;
435 if (Begin == End)
436 return S;
437
438 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
439 for (IteratorT I = Begin; I != End; ++I)
440 Len += StringRef(*I).size();
441 S.reserve(res_arg: Len);
442 size_t PrevCapacity = S.capacity();
443 (void)PrevCapacity;
444 S += (*Begin);
445 while (++Begin != End) {
446 S += Separator;
447 S += (*Begin);
448 }
449 assert(PrevCapacity == S.capacity() && "String grew during building");
450 return S;
451}
452
453template <typename Sep>
454inline void join_items_impl(std::string &Result, Sep Separator) {}
455
456template <typename Sep, typename Arg>
457inline void join_items_impl(std::string &Result, Sep Separator,
458 const Arg &Item) {
459 Result += Item;
460}
461
462template <typename Sep, typename Arg1, typename... Args>
463inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1,
464 Args &&... Items) {
465 Result += A1;
466 Result += Separator;
467 join_items_impl(Result, Separator, std::forward<Args>(Items)...);
468}
469
470inline size_t join_one_item_size(char) { return 1; }
471inline size_t join_one_item_size(const char *S) { return S ? ::strlen(s: S) : 0; }
472
473template <typename T> inline size_t join_one_item_size(const T &Str) {
474 return Str.size();
475}
476
477template <typename... Args> inline size_t join_items_size(Args &&...Items) {
478 return (0 + ... + join_one_item_size(std::forward<Args>(Items)));
479}
480
481} // end namespace detail
482
483/// Joins the strings in the range [Begin, End), adding Separator between
484/// the elements.
485template <typename IteratorT>
486inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
487 using tag = typename std::iterator_traits<IteratorT>::iterator_category;
488 return detail::join_impl(Begin, End, Separator, tag());
489}
490
491/// Joins the strings in the range [R.begin(), R.end()), adding Separator
492/// between the elements.
493template <typename Range>
494inline std::string join(Range &&R, StringRef Separator) {
495 return join(R.begin(), R.end(), Separator);
496}
497
498/// Joins the strings in the parameter pack \p Items, adding \p Separator
499/// between the elements. All arguments must be implicitly convertible to
500/// std::string, or there should be an overload of std::string::operator+=()
501/// that accepts the argument explicitly.
502template <typename Sep, typename... Args>
503inline std::string join_items(Sep Separator, Args &&... Items) {
504 std::string Result;
505 if (sizeof...(Items) == 0)
506 return Result;
507
508 size_t NS = detail::join_one_item_size(Separator);
509 size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
510 Result.reserve(res_arg: NI + (sizeof...(Items) - 1) * NS + 1);
511 detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
512 return Result;
513}
514
515/// A helper class to return the specified delimiter string after the first
516/// invocation of operator StringRef(). Used to generate a comma-separated
517/// list from a loop like so:
518///
519/// \code
520/// ListSeparator LS;
521/// for (auto &I : C)
522/// OS << LS << I.getName();
523/// \endcode
524class ListSeparator {
525 bool First = true;
526 StringRef Separator;
527 StringRef Prefix;
528
529public:
530 ListSeparator(StringRef Separator = ", ", StringRef Prefix = "")
531 : Separator(Separator), Prefix(Prefix) {}
532 operator StringRef() {
533 if (First) {
534 First = false;
535 return Prefix;
536 }
537 return Separator;
538 }
539 bool unused() { return First; }
540};
541
542/// A forward iterator over partitions of string over a separator.
543class SplittingIterator
544 : public iterator_facade_base<SplittingIterator, std::forward_iterator_tag,
545 StringRef> {
546 char SeparatorStorage;
547 StringRef Current;
548 StringRef Next;
549 StringRef Separator;
550
551public:
552 SplittingIterator(StringRef Str, StringRef Separator)
553 : Next(Str), Separator(Separator) {
554 ++*this;
555 }
556
557 SplittingIterator(StringRef Str, char Separator)
558 : SeparatorStorage(Separator), Next(Str),
559 Separator(&SeparatorStorage, 1) {
560 ++*this;
561 }
562
563 SplittingIterator(const SplittingIterator &R)
564 : SeparatorStorage(R.SeparatorStorage), Current(R.Current), Next(R.Next),
565 Separator(R.Separator) {
566 if (R.Separator.data() == &R.SeparatorStorage)
567 Separator = StringRef(&SeparatorStorage, 1);
568 }
569
570 SplittingIterator &operator=(const SplittingIterator &R) {
571 if (this == &R)
572 return *this;
573
574 SeparatorStorage = R.SeparatorStorage;
575 Current = R.Current;
576 Next = R.Next;
577 Separator = R.Separator;
578 if (R.Separator.data() == &R.SeparatorStorage)
579 Separator = StringRef(&SeparatorStorage, 1);
580 return *this;
581 }
582
583 bool operator==(const SplittingIterator &R) const {
584 assert(Separator == R.Separator);
585 return Current.data() == R.Current.data();
586 }
587
588 const StringRef &operator*() const { return Current; }
589
590 StringRef &operator*() { return Current; }
591
592 SplittingIterator &operator++() {
593 std::tie(args&: Current, args&: Next) = Next.split(Separator);
594 return *this;
595 }
596};
597
598/// Split the specified string over a separator and return a range-compatible
599/// iterable over its partitions. Used to permit conveniently iterating
600/// over separated strings like so:
601///
602/// \code
603/// for (StringRef x : llvm::split("foo,bar,baz", ","))
604/// ...;
605/// \endcode
606///
607/// Note that the passed string must remain valid throughout lifetime
608/// of the iterators.
609inline iterator_range<SplittingIterator> split(StringRef Str, StringRef Separator) {
610 return {SplittingIterator(Str, Separator),
611 SplittingIterator(StringRef(), Separator)};
612}
613
614inline iterator_range<SplittingIterator> split(StringRef Str, char Separator) {
615 return {SplittingIterator(Str, Separator),
616 SplittingIterator(StringRef(), Separator)};
617}
618
619} // end namespace llvm
620
621#endif // LLVM_ADT_STRINGEXTRAS_H
622