#ifndef LLVM_ADT_STRINGEXTRAS_H
#define LLVM_ADT_STRINGEXTRAS_H
#include "llvm/System/DataTypes.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/StringRef.h"
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
namespace llvm {
template<typename T> class SmallVectorImpl;
static inline char hexdigit(unsigned X) {
return X < 10 ? '0' + X : 'A' + X - 10;
}
template<typename IntTy>
static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
char *BufPtr = BufferEnd;
*--BufPtr = 0; if (X == 0) {
*--BufPtr = '0'; return BufPtr;
}
while (X) {
unsigned char Mod = static_cast<unsigned char>(X) & 15;
*--BufPtr = hexdigit(Mod);
X >>= 4;
}
return BufPtr;
}
static inline std::string utohexstr(uint64_t X) {
char Buffer[17];
return utohex_buffer(X, Buffer+17);
}
static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
char Buffer[11];
char *BufPtr = Buffer+11;
if (X == 0) *--BufPtr = '0';
while (X) {
*--BufPtr = '0' + char(X % 10);
X /= 10;
}
if (isNeg) *--BufPtr = '-';
return std::string(BufPtr, Buffer+11);
}
static inline std::string utostr(uint64_t X, bool isNeg = false) {
char Buffer[21];
char *BufPtr = Buffer+21;
if (X == 0) *--BufPtr = '0';
while (X) {
*--BufPtr = '0' + char(X % 10);
X /= 10;
}
if (isNeg) *--BufPtr = '-'; return std::string(BufPtr, Buffer+21);
}
static inline std::string itostr(int64_t X) {
if (X < 0)
return utostr(static_cast<uint64_t>(-X), true);
else
return utostr(static_cast<uint64_t>(X));
}
static inline std::string ftostr(double V) {
char Buffer[200];
sprintf(Buffer, "%20.6e", V);
char *B = Buffer;
while (*B == ' ') ++B;
return B;
}
static inline std::string ftostr(const APFloat& V) {
if (&V.getSemantics() == &APFloat::IEEEdouble)
return ftostr(V.convertToDouble());
else if (&V.getSemantics() == &APFloat::IEEEsingle)
return ftostr((double)V.convertToFloat());
return "<unknown format in ftostr>"; }
static inline std::string LowercaseString(const std::string &S) {
std::string result(S);
for (unsigned i = 0; i < S.length(); ++i)
if (isupper(result[i]))
result[i] = char(tolower(result[i]));
return result;
}
static inline std::string UppercaseString(const std::string &S) {
std::string result(S);
for (unsigned i = 0; i < S.length(); ++i)
if (islower(result[i]))
result[i] = char(toupper(result[i]));
return result;
}
StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
std::pair<StringRef, StringRef> getToken(StringRef Source,
StringRef Delimiters = " \t\n\v\f\r");
void SplitString(StringRef Source,
SmallVectorImpl<StringRef> &OutFragments,
StringRef Delimiters = " \t\n\v\f\r");
static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
for (unsigned i = 0, e = Str.size(); i != e; ++i)
Result = Result * 33 + Str[i];
return Result;
}
}
#endif