14 #ifndef LLVM_ADT_STRINGEXTRAS_H 15 #define LLVM_ADT_STRINGEXTRAS_H 17 #include "llvm/StringRef.h" 22 template<
typename T>
class SmallVectorImpl;
26 static inline char hexdigit(
unsigned X,
bool LowerCase =
false) {
27 const char HexChar = LowerCase ?
'a' :
'A';
28 return X < 10 ?
'0' + X : HexChar + X - 10;
32 static inline StringRef toStringRef(
bool B) {
33 return StringRef(B ?
"true" :
"false");
40 static inline unsigned hexDigitValue(
char C) {
41 if (C >=
'0' && C <=
'9')
return C-
'0';
42 if (C >=
'a' && C <=
'f')
return C-
'a'+10U;
43 if (C >=
'A' && C <=
'F')
return C-
'A'+10U;
47 static inline std::string utohexstr(uint64_t X,
bool LowerCase =
false) {
49 char *BufPtr = std::end(Buffer);
51 if (X == 0) *--BufPtr =
'0';
54 unsigned char Mod =
static_cast<unsigned char>(X) & 15;
55 *--BufPtr = hexdigit(Mod, LowerCase);
59 return std::string(BufPtr, std::end(Buffer));
64 static inline std::string toHex(StringRef Input) {
65 static const char *
const LUT =
"0123456789ABCDEF";
66 size_t Length = Input.size();
69 Output.reserve(2 * Length);
70 for (
size_t i = 0; i < Length; ++i) {
71 const unsigned char c = Input[i];
72 Output.push_back(LUT[c >> 4]);
73 Output.push_back(LUT[c & 15]);
78 static inline std::string utostr(uint64_t X,
bool isNeg =
false) {
80 char *BufPtr = std::end(Buffer);
82 if (X == 0) *--BufPtr =
'0';
85 *--BufPtr =
'0' + char(X % 10);
89 if (isNeg) *--BufPtr =
'-';
90 return std::string(BufPtr, std::end(Buffer));
94 static inline std::string itostr(int64_t X) {
96 return utostr(static_cast<uint64_t>(-X),
true);
98 return utostr(static_cast<uint64_t>(X));
104 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
112 std::pair<StringRef, StringRef> getToken(StringRef Source,
113 StringRef Delimiters =
" \t\n\v\f\r");
117 void SplitString(StringRef Source,
118 SmallVectorImpl<StringRef> &OutFragments,
119 StringRef Delimiters =
" \t\n\v\f\r");
128 static inline unsigned HashString(StringRef Str,
unsigned Result = 0) {
129 for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i)
130 Result = Result * 33 + (
unsigned char)Str[i];
135 static inline StringRef getOrdinalSuffix(
unsigned Val) {
148 default:
return "th";
153 template <
typename IteratorT>
154 inline std::string join_impl(IteratorT Begin, IteratorT End,
155 StringRef Separator, std::input_iterator_tag) {
161 while (++Begin != End) {
168 template <
typename IteratorT>
169 inline std::string join_impl(IteratorT Begin, IteratorT End,
170 StringRef Separator, std::forward_iterator_tag) {
175 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
176 for (IteratorT I = Begin; I != End; ++I)
177 Len += (*Begin).size();
180 while (++Begin != End) {
189 template <
typename IteratorT>
190 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
191 typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
192 return join_impl(Begin, End, Separator, tag());