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 itohexstr(int64_t X) { 00107 return utohexstr(static_cast<uint64_t>(X)); 00108 } 00109 00110 static inline std::string ftostr(double V) { 00111 char Buffer[200]; 00112 sprintf(Buffer, "%20.6e", V); 00113 char *B = Buffer; 00114 while (*B == ' ') ++B; 00115 return B; 00116 } 00117 00118 static inline std::string ftostr(const APFloat& V) { 00119 if (&V.getSemantics() == &APFloat::IEEEdouble) 00120 return ftostr(V.convertToDouble()); 00121 else if (&V.getSemantics() == &APFloat::IEEEsingle) 00122 return ftostr((double)V.convertToFloat()); 00123 return "<unknown format in ftostr>"; // error 00124 } 00125 00126 static inline std::string LowercaseString(const std::string &S) { 00127 std::string result(S); 00128 for (unsigned i = 0; i < S.length(); ++i) 00129 if (isupper(result[i])) 00130 result[i] = char(tolower(result[i])); 00131 return result; 00132 } 00133 00134 static inline std::string UppercaseString(const std::string &S) { 00135 std::string result(S); 00136 for (unsigned i = 0; i < S.length(); ++i) 00137 if (islower(result[i])) 00138 result[i] = char(toupper(result[i])); 00139 return result; 00140 } 00141 00142 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00143 /// case. 00144 static inline bool StringsEqualNoCase(const std::string &LHS, 00145 const std::string &RHS) { 00146 if (LHS.size() != RHS.size()) return false; 00147 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) 00148 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00149 return true; 00150 } 00151 00152 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00153 /// case. 00154 static inline bool StringsEqualNoCase(const std::string &LHS, 00155 const char *RHS) { 00156 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) { 00157 if (RHS[i] == 0) return false; // RHS too short. 00158 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00159 } 00160 return RHS[LHS.size()] == 0; // Not too long? 00161 } 00162 00163 /// CStrInCStrNoCase - Portable version of strcasestr. Locates the first 00164 /// occurance of c-string 's2' in string 's1', ignoring case. Returns 00165 /// NULL if 's2' cannot be found. 00166 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) { 00167 00168 // Are either strings NULL or empty? 00169 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0') 00170 return 0; 00171 00172 if (s1 == s2) 00173 return s1; 00174 00175 const char *I1=s1, *I2=s2; 00176 00177 while (*I1 != '\0' && *I2 != '\0' ) 00178 if (tolower(*I1) != tolower(*I2)) { // No match. Start over. 00179 ++s1; I1 = s1; I2 = s2; 00180 } 00181 else { // Character match. Advance to the next character. 00182 ++I1; ++I2; 00183 } 00184 00185 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'. 00186 return *I2 == '\0' ? s1 : 0; 00187 } 00188 00189 /// getToken - This function extracts one token from source, ignoring any 00190 /// leading characters that appear in the Delimiters string, and ending the 00191 /// token at any of the characters that appear in the Delimiters string. If 00192 /// there are no tokens in the source string, an empty string is returned. 00193 /// The Source source string is updated in place to remove the returned string 00194 /// and any delimiter prefix from it. 00195 std::string getToken(std::string &Source, 00196 const char *Delimiters = " \t\n\v\f\r"); 00197 00198 /// SplitString - Split up the specified string according to the specified 00199 /// delimiters, appending the result fragments to the output list. 00200 void SplitString(const std::string &Source, 00201 std::vector<std::string> &OutFragments, 00202 const char *Delimiters = " \t\n\v\f\r"); 00203 00204 /// UnescapeString - Modify the argument string, turning two character sequences 00205 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 00206 /// \num (where num is a 1-3 byte octal value). 00207 void UnescapeString(std::string &Str); 00208 00209 /// EscapeString - Modify the argument string, turning '\\' and anything that 00210 /// doesn't satisfy std::isprint into an escape sequence. 00211 void EscapeString(std::string &Str); 00212 00213 } // End llvm namespace 00214 00215 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.