1 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains some functions that are useful when dealing with strings.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_STRINGEXTRAS_H
15 #define LLVM_ADT_STRINGEXTRAS_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
21 template<typename T> class SmallVectorImpl;
23 /// hexdigit - Return the hexadecimal character for the
24 /// given number \p X (which should be less than 16).
25 static inline char hexdigit(unsigned X, bool LowerCase = false) {
26 const char HexChar = LowerCase ? 'a' : 'A';
27 return X < 10 ? '0' + X : HexChar + X - 10;
30 /// Interpret the given character \p C as a hexadecimal digit and return its
33 /// If \p C is not a valid hex digit, -1U is returned.
34 static inline unsigned hexDigitValue(char C) {
35 if (C >= '0' && C <= '9') return C-'0';
36 if (C >= 'a' && C <= 'f') return C-'a'+10U;
37 if (C >= 'A' && C <= 'F') return C-'A'+10U;
41 /// utohex_buffer - Emit the specified number into the buffer specified by
42 /// BufferEnd, returning a pointer to the start of the string. This can be used
43 /// like this: (note that the buffer must be large enough to handle any number):
45 /// printf("0x%s", utohex_buffer(X, Buffer+40));
47 /// This should only be used with unsigned types.
49 template<typename IntTy>
50 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
51 char *BufPtr = BufferEnd;
52 *--BufPtr = 0; // Null terminate buffer.
54 *--BufPtr = '0'; // Handle special case.
59 unsigned char Mod = static_cast<unsigned char>(X) & 15;
60 *--BufPtr = hexdigit(Mod);
66 static inline std::string utohexstr(uint64_t X) {
68 return utohex_buffer(X, Buffer+17);
71 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
73 char *BufPtr = Buffer+11;
75 if (X == 0) *--BufPtr = '0'; // Handle special case...
78 *--BufPtr = '0' + char(X % 10);
82 if (isNeg) *--BufPtr = '-'; // Add negative sign...
84 return std::string(BufPtr, Buffer+11);
87 static inline std::string utostr(uint64_t X, bool isNeg = false) {
89 char *BufPtr = Buffer+21;
91 if (X == 0) *--BufPtr = '0'; // Handle special case...
94 *--BufPtr = '0' + char(X % 10);
98 if (isNeg) *--BufPtr = '-'; // Add negative sign...
99 return std::string(BufPtr, Buffer+21);
103 static inline std::string itostr(int64_t X) {
105 return utostr(static_cast<uint64_t>(-X), true);
107 return utostr(static_cast<uint64_t>(X));
110 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
111 /// occurrence of string 's1' in string 's2', ignoring case. Returns
112 /// the offset of s2 in s1 or npos if s2 cannot be found.
113 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
115 /// getToken - This function extracts one token from source, ignoring any
116 /// leading characters that appear in the Delimiters string, and ending the
117 /// token at any of the characters that appear in the Delimiters string. If
118 /// there are no tokens in the source string, an empty string is returned.
119 /// The function returns a pair containing the extracted token and the
120 /// remaining tail string.
121 std::pair<StringRef, StringRef> getToken(StringRef Source,
122 StringRef Delimiters = " \t\n\v\f\r");
124 /// SplitString - Split up the specified string according to the specified
125 /// delimiters, appending the result fragments to the output list.
126 void SplitString(StringRef Source,
127 SmallVectorImpl<StringRef> &OutFragments,
128 StringRef Delimiters = " \t\n\v\f\r");
130 /// HashString - Hash function for strings.
132 /// This is the Bernstein hash function.
134 // FIXME: Investigate whether a modified bernstein hash function performs
135 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
137 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
138 for (unsigned i = 0, e = Str.size(); i != e; ++i)
139 Result = Result * 33 + (unsigned char)Str[i];
143 /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
144 static inline StringRef getOrdinalSuffix(unsigned Val) {
145 // It is critically important that we do this perfectly for
146 // user-written sequences with over 100 elements.
157 default: return "th";
162 } // End llvm namespace