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/ADT/StringRef.h" 18#include "llvm/Support/DataTypes.h" 19#include <iterator> 20 21namespace llvm { 22template<typename T> class SmallVectorImpl; 23 24/// hexdigit - Return the hexadecimal character for the 25/// given number \p X (which should be less than 16). 26static inline char hexdigit(unsigned X, bool LowerCase = false) { 27 const char HexChar = LowerCase ? 'a' : 'A'; 28 return X < 10 ? '0' + X : HexChar + X - 10; 29} 30 31/// Construct a string ref from a boolean. 32static inline StringRef toStringRef(bool B) { 33 return StringRef(B ? "true" : "false"); 34} 35 36/// Interpret the given character \p C as a hexadecimal digit and return its 37/// value. 38/// 39/// If \p C is not a valid hex digit, -1U is returned. 40static inline unsigned hexDigitValue(char C) { 41 if (C >= '0' && C <= '9') return C-'0'; 42 if (C >= 'a' && C <= 'f') return C-'a'+10U; 43 if (C >= 'A' && C <= 'F') return C-'A'+10U; 44 return -1U; 45} 46 47static inline std::string utohexstr(uint64_t X, bool LowerCase = false) { 48 char Buffer[17]; 49 char *BufPtr = std::end(Buffer); 50 51 if (X == 0) *--BufPtr = '0'; 52 53 while (X) { 54 unsigned char Mod = static_cast<unsigned char>(X) & 15; 55 *--BufPtr = hexdigit(Mod, LowerCase); 56 X >>= 4; 57 } 58 59 return std::string(BufPtr, std::end(Buffer)); 60} 61 62/// Convert buffer \p Input to its hexadecimal representation. 63/// The returned string is double the size of \p Input. 64static inline std::string toHex(StringRef Input) { 65 static const char *const LUT = "0123456789ABCDEF"; 66 size_t Length = Input.size(); 67 68 std::string Output; 69 Output.reserve(2 * Length); 70 for (size_t i = 0; i < Length; ++i) { 71 const unsigned char c = Input[i]; 72 Output.push_back(LUT[c >> 4]); 73 Output.push_back(LUT[c & 15]); 74 } 75 return Output; 76} 77 78static inline std::string utostr(uint64_t X, bool isNeg = false) { 79 char Buffer[21]; 80 char *BufPtr = std::end(Buffer); 81 82 if (X == 0) *--BufPtr = '0'; // Handle special case... 83 84 while (X) { 85 *--BufPtr = '0' + char(X % 10); 86 X /= 10; 87 } 88 89 if (isNeg) *--BufPtr = '-'; // Add negative sign... 90 return std::string(BufPtr, std::end(Buffer)); 91} 92 93 94static inline std::string itostr(int64_t X) { 95 if (X < 0) 96 return utostr(static_cast<uint64_t>(-X), true); 97 else 98 return utostr(static_cast<uint64_t>(X)); 99} 100 101/// StrInStrNoCase - Portable version of strcasestr. Locates the first 102/// occurrence of string 's1' in string 's2', ignoring case. Returns 103/// the offset of s2 in s1 or npos if s2 cannot be found. 104StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2); 105 106/// getToken - This function extracts one token from source, ignoring any 107/// leading characters that appear in the Delimiters string, and ending the 108/// token at any of the characters that appear in the Delimiters string. If 109/// there are no tokens in the source string, an empty string is returned. 110/// The function returns a pair containing the extracted token and the 111/// remaining tail string. 112std::pair<StringRef, StringRef> getToken(StringRef Source, 113 StringRef Delimiters = " \t\n\v\f\r"); 114 115/// SplitString - Split up the specified string according to the specified 116/// delimiters, appending the result fragments to the output list. 117void SplitString(StringRef Source, 118 SmallVectorImpl<StringRef> &OutFragments, 119 StringRef Delimiters = " \t\n\v\f\r"); 120 121/// HashString - Hash function for strings. 122/// 123/// This is the Bernstein hash function. 124// 125// FIXME: Investigate whether a modified bernstein hash function performs 126// better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx 127// X*33+c -> X*33^c 128static inline unsigned HashString(StringRef Str, unsigned Result = 0) { 129 for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i) 130 Result = Result * 33 + (unsigned char)Str[i]; 131 return Result; 132} 133 134/// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th). 135static inline StringRef getOrdinalSuffix(unsigned Val) { 136 // It is critically important that we do this perfectly for 137 // user-written sequences with over 100 elements. 138 switch (Val % 100) { 139 case 11: 140 case 12: 141 case 13: 142 return "th"; 143 default: 144 switch (Val % 10) { 145 case 1: return "st"; 146 case 2: return "nd"; 147 case 3: return "rd"; 148 default: return "th"; 149 } 150 } 151} 152 153template <typename IteratorT> 154inline std::string join_impl(IteratorT Begin, IteratorT End, 155 StringRef Separator, std::input_iterator_tag) { 156 std::string S; 157 if (Begin == End) 158 return S; 159 160 S += (*Begin); 161 while (++Begin != End) { 162 S += Separator; 163 S += (*Begin); 164 } 165 return S; 166} 167 168template <typename IteratorT> 169inline std::string join_impl(IteratorT Begin, IteratorT End, 170 StringRef Separator, std::forward_iterator_tag) { 171 std::string S; 172 if (Begin == End) 173 return S; 174 175 size_t Len = (std::distance(Begin, End) - 1) * Separator.size(); 176 for (IteratorT I = Begin; I != End; ++I) 177 Len += (*Begin).size(); 178 S.reserve(Len); 179 S += (*Begin); 180 while (++Begin != End) { 181 S += Separator; 182 S += (*Begin); 183 } 184 return S; 185} 186 187/// Joins the strings in the range [Begin, End), adding Separator between 188/// the elements. 189template <typename IteratorT> 190inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) { 191 typedef typename std::iterator_traits<IteratorT>::iterator_category tag; 192 return join_impl(Begin, End, Separator, tag()); 193} 194 195} // End llvm namespace 196 197#endif 198