LLVM API Documentation
00001 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains some functions that are useful when dealing with strings. 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 <cctype> 00020 #include <cstdio> 00021 #include <string> 00022 #include <vector> 00023 00024 namespace llvm { 00025 00026 /// hexdigit - Return the (uppercase) hexadecimal character for the 00027 /// given number \arg X (which should be less than 16). 00028 static inline char hexdigit(unsigned X) { 00029 return X < 10 ? '0' + X : 'A' + X - 10; 00030 } 00031 00032 /// utohex_buffer - Emit the specified number into the buffer specified by 00033 /// BufferEnd, returning a pointer to the start of the string. This can be used 00034 /// like this: (note that the buffer must be large enough to handle any number): 00035 /// char Buffer[40]; 00036 /// printf("0x%s", utohex_buffer(X, Buffer+40)); 00037 /// 00038 /// This should only be used with unsigned types. 00039 /// 00040 template<typename IntTy> 00041 static inline char *utohex_buffer(IntTy X, char *BufferEnd) { 00042 char *BufPtr = BufferEnd; 00043 *--BufPtr = 0; // Null terminate buffer. 00044 if (X == 0) { 00045 *--BufPtr = '0'; // Handle special case. 00046 return BufPtr; 00047 } 00048 00049 while (X) { 00050 unsigned char Mod = static_cast<unsigned char>(X) & 15; 00051 *--BufPtr = hexdigit(Mod); 00052 X >>= 4; 00053 } 00054 return BufPtr; 00055 } 00056 00057 static inline std::string utohexstr(uint64_t X) { 00058 char Buffer[40]; 00059 return utohex_buffer(X, Buffer+40); 00060 } 00061 00062 static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 00063 char Buffer[20]; 00064 char *BufPtr = Buffer+19; 00065 00066 *BufPtr = 0; // Null terminate buffer... 00067 if (X == 0) *--BufPtr = '0'; // Handle special case... 00068 00069 while (X) { 00070 *--BufPtr = '0' + char(X % 10); 00071 X /= 10; 00072 } 00073 00074 if (isNeg) *--BufPtr = '-'; // Add negative sign... 00075 00076 return std::string(BufPtr); 00077 } 00078 00079 static inline std::string utostr(uint64_t X, bool isNeg = false) { 00080 if (X == uint32_t(X)) 00081 return utostr_32(uint32_t(X), isNeg); 00082 00083 char Buffer[40]; 00084 char *BufPtr = Buffer+39; 00085 00086 *BufPtr = 0; // Null terminate buffer... 00087 if (X == 0) *--BufPtr = '0'; // Handle special case... 00088 00089 while (X) { 00090 *--BufPtr = '0' + char(X % 10); 00091 X /= 10; 00092 } 00093 00094 if (isNeg) *--BufPtr = '-'; // Add negative sign... 00095 return std::string(BufPtr); 00096 } 00097 00098 00099 static inline std::string itostr(int64_t X) { 00100 if (X < 0) 00101 return utostr(static_cast<uint64_t>(-X), true); 00102 else 00103 return utostr(static_cast<uint64_t>(X)); 00104 } 00105 00106 static inline std::string ftostr(double V) { 00107 char Buffer[200]; 00108 sprintf(Buffer, "%20.6e", V); 00109 char *B = Buffer; 00110 while (*B == ' ') ++B; 00111 return B; 00112 } 00113 00114 static inline std::string ftostr(const APFloat& V) { 00115 if (&V.getSemantics() == &APFloat::IEEEdouble) 00116 return ftostr(V.convertToDouble()); 00117 else if (&V.getSemantics() == &APFloat::IEEEsingle) 00118 return ftostr((double)V.convertToFloat()); 00119 return "<unknown format in ftostr>"; // error 00120 } 00121 00122 static inline std::string LowercaseString(const std::string &S) { 00123 std::string result(S); 00124 for (unsigned i = 0; i < S.length(); ++i) 00125 if (isupper(result[i])) 00126 result[i] = char(tolower(result[i])); 00127 return result; 00128 } 00129 00130 static inline std::string UppercaseString(const std::string &S) { 00131 std::string result(S); 00132 for (unsigned i = 0; i < S.length(); ++i) 00133 if (islower(result[i])) 00134 result[i] = char(toupper(result[i])); 00135 return result; 00136 } 00137 00138 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00139 /// case. 00140 static inline bool StringsEqualNoCase(const std::string &LHS, 00141 const std::string &RHS) { 00142 if (LHS.size() != RHS.size()) return false; 00143 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) 00144 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00145 return true; 00146 } 00147 00148 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00149 /// case. 00150 static inline bool StringsEqualNoCase(const std::string &LHS, 00151 const char *RHS) { 00152 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) { 00153 if (RHS[i] == 0) return false; // RHS too short. 00154 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00155 } 00156 return RHS[LHS.size()] == 0; // Not too long? 00157 } 00158 00159 /// StringsEqualNoCase - Return true if the two null-terminated C strings are 00160 /// equal, ignoring 00161 00162 static inline bool StringsEqualNoCase(const char *LHS, const char *RHS, 00163 unsigned len) { 00164 00165 for (unsigned i = 0; i < len; ++i) { 00166 if (tolower(LHS[i]) != tolower(RHS[i])) 00167 return false; 00168 00169 // If RHS[i] == 0 then LHS[i] == 0 or otherwise we would have returned 00170 // at the previous branch as tolower('\0') == '\0'. 00171 if (RHS[i] == 0) 00172 return true; 00173 } 00174 00175 return true; 00176 } 00177 00178 /// CStrInCStrNoCase - Portable version of strcasestr. Locates the first 00179 /// occurance of c-string 's2' in string 's1', ignoring case. Returns 00180 /// NULL if 's2' cannot be found. 00181 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) { 00182 00183 // Are either strings NULL or empty? 00184 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0') 00185 return 0; 00186 00187 if (s1 == s2) 00188 return s1; 00189 00190 const char *I1=s1, *I2=s2; 00191 00192 while (*I1 != '\0' && *I2 != '\0' ) 00193 if (tolower(*I1) != tolower(*I2)) { // No match. Start over. 00194 ++s1; I1 = s1; I2 = s2; 00195 } 00196 else { // Character match. Advance to the next character. 00197 ++I1; ++I2; 00198 } 00199 00200 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'. 00201 return *I2 == '\0' ? s1 : 0; 00202 } 00203 00204 /// getToken - This function extracts one token from source, ignoring any 00205 /// leading characters that appear in the Delimiters string, and ending the 00206 /// token at any of the characters that appear in the Delimiters string. If 00207 /// there are no tokens in the source string, an empty string is returned. 00208 /// The Source source string is updated in place to remove the returned string 00209 /// and any delimiter prefix from it. 00210 std::string getToken(std::string &Source, 00211 const char *Delimiters = " \t\n\v\f\r"); 00212 00213 /// SplitString - Split up the specified string according to the specified 00214 /// delimiters, appending the result fragments to the output list. 00215 void SplitString(const std::string &Source, 00216 std::vector<std::string> &OutFragments, 00217 const char *Delimiters = " \t\n\v\f\r"); 00218 00219 /// UnescapeString - Modify the argument string, turning two character sequences 00220 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 00221 /// \num (where num is a 1-3 byte octal value). 00222 void UnescapeString(std::string &Str); 00223 00224 /// EscapeString - Modify the argument string, turning '\\' and anything that 00225 /// doesn't satisfy std::isprint into an escape sequence. 00226 void EscapeString(std::string &Str); 00227 00228 } // End llvm namespace 00229 00230 #endif