1 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
24 static inline std::string utohexstr(uint64_t X) {
26 char *BufPtr = Buffer+39;
28 *BufPtr = 0; // Null terminate buffer...
29 if (X == 0) *--BufPtr = '0'; // Handle special case...
32 unsigned char Mod = static_cast<unsigned char>(X) & 15;
34 *--BufPtr = '0' + Mod;
36 *--BufPtr = 'A' + Mod-10;
39 return std::string(BufPtr);
42 static inline std::string utostr(uint64_t X, bool isNeg = false) {
44 char *BufPtr = Buffer+39;
46 *BufPtr = 0; // Null terminate buffer...
47 if (X == 0) *--BufPtr = '0'; // Handle special case...
50 *--BufPtr = '0' + char(X % 10);
54 if (isNeg) *--BufPtr = '-'; // Add negative sign...
55 return std::string(BufPtr);
58 static inline std::string utostr(uint32_t X, bool isNeg = false) {
60 char *BufPtr = Buffer+19;
62 *BufPtr = 0; // Null terminate buffer...
63 if (X == 0) *--BufPtr = '0'; // Handle special case...
66 *--BufPtr = '0' + char(X % 10);
70 if (isNeg) *--BufPtr = '-'; // Add negative sign...
72 return std::string(BufPtr);
75 static inline std::string itostr(int64_t X) {
77 return utostr(static_cast<uint64_t>(-X), true);
79 return utostr(static_cast<uint64_t>(X));
82 static inline std::string itostr(int32_t X) {
84 return utostr(static_cast<unsigned>(-X), true);
86 return utostr(static_cast<unsigned>(X));
89 static inline std::string ftostr(double V) {
91 sprintf(Buffer, "%20.6e", V);
93 while (*B == ' ') ++B;
97 static inline std::string LowercaseString(const std::string &S) {
98 std::string result(S);
99 for (unsigned i = 0; i < S.length(); ++i)
100 if (isupper(result[i]))
101 result[i] = char(tolower(result[i]));
105 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
107 static inline bool StringsEqualNoCase(const std::string &LHS,
108 const std::string &RHS) {
109 if (LHS.size() != RHS.size()) return false;
110 for (unsigned i = 0, e = LHS.size(); i != e; ++i)
111 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
115 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
117 static inline bool StringsEqualNoCase(const std::string &LHS,
119 for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
120 if (RHS[i] == 0) return false; // RHS too short.
121 if (tolower(LHS[i]) != tolower(RHS[i])) return false;
123 return RHS[LHS.size()] == 0; // Not too long?
126 /// getToken - This function extracts one token from source, ignoring any
127 /// leading characters that appear in the Delimiters string, and ending the
128 /// token at any of the characters that appear in the Delimiters string. If
129 /// there are no tokens in the source string, an empty string is returned.
130 /// The Source source string is updated in place to remove the returned string
131 /// and any delimiter prefix from it.
132 std::string getToken(std::string &Source,
133 const char *Delimiters = " \t\n\v\f\r");
135 } // End llvm namespace