14#ifndef LLVM_ADT_STRINGEXTRAS_H
15#define LLVM_ADT_STRINGEXTRAS_H
37inline char hexdigit(
unsigned X,
bool LowerCase =
false) {
39 static const char LUT[] =
"0123456789ABCDEF";
40 const uint8_t
Offset = LowerCase ? 32 : 0;
47inline std::vector<StringRef> toStringRefArray(
const char *
const *Strings) {
48 std::vector<StringRef>
Result;
50 Result.push_back(*Strings++);
55inline StringRef
toStringRef(
bool B) {
return StringRef(
B ?
"true" :
"false"); }
58inline StringRef
toStringRef(ArrayRef<uint8_t> Input) {
59 return StringRef(
reinterpret_cast<const char *
>(Input.begin()), Input.size());
62 return StringRef(Input.begin(), Input.size());
66template <
class CharT = u
int8_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()),
80inline unsigned hexDigitValue(
char C) {
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,
87 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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,
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,
101 return LUT[
static_cast<unsigned char>(
C)];
105inline bool isDigit(
char C) {
return C >=
'0' &&
C <=
'9'; }
108inline bool isHexDigit(
char C) {
return hexDigitValue(
C) != ~0
U; }
111inline bool isLower(
char C) {
return 'a' <=
C &&
C <=
'z'; }
114inline bool isUpper(
char C) {
return 'A' <=
C &&
C <=
'Z'; }
121inline bool isAlnum(
char C) {
return isAlpha(
C) ||
isDigit(
C); }
124inline bool isASCII(
char C) {
return static_cast<unsigned char>(
C) <= 127; }
139 unsigned char UC =
static_cast<unsigned char>(
C);
140 return (0x20 <= UC) && (UC <= 0x7E);
146inline bool isSpace(
char C) {
147 return C ==
' ' ||
C ==
'\f' ||
C ==
'\n' ||
C ==
'\r' ||
C ==
'\t' ||
152inline char toLower(
char x) {
154 return x -
'A' +
'a';
159inline char toUpper(
char x) {
161 return x -
'a' +
'A';
165inline std::string utohexstr(
uint64_t X,
bool LowerCase =
false,
166 unsigned Width = 0) {
168 char *BufPtr = std::end(Buffer);
170 if (
X == 0) *--BufPtr =
'0';
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);
178 return std::string(BufPtr, std::end(Buffer));
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(
Length * 2);
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);
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);
201inline std::string toHex(StringRef Input,
bool LowerCase =
false) {
202 return toHex(arrayRefFromStringRef(Input), LowerCase);
209inline 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)
215 Hex =
static_cast<uint8_t
>((U1 << 4) | U2);
221inline uint8_t hexFromNibbles(
char MSB,
char LSB) {
223 bool GotHex = tryGetHexFromNibbles(MSB, LSB, Hex);
225 assert(GotHex &&
"MSB and/or LSB do not correspond to hex digits");
233inline bool tryGetFromHex(StringRef Input, std::string &Output) {
239 Output.resize((Input.size() + 1) / 2);
240 char *OutputPtr =
const_cast<char *
>(Output.data());
241 if (Input.size() % 2 == 1) {
243 if (!tryGetHexFromNibbles(
'0', Input.front(), Hex))
246 Input = Input.drop_front();
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) {
257 if (!tryGetHexFromNibbles(InputPtr[OutputIndex * 2 + 0],
258 InputPtr[OutputIndex * 2 + 1],
261 OutputPtr[OutputIndex] = Hex;
268inline std::string fromHex(StringRef Input) {
270 bool GotHex = tryGetFromHex(Input, Hex);
272 assert(GotHex &&
"Input contains non hex digits");
279template <
typename N>
bool to_integer(StringRef S,
N &Num,
unsigned Base = 0) {
280 return !S.getAsInteger(
Base, Num);
285inline bool to_float(
const Twine &
T,
N &Num,
N (*StrTo)(
const char *,
char **)) {
286 SmallString<32> Storage;
287 StringRef S =
T.toNullTerminatedStringRef(Storage);
289 N Temp = StrTo(S.data(), &
End);
297inline bool to_float(
const Twine &
T,
float &Num) {
298 return detail::to_float(
T, Num, strtof);
301inline bool to_float(
const Twine &
T,
double &Num) {
302 return detail::to_float(
T, Num, strtod);
305inline bool to_float(
const Twine &
T,
long double &Num) {
306 return detail::to_float(
T, Num, strtold);
311 char *BufPtr = std::end(Buffer);
313 if (
X == 0) *--BufPtr =
'0';
316 *--BufPtr =
'0' +
char(
X % 10);
320 if (
isNeg) *--BufPtr =
'-';
321 return std::string(BufPtr, std::end(Buffer));
324inline std::string itostr(int64_t
X) {
331inline std::string
toString(
const APInt &
I,
unsigned Radix,
bool Signed,
332 bool formatAsCLiteral =
false) {
334 I.toString(S, Radix,
Signed, formatAsCLiteral);
335 return std::string(S.str());
338inline std::string
toString(
const APSInt &
I,
unsigned Radix) {
353std::pair<StringRef, StringRef> getToken(StringRef Source,
354 StringRef Delimiters =
" \t\n\v\f\r");
358void SplitString(StringRef Source,
359 SmallVectorImpl<StringRef> &OutFragments,
360 StringRef Delimiters =
" \t\n\v\f\r");
363inline StringRef getOrdinalSuffix(
unsigned Val) {
376 default:
return "th";
383void printEscapedString(StringRef
Name, raw_ostream &Out);
387void printHTMLEscaped(StringRef
String, raw_ostream &Out);
390void printLowerCase(StringRef
String, raw_ostream &Out);
395std::string convertToSnakeFromCamelCase(StringRef input);
401std::string convertToCamelFromSnakeCase(StringRef input,
402 bool capitalizeFirst =
false);
406template <
typename IteratorT>
407inline std::string join_impl(IteratorT Begin, IteratorT
End,
408 StringRef Separator, std::input_iterator_tag) {
414 while (++Begin !=
End) {
421template <
typename IteratorT>
422inline std::string join_impl(IteratorT Begin, IteratorT
End,
423 StringRef Separator, std::forward_iterator_tag) {
428 size_t Len = (std::distance(Begin,
End) - 1) * Separator.size();
429 for (IteratorT
I = Begin;
I !=
End; ++
I)
430 Len += StringRef(*I).size();
432 size_t PrevCapacity = S.capacity();
435 while (++Begin !=
End) {
439 assert(PrevCapacity == S.capacity() &&
"String grew during building");
443template <
typename Sep>
444inline void join_items_impl(std::string &Result, Sep Separator) {}
446template <
typename Sep,
typename Arg>
447inline void join_items_impl(std::string &Result, Sep Separator,
452template <
typename Sep,
typename Arg1,
typename...
Args>
453inline void join_items_impl(std::string &Result, Sep Separator,
const Arg1 &A1,
457 join_items_impl(Result, Separator, std::forward<Args>(Items)...);
460inline size_t join_one_item_size(
char) {
return 1; }
461inline size_t join_one_item_size(
const char *S) {
return S ? ::strlen(S) : 0; }
463template <
typename T>
inline size_t join_one_item_size(
const T &Str) {
467template <
typename...
Args>
inline size_t join_items_size(Args &&...Items) {
468 return (0 + ... + join_one_item_size(std::forward<Args>(Items)));
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());
483template <
typename Range>
484inline std::string join(Range &&R, StringRef Separator) {
485 return join(
R.begin(),
R.end(), Separator);
492template <
typename Sep,
typename...
Args>
493inline std::string join_items(Sep Separator, Args &&... Items) {
495 if (
sizeof...(Items) == 0)
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(NI + (
sizeof...(Items) - 1) * NS + 1);
501 detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
519 ListSeparator(StringRef Separator =
", ") : Separator(Separator) {}
520 operator StringRef() {
530class SplittingIterator
531 :
public iterator_facade_base<SplittingIterator, std::forward_iterator_tag,
533 char SeparatorStorage;
539 SplittingIterator(StringRef Str, StringRef Separator)
540 : Next(Str), Separator(Separator) {
544 SplittingIterator(StringRef Str,
char Separator)
545 : SeparatorStorage(Separator), Next(Str),
546 Separator(&SeparatorStorage, 1) {
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);
557 SplittingIterator &operator=(
const SplittingIterator &R) {
561 SeparatorStorage =
R.SeparatorStorage;
564 Separator =
R.Separator;
565 if (
R.Separator.data() == &
R.SeparatorStorage)
566 Separator = StringRef(&SeparatorStorage, 1);
570 bool operator==(
const SplittingIterator &R)
const {
571 assert(Separator ==
R.Separator);
572 return Current.data() ==
R.Current.data();
575 const StringRef &
operator*()
const {
return Current; }
577 StringRef &
operator*() {
return Current; }
579 SplittingIterator &operator++() {
580 std::tie(Current, Next) = Next.split(Separator);
596inline iterator_range<SplittingIterator>
split(StringRef Str, StringRef Separator) {
597 return {SplittingIterator(Str, Separator),
598 SplittingIterator(StringRef(), Separator)};
601inline iterator_range<SplittingIterator>
split(StringRef Str,
char Separator) {
602 return {SplittingIterator(Str, Separator),
603 SplittingIterator(StringRef(), Separator)};
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
static Error split(StringRef Str, char Separator, std::pair< StringRef, StringRef > &Split)
Checked version of split, to ensure mandatory subparts.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool operator==(const HTTPRequest &A, const HTTPRequest &B)
static bool isDigit(const char C)
static bool isHexDigit(const char C)
static bool isLower(const char C)
static bool isUpper(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
StringRef - Represent a constant reference to a string, i.e.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
APInt operator*(APInt a, uint64_t RHS)
@ Mod
The access may modify the value stored in memory.