StringExtras.h revision 07bf7efa0ac1f7ca8ca57a675dd8c6e2c52df73a
1//===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains some functions that are useful when dealing with strings. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ADT_STRINGEXTRAS_H 15#define LLVM_ADT_STRINGEXTRAS_H 16 17#include "llvm/Support/DataTypes.h" 18#include "llvm/ADT/APFloat.h" 19#include <cctype> 20#include <cstdio> 21#include <string> 22#include <vector> 23 24namespace llvm { 25 26/// hexdigit - Return the (uppercase) hexadecimal character for the 27/// given number \arg X (which should be less than 16). 28static inline char hexdigit(unsigned X) { 29 return X < 10 ? '0' + X : 'A' + X - 10; 30} 31 32/// utohex_buffer - Emit the specified number into the buffer specified by 33/// BufferEnd, returning a pointer to the start of the string. This can be used 34/// like this: (note that the buffer must be large enough to handle any number): 35/// char Buffer[40]; 36/// printf("0x%s", utohex_buffer(X, Buffer+40)); 37/// 38/// This should only be used with unsigned types. 39/// 40template<typename IntTy> 41static inline char *utohex_buffer(IntTy X, char *BufferEnd) { 42 char *BufPtr = BufferEnd; 43 *--BufPtr = 0; // Null terminate buffer. 44 if (X == 0) { 45 *--BufPtr = '0'; // Handle special case. 46 return BufPtr; 47 } 48 49 while (X) { 50 unsigned char Mod = static_cast<unsigned char>(X) & 15; 51 *--BufPtr = hexdigit(Mod); 52 X >>= 4; 53 } 54 return BufPtr; 55} 56 57static inline std::string utohexstr(uint64_t X) { 58 char Buffer[40]; 59 return utohex_buffer(X, Buffer+40); 60} 61 62static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 63 char Buffer[20]; 64 char *BufPtr = Buffer+19; 65 66 *BufPtr = 0; // Null terminate buffer... 67 if (X == 0) *--BufPtr = '0'; // Handle special case... 68 69 while (X) { 70 *--BufPtr = '0' + char(X % 10); 71 X /= 10; 72 } 73 74 if (isNeg) *--BufPtr = '-'; // Add negative sign... 75 76 return std::string(BufPtr); 77} 78 79static inline std::string utostr(uint64_t X, bool isNeg = false) { 80 if (X == uint32_t(X)) 81 return utostr_32(uint32_t(X), isNeg); 82 83 char Buffer[40]; 84 char *BufPtr = Buffer+39; 85 86 *BufPtr = 0; // Null terminate buffer... 87 if (X == 0) *--BufPtr = '0'; // Handle special case... 88 89 while (X) { 90 *--BufPtr = '0' + char(X % 10); 91 X /= 10; 92 } 93 94 if (isNeg) *--BufPtr = '-'; // Add negative sign... 95 return std::string(BufPtr); 96} 97 98 99static inline std::string itostr(int64_t X) { 100 if (X < 0) 101 return utostr(static_cast<uint64_t>(-X), true); 102 else 103 return utostr(static_cast<uint64_t>(X)); 104} 105 106static inline std::string itohexstr(int64_t X) { 107 return utohexstr(static_cast<uint64_t>(X)); 108} 109 110static inline std::string ftostr(double V) { 111 char Buffer[200]; 112 sprintf(Buffer, "%20.6e", V); 113 char *B = Buffer; 114 while (*B == ' ') ++B; 115 return B; 116} 117 118static inline std::string ftostr(const APFloat& V) { 119 if (&V.getSemantics() == &APFloat::IEEEdouble) 120 return ftostr(V.convertToDouble()); 121 else if (&V.getSemantics() == &APFloat::IEEEsingle) 122 return ftostr((double)V.convertToFloat()); 123 return "<unknown format in ftostr>"; // error 124} 125 126static inline std::string LowercaseString(const std::string &S) { 127 std::string result(S); 128 for (unsigned i = 0; i < S.length(); ++i) 129 if (isupper(result[i])) 130 result[i] = char(tolower(result[i])); 131 return result; 132} 133 134static inline std::string UppercaseString(const std::string &S) { 135 std::string result(S); 136 for (unsigned i = 0; i < S.length(); ++i) 137 if (islower(result[i])) 138 result[i] = char(toupper(result[i])); 139 return result; 140} 141 142/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 143/// case. 144static inline bool StringsEqualNoCase(const std::string &LHS, 145 const std::string &RHS) { 146 if (LHS.size() != RHS.size()) return false; 147 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) 148 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 149 return true; 150} 151 152/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 153/// case. 154static inline bool StringsEqualNoCase(const std::string &LHS, 155 const char *RHS) { 156 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) { 157 if (RHS[i] == 0) return false; // RHS too short. 158 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 159 } 160 return RHS[LHS.size()] == 0; // Not too long? 161} 162 163/// StringsEqualNoCase - Return true if the two null-terminated C strings are 164/// equal, ignoring 165 166static inline bool StringsEqualNoCase(const char *LHS, const char *RHS, 167 unsigned len) { 168 169 for (unsigned i = 0; i < len; ++i) { 170 if (tolower(LHS[i]) != tolower(RHS[i])) 171 return false; 172 173 // If RHS[i] == 0 then LHS[i] == 0 or otherwise we would have returned 174 // at the previous branch as tolower('\0') == '\0'. 175 if (RHS[i] == 0) 176 return true; 177 } 178 179 return true; 180} 181 182/// CStrInCStrNoCase - Portable version of strcasestr. Locates the first 183/// occurance of c-string 's2' in string 's1', ignoring case. Returns 184/// NULL if 's2' cannot be found. 185static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) { 186 187 // Are either strings NULL or empty? 188 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0') 189 return 0; 190 191 if (s1 == s2) 192 return s1; 193 194 const char *I1=s1, *I2=s2; 195 196 while (*I1 != '\0' && *I2 != '\0' ) 197 if (tolower(*I1) != tolower(*I2)) { // No match. Start over. 198 ++s1; I1 = s1; I2 = s2; 199 } 200 else { // Character match. Advance to the next character. 201 ++I1; ++I2; 202 } 203 204 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'. 205 return *I2 == '\0' ? s1 : 0; 206} 207 208/// getToken - This function extracts one token from source, ignoring any 209/// leading characters that appear in the Delimiters string, and ending the 210/// token at any of the characters that appear in the Delimiters string. If 211/// there are no tokens in the source string, an empty string is returned. 212/// The Source source string is updated in place to remove the returned string 213/// and any delimiter prefix from it. 214std::string getToken(std::string &Source, 215 const char *Delimiters = " \t\n\v\f\r"); 216 217/// SplitString - Split up the specified string according to the specified 218/// delimiters, appending the result fragments to the output list. 219void SplitString(const std::string &Source, 220 std::vector<std::string> &OutFragments, 221 const char *Delimiters = " \t\n\v\f\r"); 222 223/// UnescapeString - Modify the argument string, turning two character sequences 224/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 225/// \num (where num is a 1-3 byte octal value). 226void UnescapeString(std::string &Str); 227 228/// EscapeString - Modify the argument string, turning '\\' and anything that 229/// doesn't satisfy std::isprint into an escape sequence. 230void EscapeString(std::string &Str); 231 232} // End llvm namespace 233 234#endif 235