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"
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;
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):
36 /// printf("0x%s", utohex_buffer(X, Buffer+40));
38 /// This should only be used with unsigned types.
40 template<typename IntTy>
41 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
42 char *BufPtr = BufferEnd;
43 *--BufPtr = 0; // Null terminate buffer.
45 *--BufPtr = '0'; // Handle special case.
50 unsigned char Mod = static_cast<unsigned char>(X) & 15;
51 *--BufPtr = hexdigit(Mod);
57 static inline std::string utohexstr(uint64_t X) {
59 return utohex_buffer(X, Buffer+40);
62 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
64 char *BufPtr = Buffer+19;
66 *BufPtr = 0; // Null terminate buffer...
67 if (X == 0) *--BufPtr = '0'; // Handle special case...
70 *--BufPtr = '0' + char(X % 10);
74 if (isNeg) *--BufPtr = '-'; // Add negative sign...
76 return std::string(BufPtr);
79 static inline std::string utostr(uint64_t X, bool isNeg = false) {
81 return utostr_32(uint32_t(X), isNeg);
84 char *BufPtr = Buffer+39;
86 *BufPtr = 0; // Null terminate buffer...
87 if (X == 0) *--BufPtr = '0'; // Handle special case...
90 *--BufPtr = '0' + char(X % 10);
94 if (isNeg) *--BufPtr = '-'; // Add negative sign...
95 return std::string(BufPtr);
99 static inline std::string itostr(int64_t X) {
101 return utostr(static_cast<uint64_t>(-X), true);
103 return utostr(static_cast<uint64_t>(X));
106 static inline std::string itohexstr(int64_t X) {
107 return utohexstr(static_cast<uint64_t>(X));
110 static inline std::string ftostr(double V) {
112 sprintf(Buffer, "%20.6e", V);
114 while (*B == ' ') ++B;
118 static inline std::string ftostr(const APFloat& V) {
119 if (&V.getSemantics() == &APFloat::IEEEdouble)
120 return ftostr(V.convertToDouble());
121 else if (&V.getSemantics() == &APFloat::IEEEsingle)
122 return ftostr((double)V.convertToFloat());
123 return "<unknown format in ftostr>"; // error
126 static inline std::string LowercaseString(const std::string &S) {
127 std::string result(S);
128 for (unsigned i = 0; i < S.length(); ++i)
129 if (isupper(result[i]))
130 result[i] = char(tolower(result[i]));
134 static inline std::string UppercaseString(const std::string &S) {
135 std::string result(S);
136 for (unsigned i = 0; i < S.length(); ++i)
137 if (islower(result[i]))
138 result[i] = char(toupper(result[i]));
142 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
144 static inline bool StringsEqualNoCase(const std::string &LHS,
145 const std::string &RHS) {
146 if (LHS.size() != RHS.size()) return false;
147 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
148 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
152 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
154 static inline bool StringsEqualNoCase(const std::string &LHS,
156 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
157 if (RHS[i] == 0) return false; // RHS too short.
158 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
160 return RHS[LHS.size()] == 0; // Not too long?
163 /// StringsEqualNoCase - Return true if the two null-terminated C strings are
166 static inline bool StringsEqualNoCase(const char *LHS, const char *RHS,
169 for (unsigned i = 0; i < len; ++i) {
170 if (tolower(LHS[i]) != tolower(RHS[i]))
173 // If RHS[i] == 0 then LHS[i] == 0 or otherwise we would have returned
174 // at the previous branch as tolower('\0') == '\0'.
182 /// CStrInCStrNoCase - Portable version of strcasestr. Locates the first
183 /// occurance of c-string 's2' in string 's1', ignoring case. Returns
184 /// NULL if 's2' cannot be found.
185 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
187 // Are either strings NULL or empty?
188 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
194 const char *I1=s1, *I2=s2;
196 while (*I1 != '\0' && *I2 != '\0' )
197 if (tolower(*I1) != tolower(*I2)) { // No match. Start over.
198 ++s1; I1 = s1; I2 = s2;
200 else { // Character match. Advance to the next character.
204 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
205 return *I2 == '\0' ? s1 : 0;
208 /// getToken - This function extracts one token from source, ignoring any
209 /// leading characters that appear in the Delimiters string, and ending the
210 /// token at any of the characters that appear in the Delimiters string. If
211 /// there are no tokens in the source string, an empty string is returned.
212 /// The Source source string is updated in place to remove the returned string
213 /// and any delimiter prefix from it.
214 std::string getToken(std::string &Source,
215 const char *Delimiters = " \t\n\v\f\r");
217 /// SplitString - Split up the specified string according to the specified
218 /// delimiters, appending the result fragments to the output list.
219 void SplitString(const std::string &Source,
220 std::vector<std::string> &OutFragments,
221 const char *Delimiters = " \t\n\v\f\r");
223 /// UnescapeString - Modify the argument string, turning two character sequences
224 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and
225 /// \num (where num is a 1-3 byte octal value).
226 void UnescapeString(std::string &Str);
228 /// EscapeString - Modify the argument string, turning '\\' and anything that
229 /// doesn't satisfy std::isprint into an escape sequence.
230 void EscapeString(std::string &Str);
232 } // End llvm namespace