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/Support/DataTypes.h"
18 #include "llvm/ADT/StringRef.h"
21 template<typename T> class SmallVectorImpl;
23 /// hexdigit - Return the hexadecimal character for the
24 /// given number \arg 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 /// utohex_buffer - Emit the specified number into the buffer specified by
31 /// BufferEnd, returning a pointer to the start of the string. This can be used
32 /// like this: (note that the buffer must be large enough to handle any number):
34 /// printf("0x%s", utohex_buffer(X, Buffer+40));
36 /// This should only be used with unsigned types.
38 template<typename IntTy>
39 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
40 char *BufPtr = BufferEnd;
41 *--BufPtr = 0; // Null terminate buffer.
43 *--BufPtr = '0'; // Handle special case.
48 unsigned char Mod = static_cast<unsigned char>(X) & 15;
49 *--BufPtr = hexdigit(Mod);
55 static inline std::string utohexstr(uint64_t X) {
57 return utohex_buffer(X, Buffer+17);
60 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
62 char *BufPtr = Buffer+11;
64 if (X == 0) *--BufPtr = '0'; // Handle special case...
67 *--BufPtr = '0' + char(X % 10);
71 if (isNeg) *--BufPtr = '-'; // Add negative sign...
73 return std::string(BufPtr, Buffer+11);
76 static inline std::string utostr(uint64_t X, bool isNeg = false) {
78 char *BufPtr = Buffer+21;
80 if (X == 0) *--BufPtr = '0'; // Handle special case...
83 *--BufPtr = '0' + char(X % 10);
87 if (isNeg) *--BufPtr = '-'; // Add negative sign...
88 return std::string(BufPtr, Buffer+21);
92 static inline std::string itostr(int64_t X) {
94 return utostr(static_cast<uint64_t>(-X), true);
96 return utostr(static_cast<uint64_t>(X));
99 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
100 /// occurrence of string 's1' in string 's2', ignoring case. Returns
101 /// the offset of s2 in s1 or npos if s2 cannot be found.
102 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
104 /// getToken - This function extracts one token from source, ignoring any
105 /// leading characters that appear in the Delimiters string, and ending the
106 /// token at any of the characters that appear in the Delimiters string. If
107 /// there are no tokens in the source string, an empty string is returned.
108 /// The function returns a pair containing the extracted token and the
109 /// remaining tail string.
110 std::pair<StringRef, StringRef> getToken(StringRef Source,
111 StringRef Delimiters = " \t\n\v\f\r");
113 /// SplitString - Split up the specified string according to the specified
114 /// delimiters, appending the result fragments to the output list.
115 void SplitString(StringRef Source,
116 SmallVectorImpl<StringRef> &OutFragments,
117 StringRef Delimiters = " \t\n\v\f\r");
119 /// HashString - Hash function for strings.
121 /// This is the Bernstein hash function.
123 // FIXME: Investigate whether a modified bernstein hash function performs
124 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
126 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
127 for (unsigned i = 0, e = Str.size(); i != e; ++i)
128 Result = Result * 33 + Str[i];
132 } // End llvm namespace