LLVM API Documentation
00001 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 <cctype> 00019 #include <cstdio> 00020 #include <string> 00021 00022 namespace llvm { 00023 00024 static inline std::string utohexstr(uint64_t X) { 00025 char Buffer[40]; 00026 char *BufPtr = Buffer+39; 00027 00028 *BufPtr = 0; // Null terminate buffer... 00029 if (X == 0) *--BufPtr = '0'; // Handle special case... 00030 00031 while (X) { 00032 unsigned char Mod = static_cast<unsigned char>(X) & 15; 00033 if (Mod < 10) 00034 *--BufPtr = '0' + Mod; 00035 else 00036 *--BufPtr = 'A' + Mod-10; 00037 X >>= 4; 00038 } 00039 return std::string(BufPtr); 00040 } 00041 00042 static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 00043 char Buffer[20]; 00044 char *BufPtr = Buffer+19; 00045 00046 *BufPtr = 0; // Null terminate buffer... 00047 if (X == 0) *--BufPtr = '0'; // Handle special case... 00048 00049 while (X) { 00050 *--BufPtr = '0' + char(X % 10); 00051 X /= 10; 00052 } 00053 00054 if (isNeg) *--BufPtr = '-'; // Add negative sign... 00055 00056 return std::string(BufPtr); 00057 } 00058 00059 static inline std::string utostr(uint64_t X, bool isNeg = false) { 00060 if (X == uint32_t(X)) 00061 return utostr_32(uint32_t(X), isNeg); 00062 00063 char Buffer[40]; 00064 char *BufPtr = Buffer+39; 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 return std::string(BufPtr); 00076 } 00077 00078 00079 static inline std::string itostr(int64_t X) { 00080 if (X < 0) 00081 return utostr(static_cast<uint64_t>(-X), true); 00082 else 00083 return utostr(static_cast<uint64_t>(X)); 00084 } 00085 00086 static inline std::string ftostr(double V) { 00087 char Buffer[200]; 00088 sprintf(Buffer, "%20.6e", V); 00089 char *B = Buffer; 00090 while (*B == ' ') ++B; 00091 return B; 00092 } 00093 00094 static inline std::string LowercaseString(const std::string &S) { 00095 std::string result(S); 00096 for (unsigned i = 0; i < S.length(); ++i) 00097 if (isupper(result[i])) 00098 result[i] = char(tolower(result[i])); 00099 return result; 00100 } 00101 00102 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00103 /// case. 00104 static inline bool StringsEqualNoCase(const std::string &LHS, 00105 const std::string &RHS) { 00106 if (LHS.size() != RHS.size()) return false; 00107 for (unsigned i = 0, e = LHS.size(); i != e; ++i) 00108 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00109 return true; 00110 } 00111 00112 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring 00113 /// case. 00114 static inline bool StringsEqualNoCase(const std::string &LHS, 00115 const char *RHS) { 00116 for (unsigned i = 0, e = LHS.size(); i != e; ++i) { 00117 if (RHS[i] == 0) return false; // RHS too short. 00118 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 00119 } 00120 return RHS[LHS.size()] == 0; // Not too long? 00121 } 00122 00123 /// getToken - This function extracts one token from source, ignoring any 00124 /// leading characters that appear in the Delimiters string, and ending the 00125 /// token at any of the characters that appear in the Delimiters string. If 00126 /// there are no tokens in the source string, an empty string is returned. 00127 /// The Source source string is updated in place to remove the returned string 00128 /// and any delimiter prefix from it. 00129 std::string getToken(std::string &Source, 00130 const char *Delimiters = " \t\n\v\f\r"); 00131 00132 /// UnescapeString - Modify the argument string, turning two character sequences 00133 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 00134 /// \num (where num is a 1-3 byte octal value). 00135 void UnescapeString(std::string &Str); 00136 00137 /// EscapeString - Modify the argument string, turning '\\' and anything that 00138 /// doesn't satisfy std::isprint into an escape sequence. 00139 void EscapeString(std::string &Str); 00140 00141 } // End llvm namespace 00142 00143 #endif