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/System/DataTypes.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/StringRef.h"
26 template<typename T> class SmallVectorImpl;
28 /// hexdigit - Return the (uppercase) hexadecimal character for the
29 /// given number \arg X (which should be less than 16).
30 static inline char hexdigit(unsigned X) {
31 return X < 10 ? '0' + X : 'A' + X - 10;
34 /// utohex_buffer - Emit the specified number into the buffer specified by
35 /// BufferEnd, returning a pointer to the start of the string. This can be used
36 /// like this: (note that the buffer must be large enough to handle any number):
38 /// printf("0x%s", utohex_buffer(X, Buffer+40));
40 /// This should only be used with unsigned types.
42 template<typename IntTy>
43 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
44 char *BufPtr = BufferEnd;
45 *--BufPtr = 0; // Null terminate buffer.
47 *--BufPtr = '0'; // Handle special case.
52 unsigned char Mod = static_cast<unsigned char>(X) & 15;
53 *--BufPtr = hexdigit(Mod);
59 static inline std::string utohexstr(uint64_t X) {
61 return utohex_buffer(X, Buffer+40);
64 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
66 char *BufPtr = Buffer+19;
68 *BufPtr = 0; // Null terminate buffer...
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);
81 static inline std::string utostr(uint64_t X, bool isNeg = false) {
83 return utostr_32(uint32_t(X), isNeg);
86 char *BufPtr = Buffer+39;
88 *BufPtr = 0; // Null terminate buffer...
89 if (X == 0) *--BufPtr = '0'; // Handle special case...
92 *--BufPtr = '0' + char(X % 10);
96 if (isNeg) *--BufPtr = '-'; // Add negative sign...
97 return std::string(BufPtr);
101 static inline std::string itostr(int64_t X) {
103 return utostr(static_cast<uint64_t>(-X), true);
105 return utostr(static_cast<uint64_t>(X));
108 static inline std::string ftostr(double V) {
110 sprintf(Buffer, "%20.6e", V);
112 while (*B == ' ') ++B;
116 static inline std::string ftostr(const APFloat& V) {
117 if (&V.getSemantics() == &APFloat::IEEEdouble)
118 return ftostr(V.convertToDouble());
119 else if (&V.getSemantics() == &APFloat::IEEEsingle)
120 return ftostr((double)V.convertToFloat());
121 return "<unknown format in ftostr>"; // error
124 static inline std::string LowercaseString(const std::string &S) {
125 std::string result(S);
126 for (unsigned i = 0; i < S.length(); ++i)
127 if (isupper(result[i]))
128 result[i] = char(tolower(result[i]));
132 static inline std::string UppercaseString(const std::string &S) {
133 std::string result(S);
134 for (unsigned i = 0; i < S.length(); ++i)
135 if (islower(result[i]))
136 result[i] = char(toupper(result[i]));
140 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
141 /// occurrence of string 's1' in string 's2', ignoring case. Returns
142 /// the offset of s2 in s1 or npos if s2 cannot be found.
143 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
145 /// getToken - This function extracts one token from source, ignoring any
146 /// leading characters that appear in the Delimiters string, and ending the
147 /// token at any of the characters that appear in the Delimiters string. If
148 /// there are no tokens in the source string, an empty string is returned.
149 /// The function returns a pair containing the extracted token and the
150 /// remaining tail string.
151 std::pair<StringRef, StringRef> getToken(StringRef Source,
152 StringRef Delimiters = " \t\n\v\f\r");
154 /// SplitString - Split up the specified string according to the specified
155 /// delimiters, appending the result fragments to the output list.
156 void SplitString(StringRef Source,
157 SmallVectorImpl<StringRef> &OutFragments,
158 StringRef Delimiters = " \t\n\v\f\r");
160 /// HashString - Hash funtion for strings.
162 /// This is the Bernstein hash function.
164 // FIXME: Investigate whether a modified bernstein hash function performs
165 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
167 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
168 for (unsigned i = 0, e = Str.size(); i != e; ++i)
169 Result = Result * 33 + Str[i];
173 } // End llvm namespace