14#ifndef LLVM_ADT_STRINGEXTRAS_H
15#define LLVM_ADT_STRINGEXTRAS_H
38inline char hexdigit(
unsigned X,
bool LowerCase =
false) {
40 static const char LUT[] =
"0123456789ABCDEF";
49 std::vector<StringRef> Result;
51 Result.push_back(*Strings++);
67template <
class CharT = u
int8_t>
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");
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,
88 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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,
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,
102 return LUT[
static_cast<unsigned char>(
C)];
125inline bool isASCII(
char C) {
return static_cast<unsigned char>(
C) <= 127; }
140 unsigned char UC =
static_cast<unsigned char>(
C);
141 return (0x20 <= UC) && (UC <= 0x7E);
151 R
"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)";
159 return C ==
' ' ||
C ==
'\f' ||
C ==
'\n' ||
C ==
'\r' ||
C ==
'\t' ||
166 return x -
'A' +
'a';
173 return x -
'a' +
'A';
178 unsigned Width = 0) {
180 char *BufPtr = std::end(Buffer);
182 if (
X == 0 && !Width)
185 for (
unsigned i = 0; Width ? (i < Width) :
X; ++i) {
186 unsigned char Mod =
static_cast<unsigned char>(
X) & 15;
191 return std::string(BufPtr, std::end(Buffer));
201 for (
size_t i = 0; i <
Length; i++) {
203 Output[i * 2 ] =
hexdigit(c >> 4, LowerCase);
204 Output[i * 2 + 1] =
hexdigit(c & 15, LowerCase);
211 return std::string(Output);
225 if (U1 == ~0U || U2 == ~0U)
228 Hex =
static_cast<uint8_t>((U1 << 4) | U2);
238 assert(GotHex &&
"MSB and/or LSB do not correspond to hex digits");
252 Output.resize((
Input.size() + 1) / 2);
253 char *OutputPtr =
const_cast<char *
>(Output.data());
254 if (
Input.size() % 2 == 1) {
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) {
271 InputPtr[OutputIndex * 2 + 1],
274 OutputPtr[OutputIndex] = Hex;
285 assert(GotHex &&
"Input contains non hex digits");
300 StringRef S =
T.toNullTerminatedStringRef(Storage);
302 N Temp = StrTo(S.
data(), &End);
324 char *BufPtr = std::end(Buffer);
326 if (
X == 0) *--BufPtr =
'0';
329 *--BufPtr =
'0' +
char(
X % 10);
333 if (
isNeg) *--BufPtr =
'-';
334 return std::string(BufPtr, std::end(Buffer));
345 bool formatAsCLiteral =
false,
346 bool UpperCase =
true,
347 bool InsertSeparators =
false) {
349 I.toString(S, Radix,
Signed, formatAsCLiteral, UpperCase, InsertSeparators);
350 return std::string(S);
363LLVM_ABI std::pair<StringRef, StringRef>
364getToken(StringRef Source, StringRef Delimiters =
" \t\n\v\f\r");
369 SmallVectorImpl<StringRef> &OutFragments,
370 StringRef Delimiters =
" \t\n\v\f\r");
386 default:
return "th";
412 bool capitalizeFirst =
false);
416template <
typename IteratorT>
417inline std::string
join_impl(IteratorT Begin, IteratorT End,
418 StringRef Separator, std::input_iterator_tag) {
424 while (++Begin != End) {
431template <
typename IteratorT>
432inline std::string
join_impl(IteratorT Begin, IteratorT End,
433 StringRef Separator, std::forward_iterator_tag) {
438 size_t Len = (std::distance(Begin, End) - 1) * Separator.
size();
439 for (IteratorT
I = Begin;
I != End; ++
I)
442 size_t PrevCapacity = S.capacity();
445 while (++Begin != End) {
449 assert(PrevCapacity == S.capacity() &&
"String grew during building");
453template <
typename Sep>
456template <
typename Sep,
typename Arg>
462template <
typename Sep,
typename Arg1,
typename... Args>
485template <
typename IteratorT>
486inline std::string
join(IteratorT Begin, IteratorT End,
StringRef Separator) {
487 using tag =
typename std::iterator_traits<IteratorT>::iterator_category;
493template <
typename Range>
495 return join(R.begin(), R.end(), Separator);
502template <
typename Sep,
typename... Args>
503inline std::string
join_items(Sep Separator, Args &&... Items) {
505 if (
sizeof...(Items) == 0)
510 Result.reserve(NI + (
sizeof...(Items) - 1) * NS + 1);
531 : Separator(Separator), Prefix(Prefix) {}
546 char SeparatorStorage;
553 : Next(Str), Separator(Separator) {
558 : SeparatorStorage(Separator), Next(Str),
559 Separator(&SeparatorStorage, 1) {
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);
574 SeparatorStorage = R.SeparatorStorage;
577 Separator = R.Separator;
578 if (R.Separator.data() == &R.SeparatorStorage)
579 Separator =
StringRef(&SeparatorStorage, 1);
584 assert(Separator == R.Separator);
585 return Current.data() == R.Current.data();
593 std::tie(Current, Next) = Next.split(Separator);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file defines the SmallString class.
Class for arbitrary precision integers.
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ListSeparator(StringRef Separator=", ", StringRef Prefix="")
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
A forward iterator over partitions of string over a separator.
const StringRef & operator*() const
SplittingIterator(StringRef Str, char Separator)
SplittingIterator(StringRef Str, StringRef Separator)
bool operator==(const SplittingIterator &R) const
SplittingIterator & operator=(const SplittingIterator &R)
SplittingIterator & operator++()
SplittingIterator(const SplittingIterator &R)
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ C
The default llvm calling convention, compatible with C.
bool to_float(const Twine &T, N &Num, N(*StrTo)(const char *, char **))
std::string join_impl(IteratorT Begin, IteratorT End, StringRef Separator, std::input_iterator_tag)
size_t join_one_item_size(char)
size_t join_items_size(Args &&...Items)
void join_items_impl(std::string &Result, Sep Separator)
This is an optimization pass for GlobalISel generic memory operations.
char toLower(char x)
Returns the corresponding lowercase character if x is uppercase.
std::string fromHex(StringRef Input)
Convert hexadecimal string Input to its binary representation. The return string is half the size of ...
LLVM_ABI void printHTMLEscaped(StringRef String, raw_ostream &Out)
Print each character of the specified string, escaping HTML special characters.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
LLVM_ABI void printLowerCase(StringRef String, raw_ostream &Out)
printLowerCase - Print each character as lowercase if it is uppercase.
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
bool isLower(char C)
Checks if character C is a lowercase letter as classified by "C" locale.
bool tryGetFromHex(StringRef Input, std::string &Output)
Convert hexadecimal string Input to its binary representation and store the result in Output....
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
std::string utostr(uint64_t X, bool isNeg=false)
LLVM_ABI void printEscapedString(StringRef Name, raw_ostream &Out)
Print each character of the specified string, escaping it if it is not printable or if it is an escap...
LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters,...
bool to_float(const Twine &T, float &Num)
bool isAlpha(char C)
Checks if character C is a valid letter as classified by "C" locale.
bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex)
Store the binary representation of the two provided values, MSB and LSB, that make up the nibbles of ...
LLVM_ABI std::string convertToSnakeFromCamelCase(StringRef input)
Converts a string from camel-case to snake-case by replacing all uppercase letters with '_' followed ...
bool isUpper(char C)
Checks if character C is a uppercase letter as classified by "C" locale.
uint8_t hexFromNibbles(char MSB, char LSB)
Return the binary representation of the two provided values, MSB and LSB, that make up the nibbles of...
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
@ Mod
The access may modify the value stored in memory.
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
char toUpper(char x)
Returns the corresponding uppercase character if x is lowercase.
StringRef getOrdinalSuffix(unsigned Val)
Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
std::vector< StringRef > toStringRefArray(const char *const *Strings)
Given an array of c-style strings terminated by a null pointer, construct a vector of StringRefs repr...
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI std::string convertToCamelFromSnakeCase(StringRef input, bool capitalizeFirst=false)
Converts a string from snake-case to camel-case by replacing all occurrences of '_' followed by a low...
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
std::string join_items(Sep Separator, Args &&... Items)
Joins the strings in the parameter pack Items, adding Separator between the elements....
bool isASCII(char C)
Checks whether character C is valid ASCII (high bit is zero).
bool isPrint(char C)
Checks whether character C is printable.
bool isSpace(char C)
Checks whether character C is whitespace in the "C" locale.
bool isHexDigit(char C)
Checks if character C is a hexadecimal numeric character.
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...
bool isPunct(char C)
Checks whether character C is a punctuation character.
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
std::string itostr(int64_t X)