14 #ifndef WPIUTIL_WPI_STRINGEXTRAS_H 15 #define WPIUTIL_WPI_STRINGEXTRAS_H 17 #include "wpi/ArrayRef.h" 18 #include "wpi/SmallString.h" 19 #include "wpi/StringRef.h" 20 #include "wpi/Twine.h" 32 template<
typename T>
class SmallVectorImpl;
37 inline char hexdigit(
unsigned X,
bool LowerCase =
false) {
38 const char HexChar = LowerCase ?
'a' :
'A';
39 return X < 10 ?
'0' + X : HexChar + X - 10;
47 return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.
size());
52 return {Input.bytes_begin(), Input.bytes_end()};
60 if (C >=
'0' && C <=
'9')
return C-
'0';
61 if (C >=
'a' && C <=
'f')
return C-
'a'+10U;
62 if (C >=
'A' && C <=
'F')
return C-
'A'+10U;
67 inline bool isDigit(
char C) {
return C >=
'0' && C <=
'9'; }
74 return (
'a' <= C && C <=
'z') || (
'A' <= C && C <=
'Z');
83 if (x >=
'A' && x <=
'Z')
90 if (x >=
'a' && x <=
'z')
95 inline std::string utohexstr(uint64_t X,
bool LowerCase =
false) {
97 char *BufPtr = std::end(Buffer);
99 if (X == 0) *--BufPtr =
'0';
102 unsigned char Mod =
static_cast<unsigned char>(X) & 15;
103 *--BufPtr =
hexdigit(Mod, LowerCase);
107 return std::string(BufPtr, std::end(Buffer));
113 static const char *
const LUT =
"0123456789ABCDEF";
114 size_t Length = Input.
size();
117 Output.reserve(2 * Length);
118 for (
size_t i = 0; i < Length; ++i) {
119 const unsigned char c = Input[i];
120 Output.push_back(LUT[c >> 4]);
121 Output.push_back(LUT[c & 15]);
130 inline uint8_t hexFromNibbles(
char MSB,
char LSB) {
133 assert(U1 != -1U && U2 != -1U);
135 return static_cast<uint8_t
>((U1 << 4) | U2);
142 return std::string();
145 Output.reserve((Input.
size() + 1) / 2);
146 if (Input.
size() % 2 == 1) {
147 Output.push_back(hexFromNibbles(
'0', Input.
front()));
151 assert(Input.
size() % 2 == 0);
152 while (!Input.
empty()) {
153 uint8_t Hex = hexFromNibbles(Input[0], Input[1]);
154 Output.push_back(Hex);
168 template <
typename N>
169 inline bool to_float(
const Twine &T, N &Num, N (*StrTo)(
const char *,
char **)) {
173 N Temp = StrTo(S.
data(), &End);
181 inline bool to_float(
const Twine &T,
float &Num) {
182 return detail::to_float(T, Num, strtof);
185 inline bool to_float(
const Twine &T,
double &Num) {
186 return detail::to_float(T, Num, strtod);
189 inline bool to_float(
const Twine &T,
long double &Num) {
190 return detail::to_float(T, Num, strtold);
193 inline std::string utostr(uint64_t X,
bool isNeg =
false) {
195 char *BufPtr = std::end(Buffer);
197 if (X == 0) *--BufPtr =
'0';
200 *--BufPtr =
'0' + char(X % 10);
204 if (isNeg) *--BufPtr =
'-';
210 return utostr(static_cast<uint64_t>(-X),
true);
212 return utostr(static_cast<uint64_t>(X));
242 static inline unsigned HashString(
StringRef Str,
unsigned Result = 0) {
243 for (StringRef::size_type i = 0, e = Str.
size(); i != e; ++i)
244 Result = Result * 33 + (
unsigned char)Str[i];
262 default:
return "th";
276 template <
typename IteratorT>
277 inline std::string join_impl(IteratorT Begin, IteratorT End,
278 StringRef Separator, std::input_iterator_tag) {
284 while (++Begin != End) {
291 template <
typename IteratorT>
292 inline std::string join_impl(IteratorT Begin, IteratorT End,
293 StringRef Separator, std::forward_iterator_tag) {
298 size_t Len = (std::distance(Begin, End) - 1) * Separator.
size();
299 for (IteratorT I = Begin; I != End; ++I)
300 Len += (*Begin).size();
303 while (++Begin != End) {
310 template <
typename Sep>
311 inline void join_items_impl(
std::string &Result, Sep Separator) {}
313 template <
typename Sep,
typename Arg>
314 inline void join_items_impl(
std::string &Result, Sep Separator,
319 template <
typename Sep,
typename Arg1,
typename... Args>
320 inline void join_items_impl(
std::string &Result, Sep Separator,
const Arg1 &A1,
324 join_items_impl(Result, Separator, std::forward<Args>(Items)...);
327 inline size_t join_one_item_size(
char C) {
return 1; }
328 inline size_t join_one_item_size(
const char *S) {
return S ? ::strlen(S) : 0; }
330 template <
typename T>
inline size_t join_one_item_size(
const T &Str) {
334 inline size_t join_items_size() {
return 0; }
336 template <
typename A1>
inline size_t join_items_size(
const A1 &A) {
337 return join_one_item_size(A);
339 template <
typename A1,
typename... Args>
340 inline size_t join_items_size(
const A1 &A, Args &&... Items) {
341 return join_one_item_size(A) + join_items_size(std::forward<Args>(Items)...);
348 template <
typename IteratorT>
350 using tag =
typename std::iterator_traits<IteratorT>::iterator_category;
351 return detail::join_impl(Begin, End, Separator, tag());
356 template <
typename Range>
358 return join(R.begin(), R.end(), Separator);
365 template <
typename Sep,
typename... Args>
368 if (
sizeof...(Items) == 0)
371 size_t NS = detail::join_one_item_size(Separator);
372 size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
373 Result.reserve(NI + (
sizeof...(Items) - 1) * NS + 1);
374 detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
380 #endif // LLVM_ADT_STRINGEXTRAS_H This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
bool isHexDigit(char C)
Checks if character C is a hexadecimal numeric character.
Definition: StringExtras.h:70
LLVM_NODISCARD char front() const noexcept
front - Get the first character in the string.
Definition: StringRef.h:142
std::string fromHex(StringRef Input)
Convert hexadecimal string Input to its binary representation.
Definition: StringExtras.h:140
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const noexcept
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
StringRef getOrdinalSuffix(unsigned Val)
Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
Definition: StringExtras.h:249
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
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.
Definition: StringExtras.h:163
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
Definition: Twine.cpp:30
bool isAlpha(char C)
Checks if character C is a valid letter as classified by "C" locale.
Definition: StringExtras.h:73
StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2)
StrInStrNoCase - Portable version of strcasestr.
Definition: StringExtras.cpp:22
void printLowerCase(StringRef String, raw_ostream &Out)
printLowerCase - Print each character as lowercase if it is uppercase.
Definition: StringExtras.cpp:71
char toLower(char x)
Returns the corresponding lowercase character if x is uppercase.
Definition: StringExtras.h:82
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const noexcept
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:475
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16)...
Definition: StringExtras.h:37
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
Definition: StringExtras.h:79
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 ...
Definition: StringExtras.cpp:38
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
Definition: StringExtras.h:67
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:59
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const noexcept
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:603
void PrintEscapedString(StringRef Name, raw_ostream &Out)
PrintEscapedString - Print each character of the specified string, escaping it if it is not printable...
Definition: StringExtras.cpp:61
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const noexcept
empty - Check if the string is empty.
Definition: StringRef.h:133
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
Definition: StringExtras.h:349
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Definition: StringExtras.h:51
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: WindowsSupport.h:184
std::string join_items(Sep Separator, Args &&...Items)
Joins the strings in the parameter pack Items, adding Separator between the elements.
Definition: StringExtras.h:366
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138
std::string toHex(StringRef Input)
Convert buffer Input to its hexadecimal representation.
Definition: StringExtras.h:112
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, appending the result fragments to the output list.
Definition: StringExtras.cpp:51
char toUpper(char x)
Returns the corresponding uppercase character if x is lowercase.
Definition: StringExtras.h:89
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:43