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