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;
55 template<
typename IntTy>
56 static inline char *utohex_buffer(IntTy X,
char *BufferEnd,
bool LowerCase =
false) {
57 char *BufPtr = BufferEnd;
65 unsigned char Mod =
static_cast<unsigned char>(X) & 15;
66 *--BufPtr = hexdigit(Mod, LowerCase);
72 static inline std::string utohexstr(uint64_t X,
bool LowerCase =
false) {
74 return utohex_buffer(X, Buffer+17, LowerCase);
77 static inline std::string utostr_32(uint32_t X,
bool isNeg =
false) {
79 char *BufPtr = Buffer+11;
81 if (X == 0) *--BufPtr =
'0';
84 *--BufPtr =
'0' + char(X % 10);
88 if (isNeg) *--BufPtr =
'-';
90 return std::string(BufPtr, Buffer+11);
93 static inline std::string utostr(uint64_t X,
bool isNeg =
false) {
95 char *BufPtr = Buffer+21;
97 if (X == 0) *--BufPtr =
'0';
100 *--BufPtr =
'0' + char(X % 10);
104 if (isNeg) *--BufPtr =
'-';
105 return std::string(BufPtr, Buffer+21);
109 static inline std::string itostr(int64_t X) {
111 return utostr(static_cast<uint64_t>(-X),
true);
113 return utostr(static_cast<uint64_t>(X));
119 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
127 std::pair<StringRef, StringRef> getToken(StringRef Source,
128 StringRef Delimiters =
" \t\n\v\f\r");
132 void SplitString(StringRef Source,
133 SmallVectorImpl<StringRef> &OutFragments,
134 StringRef Delimiters =
" \t\n\v\f\r");
143 static inline unsigned HashString(StringRef Str,
unsigned Result = 0) {
144 for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i)
145 Result = Result * 33 + (
unsigned char)Str[i];
150 static inline StringRef getOrdinalSuffix(
unsigned Val) {
163 default:
return "th";
168 template <
typename IteratorT>
169 inline std::string join_impl(IteratorT Begin, IteratorT End,
170 StringRef Separator, std::input_iterator_tag) {
176 while (++Begin != End) {
183 template <
typename IteratorT>
184 inline std::string join_impl(IteratorT Begin, IteratorT End,
185 StringRef Separator, std::forward_iterator_tag) {
190 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
191 for (IteratorT I = Begin; I != End; ++I)
192 Len += (*Begin).size();
195 while (++Begin != End) {
204 template <
typename IteratorT>
205 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
206 typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
207 return join_impl(Begin, End, Separator, tag());