Remove itohexstr, which only had one user.
[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/Support/DataTypes.h"
18 #include "llvm/ADT/APFloat.h"
19 #include <cctype>
20 #include <cstdio>
21 #include <string>
22 #include <vector>
23
24 namespace llvm {
25
26 /// hexdigit - Return the (uppercase) hexadecimal character for the
27 /// given number \arg X (which should be less than 16).
28 static inline char hexdigit(unsigned X) {
29   return X < 10 ? '0' + X : 'A' + X - 10;
30 }
31
32 /// utohex_buffer - Emit the specified number into the buffer specified by
33 /// BufferEnd, returning a pointer to the start of the string.  This can be used
34 /// like this: (note that the buffer must be large enough to handle any number):
35 ///    char Buffer[40];
36 ///    printf("0x%s", utohex_buffer(X, Buffer+40));
37 ///
38 /// This should only be used with unsigned types.
39 ///
40 template<typename IntTy>
41 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
42   char *BufPtr = BufferEnd;
43   *--BufPtr = 0;      // Null terminate buffer.
44   if (X == 0) {
45     *--BufPtr = '0';  // Handle special case.
46     return BufPtr;
47   }
48
49   while (X) {
50     unsigned char Mod = static_cast<unsigned char>(X) & 15;
51     *--BufPtr = hexdigit(Mod);
52     X >>= 4;
53   }
54   return BufPtr;
55 }
56
57 static inline std::string utohexstr(uint64_t X) {
58   char Buffer[40];
59   return utohex_buffer(X, Buffer+40);
60 }
61
62 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
63   char Buffer[20];
64   char *BufPtr = Buffer+19;
65
66   *BufPtr = 0;                  // Null terminate buffer...
67   if (X == 0) *--BufPtr = '0';  // Handle special case...
68
69   while (X) {
70     *--BufPtr = '0' + char(X % 10);
71     X /= 10;
72   }
73
74   if (isNeg) *--BufPtr = '-';   // Add negative sign...
75
76   return std::string(BufPtr);
77 }
78
79 static inline std::string utostr(uint64_t X, bool isNeg = false) {
80   if (X == uint32_t(X))
81     return utostr_32(uint32_t(X), isNeg);
82
83   char Buffer[40];
84   char *BufPtr = Buffer+39;
85
86   *BufPtr = 0;                  // Null terminate buffer...
87   if (X == 0) *--BufPtr = '0';  // Handle special case...
88
89   while (X) {
90     *--BufPtr = '0' + char(X % 10);
91     X /= 10;
92   }
93
94   if (isNeg) *--BufPtr = '-';   // Add negative sign...
95   return std::string(BufPtr);
96 }
97
98
99 static inline std::string itostr(int64_t X) {
100   if (X < 0)
101     return utostr(static_cast<uint64_t>(-X), true);
102   else
103     return utostr(static_cast<uint64_t>(X));
104 }
105
106 static inline std::string ftostr(double V) {
107   char Buffer[200];
108   sprintf(Buffer, "%20.6e", V);
109   char *B = Buffer;
110   while (*B == ' ') ++B;
111   return B;
112 }
113
114 static inline std::string ftostr(const APFloat& V) {
115   if (&V.getSemantics() == &APFloat::IEEEdouble)
116     return ftostr(V.convertToDouble());
117   else if (&V.getSemantics() == &APFloat::IEEEsingle)
118     return ftostr((double)V.convertToFloat());
119   return "<unknown format in ftostr>"; // error
120 }
121
122 static inline std::string LowercaseString(const std::string &S) {
123   std::string result(S);
124   for (unsigned i = 0; i < S.length(); ++i)
125     if (isupper(result[i]))
126       result[i] = char(tolower(result[i]));
127   return result;
128 }
129
130 static inline std::string UppercaseString(const std::string &S) {
131   std::string result(S);
132   for (unsigned i = 0; i < S.length(); ++i)
133     if (islower(result[i]))
134       result[i] = char(toupper(result[i]));
135   return result;
136 }
137
138 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
139 /// case.
140 static inline bool StringsEqualNoCase(const std::string &LHS,
141                                       const std::string &RHS) {
142   if (LHS.size() != RHS.size()) return false;
143   for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
144     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
145   return true;
146 }
147
148 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
149 /// case.
150 static inline bool StringsEqualNoCase(const std::string &LHS,
151                                       const char *RHS) {
152   for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
153     if (RHS[i] == 0) return false;  // RHS too short.
154     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
155   }
156   return RHS[LHS.size()] == 0;  // Not too long?
157 }
158   
159 /// StringsEqualNoCase - Return true if the two null-terminated C strings are
160 ///  equal, ignoring
161
162 static inline bool StringsEqualNoCase(const char *LHS, const char *RHS,
163                                       unsigned len) {
164
165   for (unsigned i = 0; i < len; ++i) {
166     if (tolower(LHS[i]) != tolower(RHS[i]))
167       return false;
168     
169     // If RHS[i] == 0 then LHS[i] == 0 or otherwise we would have returned
170     // at the previous branch as tolower('\0') == '\0'.
171     if (RHS[i] == 0)
172       return true;
173   }
174   
175   return true;
176 }
177
178 /// CStrInCStrNoCase - Portable version of strcasestr.  Locates the first
179 ///  occurance of c-string 's2' in string 's1', ignoring case.  Returns
180 ///  NULL if 's2' cannot be found.
181 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
182
183   // Are either strings NULL or empty?
184   if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
185     return 0;
186
187   if (s1 == s2)
188     return s1;
189
190   const char *I1=s1, *I2=s2;
191
192   while (*I1 != '\0' && *I2 != '\0' )
193     if (tolower(*I1) != tolower(*I2)) { // No match.  Start over.
194       ++s1; I1 = s1; I2 = s2;
195     }
196     else { // Character match.  Advance to the next character.
197       ++I1; ++I2;
198     }
199
200   // If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
201   return *I2 == '\0' ? s1 : 0;
202 }
203
204 /// getToken - This function extracts one token from source, ignoring any
205 /// leading characters that appear in the Delimiters string, and ending the
206 /// token at any of the characters that appear in the Delimiters string.  If
207 /// there are no tokens in the source string, an empty string is returned.
208 /// The Source source string is updated in place to remove the returned string
209 /// and any delimiter prefix from it.
210 std::string getToken(std::string &Source,
211                      const char *Delimiters = " \t\n\v\f\r");
212
213 /// SplitString - Split up the specified string according to the specified
214 /// delimiters, appending the result fragments to the output list.
215 void SplitString(const std::string &Source,
216                  std::vector<std::string> &OutFragments,
217                  const char *Delimiters = " \t\n\v\f\r");
218
219 /// UnescapeString - Modify the argument string, turning two character sequences
220 /// like '\\' 'n' into '\n'.  This handles: \e \a \b \f \n \r \t \v \' \\ and
221 /// \num (where num is a 1-3 byte octal value).
222 void UnescapeString(std::string &Str);
223
224 /// EscapeString - Modify the argument string, turning '\\' and anything that
225 /// doesn't satisfy std::isprint into an escape sequence.
226 void EscapeString(std::string &Str);
227
228 } // End llvm namespace
229
230 #endif