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 static inline std::string utohexstr(uint64_t X) {
28 char *BufPtr = Buffer+39;
30 *BufPtr = 0; // Null terminate buffer...
31 if (X == 0) *--BufPtr = '0'; // Handle special case...
34 unsigned char Mod = static_cast<unsigned char>(X) & 15;
36 *--BufPtr = '0' + Mod;
38 *--BufPtr = 'A' + Mod-10;
41 return std::string(BufPtr);
44 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
46 char *BufPtr = Buffer+19;
48 *BufPtr = 0; // Null terminate buffer...
49 if (X == 0) *--BufPtr = '0'; // Handle special case...
52 *--BufPtr = '0' + char(X % 10);
56 if (isNeg) *--BufPtr = '-'; // Add negative sign...
58 return std::string(BufPtr);
61 static inline std::string utostr(uint64_t X, bool isNeg = false) {
63 return utostr_32(uint32_t(X), isNeg);
66 char *BufPtr = Buffer+39;
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...
77 return std::string(BufPtr);
81 static inline std::string itostr(int64_t X) {
83 return utostr(static_cast<uint64_t>(-X), true);
85 return utostr(static_cast<uint64_t>(X));
88 static inline std::string ftostr(double V) {
90 sprintf(Buffer, "%20.6e", V);
92 while (*B == ' ') ++B;
96 static inline std::string ftostr(const APFloat& V) {
97 if (&V.getSemantics() == &APFloat::IEEEdouble)
98 return ftostr(V.convertToDouble());
99 else if (&V.getSemantics() == &APFloat::IEEEsingle)
100 return ftostr((double)V.convertToFloat());
101 return "<unknown format in ftostr>"; // error
104 static inline std::string LowercaseString(const std::string &S) {
105 std::string result(S);
106 for (unsigned i = 0; i < S.length(); ++i)
107 if (isupper(result[i]))
108 result[i] = char(tolower(result[i]));
112 static inline std::string UppercaseString(const std::string &S) {
113 std::string result(S);
114 for (unsigned i = 0; i < S.length(); ++i)
115 if (islower(result[i]))
116 result[i] = char(toupper(result[i]));
120 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
122 static inline bool StringsEqualNoCase(const std::string &LHS,
123 const std::string &RHS) {
124 if (LHS.size() != RHS.size()) return false;
125 for (unsigned i = 0, e = LHS.size(); i != e; ++i)
126 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
130 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
132 static inline bool StringsEqualNoCase(const std::string &LHS,
134 for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
135 if (RHS[i] == 0) return false; // RHS too short.
136 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
138 return RHS[LHS.size()] == 0; // Not too long?
141 /// getToken - This function extracts one token from source, ignoring any
142 /// leading characters that appear in the Delimiters string, and ending the
143 /// token at any of the characters that appear in the Delimiters string. If
144 /// there are no tokens in the source string, an empty string is returned.
145 /// The Source source string is updated in place to remove the returned string
146 /// and any delimiter prefix from it.
147 std::string getToken(std::string &Source,
148 const char *Delimiters = " \t\n\v\f\r");
150 /// SplitString - Split up the specified string according to the specified
151 /// delimiters, appending the result fragments to the output list.
152 void SplitString(const std::string &Source,
153 std::vector<std::string> &OutFragments,
154 const char *Delimiters = " \t\n\v\f\r");
156 /// UnescapeString - Modify the argument string, turning two character sequences
157 /// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and
158 /// \num (where num is a 1-3 byte octal value).
159 void UnescapeString(std::string &Str);
161 /// EscapeString - Modify the argument string, turning '\\' and anything that
162 /// doesn't satisfy std::isprint into an escape sequence.
163 void EscapeString(std::string &Str);
165 } // End llvm namespace