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;
63 return (std::numeric_limits<unsigned>::max)();
67 inline bool isDigit(
char C) {
return C >=
'0' && C <=
'9'; }
74 return (
'a' <= C && C <=
'z') || (
'A' <= C && C <=
'Z');
82 inline bool isASCII(
char C) {
return static_cast<unsigned char>(C) <= 127; }
97 unsigned char UC = static_cast<unsigned char>(C);
98 return (0x20 <= UC) && (UC <= 0x7E);
103 if (x >=
'A' && x <=
'Z')
104 return x -
'A' +
'a';
110 if (x >=
'a' && x <=
'z')
111 return x -
'a' +
'A';
115 inline std::string utohexstr(uint64_t X,
bool LowerCase =
false) {
117 char *BufPtr = std::end(Buffer);
119 if (X == 0) *--BufPtr =
'0';
122 unsigned char Mod = static_cast<unsigned char>(X) & 15;
123 *--BufPtr =
hexdigit(Mod, LowerCase);
127 return std::string(BufPtr, std::end(Buffer));
133 static const char *
const LUT =
"0123456789ABCDEF";
134 const uint8_t Offset = LowerCase ? 32 : 0;
135 size_t Length = Input.
size();
138 Output.reserve(2 * Length);
139 for (
size_t i = 0; i < Length; ++i) {
140 const unsigned char c = Input[i];
141 Output.push_back(LUT[c >> 4] | Offset);
142 Output.push_back(LUT[c & 15] | Offset);
147 inline std::string
toHex(ArrayRef<uint8_t> Input,
bool LowerCase =
false) {
151 inline uint8_t hexFromNibbles(
char MSB,
char LSB) {
154 assert(U1 != (std::numeric_limits<unsigned>::max)() && U2 != (std::numeric_limits<unsigned>::max)());
156 return static_cast<uint8_t>((U1 << 4) | U2);
163 return std::string();
166 Output.reserve((Input.
size() + 1) / 2);
167 if (Input.
size() % 2 == 1) {
168 Output.push_back(hexFromNibbles(
'0', Input.
front()));
172 assert(Input.
size() % 2 == 0);
173 while (!Input.
empty()) {
174 uint8_t Hex = hexFromNibbles(Input[0], Input[1]);
175 Output.push_back(Hex);
185 return !S.getAsInteger(Base, Num);
189 template <
typename N>
190 inline bool to_float(
const Twine &T, N &Num, N (*StrTo)(
const char *,
char **)) {
191 SmallString<32> Storage;
192 StringRef S = T.toNullTerminatedStringRef(Storage);
194 N Temp = StrTo(S.data(), &End);
202 inline bool to_float(
const Twine &T,
float &Num) {
203 return detail::to_float(T, Num, strtof);
206 inline bool to_float(
const Twine &T,
double &Num) {
207 return detail::to_float(T, Num, strtod);
210 inline bool to_float(
const Twine &T,
long double &Num) {
211 return detail::to_float(T, Num, strtold);
214 inline std::string utostr(uint64_t X,
bool isNeg =
false) {
216 char *BufPtr = std::end(Buffer);
218 if (X == 0) *--BufPtr =
'0';
221 *--BufPtr =
'0' + char(X % 10);
225 if (isNeg) *--BufPtr =
'-';
226 return std::string(BufPtr, std::end(Buffer));
229 inline std::string itostr(int64_t X) {
231 return utostr(static_cast<uint64_t>(-X),
true);
233 return utostr(static_cast<uint64_t>(X));
247 std::pair<StringRef, StringRef>
getToken(StringRef Source,
248 StringRef Delimiters =
" \t\n\v\f\r");
253 SmallVectorImpl<StringRef> &OutFragments,
254 StringRef Delimiters =
" \t\n\v\f\r");
264 for (StringRef::size_type i = 0, e = Str.
size(); i != e; ++i)
265 Result = Result * 33 + (
unsigned char)Str[i];
283 default:
return "th";
301 template <
typename IteratorT>
302 inline std::string join_impl(IteratorT Begin, IteratorT End,
303 StringRef Separator, std::input_iterator_tag) {
309 while (++Begin != End) {
316 template <
typename IteratorT>
317 inline std::string join_impl(IteratorT Begin, IteratorT End,
318 StringRef Separator, std::forward_iterator_tag) {
323 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
324 for (IteratorT I = Begin; I != End; ++I)
325 Len += (*Begin).size();
328 while (++Begin != End) {
335 template <
typename Sep>
336 inline void join_items_impl(
std::string &Result, Sep Separator) {}
338 template <
typename Sep,
typename Arg>
339 inline void join_items_impl(
std::string &Result, Sep Separator,
344 template <
typename Sep,
typename Arg1,
typename... Args>
345 inline void join_items_impl(
std::string &Result, Sep Separator,
const Arg1 &A1,
349 join_items_impl(Result, Separator, std::forward<Args>(Items)...);
352 inline size_t join_one_item_size(
char C) {
return 1; }
353 inline size_t join_one_item_size(
const char *S) {
return S ? ::strlen(S) : 0; }
355 template <
typename T>
inline size_t join_one_item_size(
const T &Str) {
359 inline size_t join_items_size() {
return 0; }
361 template <
typename A1>
inline size_t join_items_size(
const A1 &A) {
362 return join_one_item_size(A);
364 template <
typename A1,
typename... Args>
365 inline size_t join_items_size(
const A1 &A, Args &&... Items) {
366 return join_one_item_size(A) + join_items_size(std::forward<Args>(Items)...);
373 template <
typename IteratorT>
374 inline std::string
join(IteratorT Begin, IteratorT End,
StringRef Separator) {
375 using tag =
typename std::iterator_traits<IteratorT>::iterator_category;
376 return detail::join_impl(Begin, End, Separator, tag());
381 template <
typename Range>
383 return join(R.begin(), R.end(), Separator);
390 template <
typename Sep,
typename... Args>
391 inline std::string
join_items(Sep Separator, Args &&... Items) {
393 if (
sizeof...(Items) == 0)
396 size_t NS = detail::join_one_item_size(Separator);
397 size_t NI = detail::join_items_size(std::forward<Args>(Items)...);
398 Result.reserve(NI + (
sizeof...(Items) - 1) * NS + 1);
399 detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...);
405 #endif // WPIUTIL_WPI_STRINGEXTRAS_H
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const noexcept
empty - Check if the string is empty.
Definition: StringRef.h:133
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:43
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138
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:184
bool isASCII(char C)
Checks whether character C is valid ASCII (high bit is zero).
Definition: StringExtras.h:82
StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2)
StrInStrNoCase - Portable version of strcasestr.
bool isAlpha(char C)
Checks if character C is a valid letter as classified by "C" locale.
Definition: StringExtras.h:73
static unsigned HashString(StringRef Str, unsigned Result=0)
HashString - Hash function for strings.
Definition: StringExtras.h:263
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
Definition: StringExtras.h:67
bool isPrint(char C)
Checks whether character C is printable.
Definition: StringExtras.h:96
StringRef getOrdinalSuffix(unsigned Val)
Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
Definition: StringExtras.h:270
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 ...
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
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
void printLowerCase(StringRef String, raw_ostream &Out)
printLowerCase - Print each character as lowercase if it is uppercase.
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:59
char toLower(char x)
Returns the corresponding lowercase character if x is uppercase.
Definition: StringExtras.h:102
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
void printHTMLEscaped(StringRef String, raw_ostream &Out)
Print each character of the specified string, escaping HTML special characters.
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:374
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
LLVM_NODISCARD char front() const noexcept
front - Get the first character in the string.
Definition: StringRef.h:142
ArrayRef< uint8_t > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
Definition: StringExtras.h:51
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,...
std::string toHex(StringRef Input, bool LowerCase=false)
Convert buffer Input to its hexadecimal representation.
Definition: StringExtras.h:132
std::string join_items(Sep Separator, Args &&... Items)
Joins the strings in the parameter pack Items, adding Separator between the elements.
Definition: StringExtras.h:391
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...
char toUpper(char x)
Returns the corresponding uppercase character if x is lowercase.
Definition: StringExtras.h:109
std::string fromHex(StringRef Input)
Convert hexadecimal string Input to its binary representation.
Definition: StringExtras.h:161
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
bool isHexDigit(char C)
Checks if character C is a hexadecimal numeric character.
Definition: StringExtras.h:70