StringExtras.h revision 1fd2e6d84e271c17d7ae8778058c13de517dbc17
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 26static inline std::string utohexstr(uint64_t X) { 27 char Buffer[40]; 28 char *BufPtr = Buffer+39; 29 30 *BufPtr = 0; // Null terminate buffer... 31 if (X == 0) *--BufPtr = '0'; // Handle special case... 32 33 while (X) { 34 unsigned char Mod = static_cast<unsigned char>(X) & 15; 35 if (Mod < 10) 36 *--BufPtr = '0' + Mod; 37 else 38 *--BufPtr = 'A' + Mod-10; 39 X >>= 4; 40 } 41 return std::string(BufPtr); 42} 43 44static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 45 char Buffer[20]; 46 char *BufPtr = Buffer+19; 47 48 *BufPtr = 0; // Null terminate buffer... 49 if (X == 0) *--BufPtr = '0'; // Handle special case... 50 51 while (X) { 52 *--BufPtr = '0' + char(X % 10); 53 X /= 10; 54 } 55 56 if (isNeg) *--BufPtr = '-'; // Add negative sign... 57 58 return std::string(BufPtr); 59} 60 61static inline std::string utostr(uint64_t X, bool isNeg = false) { 62 if (X == uint32_t(X)) 63 return utostr_32(uint32_t(X), isNeg); 64 65 char Buffer[40]; 66 char *BufPtr = Buffer+39; 67 68 *BufPtr = 0; // Null terminate buffer... 69 if (X == 0) *--BufPtr = '0'; // Handle special case... 70 71 while (X) { 72 *--BufPtr = '0' + char(X % 10); 73 X /= 10; 74 } 75 76 if (isNeg) *--BufPtr = '-'; // Add negative sign... 77 return std::string(BufPtr); 78} 79 80 81static inline std::string itostr(int64_t X) { 82 if (X < 0) 83 return utostr(static_cast<uint64_t>(-X), true); 84 else 85 return utostr(static_cast<uint64_t>(X)); 86} 87 88static inline std::string itohexstr(int64_t X) { 89 return utohexstr(static_cast<uint64_t>(X)); 90} 91 92static inline std::string ftostr(double V) { 93 char Buffer[200]; 94 sprintf(Buffer, "%20.6e", V); 95 char *B = Buffer; 96 while (*B == ' ') ++B; 97 return B; 98} 99 100static inline std::string ftostr(const APFloat& V) { 101 if (&V.getSemantics() == &APFloat::IEEEdouble) 102 return ftostr(V.convertToDouble()); 103 else if (&V.getSemantics() == &APFloat::IEEEsingle) 104 return ftostr((double)V.convertToFloat()); 105 return "<unknown format in ftostr>"; // error 106} 107 108static inline std::string LowercaseString(const std::string &S) { 109 std::string result(S); 110 for (unsigned i = 0; i < S.length(); ++i) 111 if (isupper(result[i])) 112 result[i] = char(tolower(result[i])); 113 return result; 114} 115 116static inline std::string UppercaseString(const std::string &S) { 117 std::string result(S); 118 for (unsigned i = 0; i < S.length(); ++i) 119 if (islower(result[i])) 120 result[i] = char(toupper(result[i])); 121 return result; 122} 123 124/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 125/// case. 126static inline bool StringsEqualNoCase(const std::string &LHS, 127 const std::string &RHS) { 128 if (LHS.size() != RHS.size()) return false; 129 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) 130 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 131 return true; 132} 133 134/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 135/// case. 136static inline bool StringsEqualNoCase(const std::string &LHS, 137 const char *RHS) { 138 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) { 139 if (RHS[i] == 0) return false; // RHS too short. 140 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 141 } 142 return RHS[LHS.size()] == 0; // Not too long? 143} 144 145/// CStrInCStrNoCase - Portable version of strcasestr. Locates the first 146/// occurance of c-string 's2' in string 's1', ignoring case. Returns 147/// NULL if 's2' cannot be found. 148static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) { 149 150 // Are either strings NULL or empty? 151 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0') 152 return 0; 153 154 if (s1 == s2) 155 return s1; 156 157 const char *I1=s1, *I2=s2; 158 159 while (*I1 != '\0' || *I2 != '\0' ) 160 if (tolower(*I1) != tolower(*I2)) { // No match. Start over. 161 ++s1; I1 = s1; I2 = s2; 162 } 163 else { // Character match. Advance to the next character. 164 ++I1; ++I2; 165 } 166 167 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'. 168 return *I2 == '\0' ? s1 : 0; 169} 170 171/// getToken - This function extracts one token from source, ignoring any 172/// leading characters that appear in the Delimiters string, and ending the 173/// token at any of the characters that appear in the Delimiters string. If 174/// there are no tokens in the source string, an empty string is returned. 175/// The Source source string is updated in place to remove the returned string 176/// and any delimiter prefix from it. 177std::string getToken(std::string &Source, 178 const char *Delimiters = " \t\n\v\f\r"); 179 180/// SplitString - Split up the specified string according to the specified 181/// delimiters, appending the result fragments to the output list. 182void SplitString(const std::string &Source, 183 std::vector<std::string> &OutFragments, 184 const char *Delimiters = " \t\n\v\f\r"); 185 186/// UnescapeString - Modify the argument string, turning two character sequences 187/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 188/// \num (where num is a 1-3 byte octal value). 189void UnescapeString(std::string &Str); 190 191/// EscapeString - Modify the argument string, turning '\\' and anything that 192/// doesn't satisfy std::isprint into an escape sequence. 193void EscapeString(std::string &Str); 194 195} // End llvm namespace 196 197#endif 198