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 static inline std::string utohexstr(uint64_t X) {
34 char *BufPtr = Buffer+39;
36 *BufPtr = 0; // Null terminate buffer...
37 if (X == 0) *--BufPtr = '0'; // Handle special case...
40 unsigned char Mod = static_cast<unsigned char>(X) & 15;
41 *--BufPtr = hexdigit(Mod);
44 return std::string(BufPtr);
47 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
49 char *BufPtr = Buffer+19;
51 *BufPtr = 0; // Null terminate buffer...
52 if (X == 0) *--BufPtr = '0'; // Handle special case...
55 *--BufPtr = '0' + char(X % 10);
59 if (isNeg) *--BufPtr = '-'; // Add negative sign...
61 return std::string(BufPtr);
64 static inline std::string utostr(uint64_t X, bool isNeg = false) {
66 return utostr_32(uint32_t(X), isNeg);
69 char *BufPtr = Buffer+39;
71 *BufPtr = 0; // Null terminate buffer...
72 if (X == 0) *--BufPtr = '0'; // Handle special case...
75 *--BufPtr = '0' + char(X % 10);
79 if (isNeg) *--BufPtr = '-'; // Add negative sign...
80 return std::string(BufPtr);
84 static inline std::string itostr(int64_t X) {
86 return utostr(static_cast<uint64_t>(-X), true);
88 return utostr(static_cast<uint64_t>(X));
91 static inline std::string itohexstr(int64_t X) {
92 return utohexstr(static_cast<uint64_t>(X));
95 static inline std::string ftostr(double V) {
97 sprintf(Buffer, "%20.6e", V);
99 while (*B == ' ') ++B;
103 static inline std::string ftostr(const APFloat& V) {
104 if (&V.getSemantics() == &APFloat::IEEEdouble)
105 return ftostr(V.convertToDouble());
106 else if (&V.getSemantics() == &APFloat::IEEEsingle)
107 return ftostr((double)V.convertToFloat());
108 return "<unknown format in ftostr>"; // error
111 static inline std::string LowercaseString(const std::string &S) {
112 std::string result(S);
113 for (unsigned i = 0; i < S.length(); ++i)
114 if (isupper(result[i]))
115 result[i] = char(tolower(result[i]));
119 static inline std::string UppercaseString(const std::string &S) {
120 std::string result(S);
121 for (unsigned i = 0; i < S.length(); ++i)
122 if (islower(result[i]))
123 result[i] = char(toupper(result[i]));
127 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
129 static inline bool StringsEqualNoCase(const std::string &LHS,
130 const std::string &RHS) {
131 if (LHS.size() != RHS.size()) return false;
132 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
133 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
137 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
139 static inline bool StringsEqualNoCase(const std::string &LHS,
141 for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
142 if (RHS[i] == 0) return false; // RHS too short.
143 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
145 return RHS[LHS.size()] == 0; // Not too long?
148 /// CStrInCStrNoCase - Portable version of strcasestr. Locates the first
149 /// occurance of c-string 's2' in string 's1', ignoring case. Returns
150 /// NULL if 's2' cannot be found.
151 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
153 // Are either strings NULL or empty?
154 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
160 const char *I1=s1, *I2=s2;
162 while (*I1 != '\0' || *I2 != '\0' )
163 if (tolower(*I1) != tolower(*I2)) { // No match. Start over.
164 ++s1; I1 = s1; I2 = s2;
166 else { // Character match. Advance to the next character.
170 // If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
171 return *I2 == '\0' ? s1 : 0;
174 /// getToken - This function extracts one token from source, ignoring any
175 /// leading characters that appear in the Delimiters string, and ending the
176 /// token at any of the characters that appear in the Delimiters string. If
177 /// there are no tokens in the source string, an empty string is returned.
178 /// The Source source string is updated in place to remove the returned string
179 /// and any delimiter prefix from it.
180 std::string getToken(std::string &Source,
181 const char *Delimiters = " \t\n\v\f\r");
183 /// SplitString - Split up the specified string according to the specified
184 /// delimiters, appending the result fragments to the output list.
185 void SplitString(const std::string &Source,
186 std::vector<std::string> &OutFragments,
187 const char *Delimiters = " \t\n\v\f\r");
189 /// UnescapeString - Modify the argument string, turning two character sequences
190 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and
191 /// \num (where num is a 1-3 byte octal value).
192 void UnescapeString(std::string &Str);
194 /// EscapeString - Modify the argument string, turning '\\' and anything that
195 /// doesn't satisfy std::isprint into an escape sequence.
196 void EscapeString(std::string &Str);
198 } // End llvm namespace