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/ADT/StringRef.h" 00018 #include "llvm/Support/DataTypes.h" 00019 00020 namespace llvm { 00021 template<typename T> class SmallVectorImpl; 00022 00023 /// hexdigit - Return the hexadecimal character for the 00024 /// given number \p X (which should be less than 16). 00025 static inline char hexdigit(unsigned X, bool LowerCase = false) { 00026 const char HexChar = LowerCase ? 'a' : 'A'; 00027 return X < 10 ? '0' + X : HexChar + X - 10; 00028 } 00029 00030 /// Interpret the given character \p C as a hexadecimal digit and return its 00031 /// value. 00032 /// 00033 /// If \p C is not a valid hex digit, -1U is returned. 00034 static inline unsigned hexDigitValue(char C) { 00035 if (C >= '0' && C <= '9') return C-'0'; 00036 if (C >= 'a' && C <= 'f') return C-'a'+10U; 00037 if (C >= 'A' && C <= 'F') return C-'A'+10U; 00038 return -1U; 00039 } 00040 00041 /// utohex_buffer - Emit the specified number into the buffer specified by 00042 /// BufferEnd, returning a pointer to the start of the string. This can be used 00043 /// like this: (note that the buffer must be large enough to handle any number): 00044 /// char Buffer[40]; 00045 /// printf("0x%s", utohex_buffer(X, Buffer+40)); 00046 /// 00047 /// This should only be used with unsigned types. 00048 /// 00049 template<typename IntTy> 00050 static inline char *utohex_buffer(IntTy X, char *BufferEnd) { 00051 char *BufPtr = BufferEnd; 00052 *--BufPtr = 0; // Null terminate buffer. 00053 if (X == 0) { 00054 *--BufPtr = '0'; // Handle special case. 00055 return BufPtr; 00056 } 00057 00058 while (X) { 00059 unsigned char Mod = static_cast<unsigned char>(X) & 15; 00060 *--BufPtr = hexdigit(Mod); 00061 X >>= 4; 00062 } 00063 return BufPtr; 00064 } 00065 00066 static inline std::string utohexstr(uint64_t X) { 00067 char Buffer[17]; 00068 return utohex_buffer(X, Buffer+17); 00069 } 00070 00071 static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 00072 char Buffer[11]; 00073 char *BufPtr = Buffer+11; 00074 00075 if (X == 0) *--BufPtr = '0'; // Handle special case... 00076 00077 while (X) { 00078 *--BufPtr = '0' + char(X % 10); 00079 X /= 10; 00080 } 00081 00082 if (isNeg) *--BufPtr = '-'; // Add negative sign... 00083 00084 return std::string(BufPtr, Buffer+11); 00085 } 00086 00087 static inline std::string utostr(uint64_t X, bool isNeg = false) { 00088 char Buffer[21]; 00089 char *BufPtr = Buffer+21; 00090 00091 if (X == 0) *--BufPtr = '0'; // Handle special case... 00092 00093 while (X) { 00094 *--BufPtr = '0' + char(X % 10); 00095 X /= 10; 00096 } 00097 00098 if (isNeg) *--BufPtr = '-'; // Add negative sign... 00099 return std::string(BufPtr, Buffer+21); 00100 } 00101 00102 00103 static inline std::string itostr(int64_t X) { 00104 if (X < 0) 00105 return utostr(static_cast<uint64_t>(-X), true); 00106 else 00107 return utostr(static_cast<uint64_t>(X)); 00108 } 00109 00110 /// StrInStrNoCase - Portable version of strcasestr. Locates the first 00111 /// occurrence of string 's1' in string 's2', ignoring case. Returns 00112 /// the offset of s2 in s1 or npos if s2 cannot be found. 00113 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2); 00114 00115 /// getToken - This function extracts one token from source, ignoring any 00116 /// leading characters that appear in the Delimiters string, and ending the 00117 /// token at any of the characters that appear in the Delimiters string. If 00118 /// there are no tokens in the source string, an empty string is returned. 00119 /// The function returns a pair containing the extracted token and the 00120 /// remaining tail string. 00121 std::pair<StringRef, StringRef> getToken(StringRef Source, 00122 StringRef Delimiters = " \t\n\v\f\r"); 00123 00124 /// SplitString - Split up the specified string according to the specified 00125 /// delimiters, appending the result fragments to the output list. 00126 void SplitString(StringRef Source, 00127 SmallVectorImpl<StringRef> &OutFragments, 00128 StringRef Delimiters = " \t\n\v\f\r"); 00129 00130 /// HashString - Hash function for strings. 00131 /// 00132 /// This is the Bernstein hash function. 00133 // 00134 // FIXME: Investigate whether a modified bernstein hash function performs 00135 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 00136 // X*33+c -> X*33^c 00137 static inline unsigned HashString(StringRef Str, unsigned Result = 0) { 00138 for (unsigned i = 0, e = Str.size(); i != e; ++i) 00139 Result = Result * 33 + (unsigned char)Str[i]; 00140 return Result; 00141 } 00142 00143 /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th). 00144 static inline StringRef getOrdinalSuffix(unsigned Val) { 00145 // It is critically important that we do this perfectly for 00146 // user-written sequences with over 100 elements. 00147 switch (Val % 100) { 00148 case 11: 00149 case 12: 00150 case 13: 00151 return "th"; 00152 default: 00153 switch (Val % 10) { 00154 case 1: return "st"; 00155 case 2: return "nd"; 00156 case 3: return "rd"; 00157 default: return "th"; 00158 } 00159 } 00160 } 00161 00162 } // End llvm namespace 00163 00164 #endif