00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef LLVM_ADT_STRINGEXTRAS_H
00015 #define LLVM_ADT_STRINGEXTRAS_H
00016
00017 #include "llvm/Support/DataTypes.h"
00018 #include "llvm/ADT/APFloat.h"
00019 #include "llvm/ADT/DenseMapInfo.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include <cctype>
00022 #include <cstdio>
00023 #include <string>
00024
00025 namespace llvm {
00026 template<typename T> class SmallVectorImpl;
00027
00030 static inline char hexdigit(unsigned X, bool LowerCase = false) {
00031 const char HexChar = LowerCase ? 'a' : 'A';
00032 return X < 10 ? '0' + X : HexChar + X - 10;
00033 }
00034
00043 template<typename IntTy>
00044 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
00045 char *BufPtr = BufferEnd;
00046 *--BufPtr = 0;
00047 if (X == 0) {
00048 *--BufPtr = '0';
00049 return BufPtr;
00050 }
00051
00052 while (X) {
00053 unsigned char Mod = static_cast<unsigned char>(X) & 15;
00054 *--BufPtr = hexdigit(Mod);
00055 X >>= 4;
00056 }
00057 return BufPtr;
00058 }
00059
00060 static inline std::string utohexstr(uint64_t X) {
00061 char Buffer[17];
00062 return utohex_buffer(X, Buffer+17);
00063 }
00064
00065 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
00066 char Buffer[11];
00067 char *BufPtr = Buffer+11;
00068
00069 if (X == 0) *--BufPtr = '0';
00070
00071 while (X) {
00072 *--BufPtr = '0' + char(X % 10);
00073 X /= 10;
00074 }
00075
00076 if (isNeg) *--BufPtr = '-';
00077
00078 return std::string(BufPtr, Buffer+11);
00079 }
00080
00081 static inline std::string utostr(uint64_t X, bool isNeg = false) {
00082 char Buffer[21];
00083 char *BufPtr = Buffer+21;
00084
00085 if (X == 0) *--BufPtr = '0';
00086
00087 while (X) {
00088 *--BufPtr = '0' + char(X % 10);
00089 X /= 10;
00090 }
00091
00092 if (isNeg) *--BufPtr = '-';
00093 return std::string(BufPtr, Buffer+21);
00094 }
00095
00096
00097 static inline std::string itostr(int64_t X) {
00098 if (X < 0)
00099 return utostr(static_cast<uint64_t>(-X), true);
00100 else
00101 return utostr(static_cast<uint64_t>(X));
00102 }
00103
00104 static inline std::string ftostr(double V) {
00105 char Buffer[200];
00106 sprintf(Buffer, "%20.6e", V);
00107 char *B = Buffer;
00108 while (*B == ' ') ++B;
00109 return B;
00110 }
00111
00112 static inline std::string ftostr(const APFloat& V) {
00113 if (&V.getSemantics() == &APFloat::IEEEdouble)
00114 return ftostr(V.convertToDouble());
00115 else if (&V.getSemantics() == &APFloat::IEEEsingle)
00116 return ftostr((double)V.convertToFloat());
00117 return "<unknown format in ftostr>";
00118 }
00119
00120 static inline std::string LowercaseString(const std::string &S) {
00121 std::string result(S);
00122 for (unsigned i = 0; i < S.length(); ++i)
00123 if (isupper(result[i]))
00124 result[i] = char(tolower(result[i]));
00125 return result;
00126 }
00127
00128 static inline std::string UppercaseString(const std::string &S) {
00129 std::string result(S);
00130 for (unsigned i = 0; i < S.length(); ++i)
00131 if (islower(result[i]))
00132 result[i] = char(toupper(result[i]));
00133 return result;
00134 }
00135
00139 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
00140
00147 std::pair<StringRef, StringRef> getToken(StringRef Source,
00148 StringRef Delimiters = " \t\n\v\f\r");
00149
00152 void SplitString(StringRef Source,
00153 SmallVectorImpl<StringRef> &OutFragments,
00154 StringRef Delimiters = " \t\n\v\f\r");
00155
00159
00160
00161
00162
00163 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
00164 for (unsigned i = 0, e = Str.size(); i != e; ++i)
00165 Result = Result * 33 + Str[i];
00166 return Result;
00167 }
00168
00169 }
00170
00171 #endif