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,
333 bool UpperCase =
true,
334 bool InsertSeparators =
false) {
336 I.toString(S, Radix,
Signed, formatAsCLiteral, UpperCase, InsertSeparators);
337 return std::string(S);
340inline std::string
toString(
const APSInt &
I,
unsigned Radix) {
355std::pair<StringRef, StringRef> getToken(StringRef Source,
356 StringRef Delimiters =
" \t\n\v\f\r");
360void SplitString(StringRef Source,
361 SmallVectorImpl<StringRef> &OutFragments,
362 StringRef Delimiters =
" \t\n\v\f\r");
365inline StringRef getOrdinalSuffix(
unsigned Val) {
378 default:
return "th";
385void printEscapedString(StringRef
Name, raw_ostream &Out);
389void printHTMLEscaped(StringRef
String, raw_ostream &Out);
392void printLowerCase(StringRef
String, raw_ostream &Out);
397std::string convertToSnakeFromCamelCase(StringRef input);
403std::string convertToCamelFromSnakeCase(StringRef input,
404 bool capitalizeFirst =
false);
408template <
typename IteratorT>
409inline std::string join_impl(IteratorT Begin, IteratorT
End,
410 StringRef Separator, std::input_iterator_tag) {
416 while (++Begin !=
End) {
423template <
typename IteratorT>
424inline std::string join_impl(IteratorT Begin, IteratorT
End,
425 StringRef Separator, std::forward_iterator_tag) {
430 size_t Len = (std::distance(Begin,
End) - 1) * Separator.size();
431 for (IteratorT
I = Begin;
I !=
End; ++
I)
432 Len += StringRef(*I).size();
434 size_t PrevCapacity = S.capacity();
437 while (++Begin !=
End) {
441 assert(PrevCapacity == S.capacity() &&
"String grew during building");
445template <
typename Sep>
446inline void join_items_impl(std::string &Result, Sep Separator) {}
448template <
typename Sep,
typename Arg>
449inline void join_items_impl(std::string &Result, Sep Separator,
454template <
typename Sep,
typename Arg1,
typename...
Args>
455inline void join_items_impl(std::string &Result, Sep Separator,
const Arg1 &A1,
459 join_items_impl(Result, Separator, std::forward<Args>(Items)...);
462inline size_t join_one_item_size(
char) {
return 1; }
463inline size_t join_one_item_size(
const char *S) {
return S ? ::strlen(S) : 0; }
465template <
typename T>
inline size_t join_one_item_size(
const T &Str) {
469template <
typename...
Args>
inline size_t join_items_size(Args &&...Items) {
470 return (0 + ... + join_one_item_size(std::forward<Args>(Items)));
477template <
typename IteratorT>
478inline 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());
485template <
typename Range>
486inline std::string join(
Range &&R, StringRef Separator) {
487 return join(
R.begin(),
R.end(), Separator);
494template <
typename Sep,
typename...
Args>
495inline std::string join_items(Sep Separator, Args &&... Items) {
497 if (
sizeof...(Items) == 0)
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)...);
521 ListSeparator(StringRef Separator =
", ") : Separator(Separator) {}
522 operator StringRef() {
532class SplittingIterator
533 :
public iterator_facade_base<SplittingIterator, std::forward_iterator_tag,
535 char SeparatorStorage;
541 SplittingIterator(StringRef Str, StringRef Separator)
542 : Next(Str), Separator(Separator) {
546 SplittingIterator(StringRef Str,
char Separator)
547 : SeparatorStorage(Separator), Next(Str),
548 Separator(&SeparatorStorage, 1) {
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);
559 SplittingIterator &operator=(
const SplittingIterator &R) {
563 SeparatorStorage =
R.SeparatorStorage;
566 Separator =
R.Separator;
567 if (
R.Separator.data() == &
R.SeparatorStorage)
568 Separator = StringRef(&SeparatorStorage, 1);
572 bool operator==(
const SplittingIterator &R)
const {
573 assert(Separator ==
R.Separator);
574 return Current.data() ==
R.Current.data();
577 const StringRef &
operator*()
const {
return Current; }
579 StringRef &
operator*() {
return Current; }
581 SplittingIterator &operator++() {
582 std::tie(Current, Next) = Next.split(Separator);
598inline iterator_range<SplittingIterator>
split(StringRef Str, StringRef Separator) {
599 return {SplittingIterator(Str, Separator),
600 SplittingIterator(StringRef(), Separator)};
603inline iterator_range<SplittingIterator>
split(StringRef Str,
char Separator) {
604 return {SplittingIterator(Str, Separator),
605 SplittingIterator(StringRef(), Separator)};
static MachineBasicBlock * split(MachineBasicBlock::iterator I)
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 GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
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)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
@ Mod
The access may modify the value stored in memory.