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

source code of llvm/include/llvm/ADT/StringExtras.h