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/APFloat.h"
19 #include "llvm/ADT/DenseMapInfo.h"
20 #include "llvm/ADT/StringRef.h"
26 template<typename T> class SmallVectorImpl;
28 /// hexdigit - Return the hexadecimal character for the
29 /// given number \arg X (which should be less than 16).
30 static inline char hexdigit(unsigned X, bool LowerCase = false) {
31 const char HexChar = LowerCase ? 'a' : 'A';
32 return X < 10 ? '0' + X : HexChar + X - 10;
35 /// utohex_buffer - Emit the specified number into the buffer specified by
36 /// BufferEnd, returning a pointer to the start of the string. This can be used
37 /// like this: (note that the buffer must be large enough to handle any number):
39 /// printf("0x%s", utohex_buffer(X, Buffer+40));
41 /// This should only be used with unsigned types.
43 template<typename IntTy>
44 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
45 char *BufPtr = BufferEnd;
46 *--BufPtr = 0; // Null terminate buffer.
48 *--BufPtr = '0'; // Handle special case.
53 unsigned char Mod = static_cast<unsigned char>(X) & 15;
54 *--BufPtr = hexdigit(Mod);
60 static inline std::string utohexstr(uint64_t X) {
62 return utohex_buffer(X, Buffer+17);
65 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
67 char *BufPtr = Buffer+11;
69 if (X == 0) *--BufPtr = '0'; // Handle special case...
72 *--BufPtr = '0' + char(X % 10);
76 if (isNeg) *--BufPtr = '-'; // Add negative sign...
78 return std::string(BufPtr, Buffer+11);
81 static inline std::string utostr(uint64_t X, bool isNeg = false) {
83 char *BufPtr = Buffer+21;
85 if (X == 0) *--BufPtr = '0'; // Handle special case...
88 *--BufPtr = '0' + char(X % 10);
92 if (isNeg) *--BufPtr = '-'; // Add negative sign...
93 return std::string(BufPtr, Buffer+21);
97 static inline std::string itostr(int64_t X) {
99 return utostr(static_cast<uint64_t>(-X), true);
101 return utostr(static_cast<uint64_t>(X));
104 static inline std::string ftostr(double V) {
106 sprintf(Buffer, "%20.6e", V);
108 while (*B == ' ') ++B;
112 static inline std::string ftostr(const APFloat& V) {
113 if (&V.getSemantics() == &APFloat::IEEEdouble)
114 return ftostr(V.convertToDouble());
115 else if (&V.getSemantics() == &APFloat::IEEEsingle)
116 return ftostr((double)V.convertToFloat());
117 return "<unknown format in ftostr>"; // error
120 static inline std::string LowercaseString(const std::string &S) {
121 std::string result(S);
122 for (unsigned i = 0; i < S.length(); ++i)
123 if (isupper(result[i]))
124 result[i] = char(tolower(result[i]));
128 static inline std::string UppercaseString(const std::string &S) {
129 std::string result(S);
130 for (unsigned i = 0; i < S.length(); ++i)
131 if (islower(result[i]))
132 result[i] = char(toupper(result[i]));
136 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
137 /// occurrence of string 's1' in string 's2', ignoring case. Returns
138 /// the offset of s2 in s1 or npos if s2 cannot be found.
139 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
141 /// getToken - This function extracts one token from source, ignoring any
142 /// leading characters that appear in the Delimiters string, and ending the
143 /// token at any of the characters that appear in the Delimiters string. If
144 /// there are no tokens in the source string, an empty string is returned.
145 /// The function returns a pair containing the extracted token and the
146 /// remaining tail string.
147 std::pair<StringRef, StringRef> getToken(StringRef Source,
148 StringRef Delimiters = " \t\n\v\f\r");
150 /// SplitString - Split up the specified string according to the specified
151 /// delimiters, appending the result fragments to the output list.
152 void SplitString(StringRef Source,
153 SmallVectorImpl<StringRef> &OutFragments,
154 StringRef Delimiters = " \t\n\v\f\r");
156 /// HashString - Hash function for strings.
158 /// This is the Bernstein hash function.
160 // FIXME: Investigate whether a modified bernstein hash function performs
161 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
163 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
164 for (unsigned i = 0, e = Str.size(); i != e; ++i)
165 Result = Result * 33 + Str[i];
169 } // End llvm namespace