Turns out llvm-gcc still uses SplitString with a vector. Add it back until I
[oota-llvm.git] / include / llvm / ADT / StringExtras.h
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/System/DataTypes.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <cctype>
21 #include <cstdio>
22 #include <string>
23 #include <vector>
24
25 namespace llvm {
26 template<typename T> class SmallVectorImpl;
27
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;
32 }
33
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):
37 ///    char Buffer[40];
38 ///    printf("0x%s", utohex_buffer(X, Buffer+40));
39 ///
40 /// This should only be used with unsigned types.
41 ///
42 template<typename IntTy>
43 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
44   char *BufPtr = BufferEnd;
45   *--BufPtr = 0;      // Null terminate buffer.
46   if (X == 0) {
47     *--BufPtr = '0';  // Handle special case.
48     return BufPtr;
49   }
50
51   while (X) {
52     unsigned char Mod = static_cast<unsigned char>(X) & 15;
53     *--BufPtr = hexdigit(Mod);
54     X >>= 4;
55   }
56   return BufPtr;
57 }
58
59 static inline std::string utohexstr(uint64_t X) {
60   char Buffer[40];
61   return utohex_buffer(X, Buffer+40);
62 }
63
64 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
65   char Buffer[20];
66   char *BufPtr = Buffer+19;
67
68   *BufPtr = 0;                  // Null terminate buffer...
69   if (X == 0) *--BufPtr = '0';  // Handle special case...
70
71   while (X) {
72     *--BufPtr = '0' + char(X % 10);
73     X /= 10;
74   }
75
76   if (isNeg) *--BufPtr = '-';   // Add negative sign...
77
78   return std::string(BufPtr);
79 }
80
81 static inline std::string utostr(uint64_t X, bool isNeg = false) {
82   if (X == uint32_t(X))
83     return utostr_32(uint32_t(X), isNeg);
84
85   char Buffer[40];
86   char *BufPtr = Buffer+39;
87
88   *BufPtr = 0;                  // Null terminate buffer...
89   if (X == 0) *--BufPtr = '0';  // Handle special case...
90
91   while (X) {
92     *--BufPtr = '0' + char(X % 10);
93     X /= 10;
94   }
95
96   if (isNeg) *--BufPtr = '-';   // Add negative sign...
97   return std::string(BufPtr);
98 }
99
100
101 static inline std::string itostr(int64_t X) {
102   if (X < 0)
103     return utostr(static_cast<uint64_t>(-X), true);
104   else
105     return utostr(static_cast<uint64_t>(X));
106 }
107
108 static inline std::string ftostr(double V) {
109   char Buffer[200];
110   sprintf(Buffer, "%20.6e", V);
111   char *B = Buffer;
112   while (*B == ' ') ++B;
113   return B;
114 }
115
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
122 }
123
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]));
129   return result;
130 }
131
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]));
137   return result;
138 }
139
140 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
141 /// case.
142 static inline bool StringsEqualNoCase(const std::string &LHS,
143                                       const std::string &RHS) {
144   if (LHS.size() != RHS.size()) return false;
145   for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
146     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
147   return true;
148 }
149
150 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
151 /// case.
152 static inline bool StringsEqualNoCase(const std::string &LHS,
153                                       const char *RHS) {
154   for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
155     if (RHS[i] == 0) return false;  // RHS too short.
156     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
157   }
158   return RHS[LHS.size()] == 0;  // Not too long?
159 }
160   
161 /// StringsEqualNoCase - Return true if the two null-terminated C strings are
162 ///  equal, ignoring
163
164 static inline bool StringsEqualNoCase(const char *LHS, const char *RHS,
165                                       unsigned len) {
166
167   for (unsigned i = 0; i < len; ++i) {
168     if (tolower(LHS[i]) != tolower(RHS[i]))
169       return false;
170     
171     // If RHS[i] == 0 then LHS[i] == 0 or otherwise we would have returned
172     // at the previous branch as tolower('\0') == '\0'.
173     if (RHS[i] == 0)
174       return true;
175   }
176   
177   return true;
178 }
179
180 /// CStrInCStrNoCase - Portable version of strcasestr.  Locates the first
181 ///  occurance of c-string 's2' in string 's1', ignoring case.  Returns
182 ///  NULL if 's2' cannot be found.
183 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
184
185   // Are either strings NULL or empty?
186   if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
187     return 0;
188
189   if (s1 == s2)
190     return s1;
191
192   const char *I1=s1, *I2=s2;
193
194   while (*I1 != '\0' && *I2 != '\0' )
195     if (tolower(*I1) != tolower(*I2)) { // No match.  Start over.
196       ++s1; I1 = s1; I2 = s2;
197     }
198     else { // Character match.  Advance to the next character.
199       ++I1; ++I2;
200     }
201
202   // If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
203   return *I2 == '\0' ? s1 : 0;
204 }
205
206 /// getToken - This function extracts one token from source, ignoring any
207 /// leading characters that appear in the Delimiters string, and ending the
208 /// token at any of the characters that appear in the Delimiters string.  If
209 /// there are no tokens in the source string, an empty string is returned.
210 /// The function returns a pair containing the extracted token and the
211 /// remaining tail string.
212 std::pair<StringRef, StringRef> getToken(StringRef Source,
213                                          StringRef Delimiters = " \t\n\v\f\r");
214
215 /// SplitString - Split up the specified string according to the specified
216 /// delimiters, appending the result fragments to the output list.
217 void SplitString(StringRef Source,
218                  SmallVectorImpl<StringRef> &OutFragments,
219                  StringRef Delimiters = " \t\n\v\f\r");
220
221 // FIXME: remove when llvm-gcc doesn't use this anymore
222 void SplitString(StringRef Source,
223                  std::vector<std::string> &OutFragments,
224                  StringRef Delimiters = " \t\n\v\f\r");
225
226 /// HashString - Hash funtion for strings.
227 ///
228 /// This is the Bernstein hash function.
229 //
230 // FIXME: Investigate whether a modified bernstein hash function performs
231 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
232 //   X*33+c -> X*33^c
233 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
234   for (unsigned i = 0, e = Str.size(); i != e; ++i)
235     Result = Result * 33 + Str[i];
236   return Result;
237 }
238
239 } // End llvm namespace
240
241 #endif