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 = (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(unsigned long long X, bool isNeg = false) { 00043 char Buffer[40]; 00044 char *BufPtr = Buffer+39; 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 return std::string(BufPtr); 00056 } 00057 00058 static inline std::string utostr(unsigned long X, bool isNeg = false) { 00059 return utostr(static_cast<unsigned long long>(X), isNeg); 00060 } 00061 00062 static inline std::string utostr(unsigned 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 itostr(long long 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 itostr(long X) { 00087 if (X < 0) 00088 return utostr(static_cast<uint64_t>(-X), true); 00089 else 00090 return utostr(static_cast<uint64_t>(X)); 00091 } 00092 00093 static inline std::string itostr(int X) { 00094 if (X < 0) 00095 return utostr(static_cast<unsigned>(-X), true); 00096 else 00097 return utostr(static_cast<unsigned>(X)); 00098 } 00099 00100 static inline std::string ftostr(double V) { 00101 char Buffer[200]; 00102 sprintf(Buffer, "%20.6e", V); 00103 return Buffer; 00104 } 00105 00106 static inline std::string LowercaseString(const std::string &S) { 00107 std::string result(S); 00108 for (unsigned i = 0; i < S.length(); ++i) 00109 if (isupper(result[i])) 00110 result[i] = (char)tolower(result[i]); 00111 return result; 00112 } 00113 00114 /// getToken - This function extracts one token from source, ignoring any 00115 /// leading characters that appear in the Delimiters string, and ending the 00116 /// token at any of the characters that appear in the Delimiters string. If 00117 /// there are no tokens in the source string, an empty string is returned. 00118 /// The Source source string is updated in place to remove the returned string 00119 /// and any delimiter prefix from it. 00120 std::string getToken(std::string &Source, 00121 const char *Delimiters = " \t\n\v\f\r"); 00122 00123 } // End llvm namespace 00124 00125 #endif