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 = (unsigned char)X & 15;
34 *--BufPtr = '0' + Mod;
36 *--BufPtr = 'A' + Mod-10;
39 return std::string(BufPtr);
42 static inline std::string utostr(unsigned long long 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(unsigned long X, bool isNeg = false) {
59 return utostr(static_cast<unsigned long long>(X), isNeg);
62 static inline std::string utostr(unsigned 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 itostr(long long X) {
81 return utostr(static_cast<uint64_t>(-X), true);
83 return utostr(static_cast<uint64_t>(X));
86 static inline std::string itostr(long X) {
88 return utostr(static_cast<uint64_t>(-X), true);
90 return utostr(static_cast<uint64_t>(X));
93 static inline std::string itostr(int X) {
95 return utostr(static_cast<unsigned>(-X), true);
97 return utostr(static_cast<unsigned>(X));
100 static inline std::string ftostr(double V) {
102 sprintf(Buffer, "%20.6e", V);
104 while (*B == ' ') ++B;
108 static inline std::string LowercaseString(const std::string &S) {
109 std::string result(S);
110 for (unsigned i = 0; i < S.length(); ++i)
111 if (isupper(result[i]))
112 result[i] = (char)tolower(result[i]);
116 /// getToken - This function extracts one token from source, ignoring any
117 /// leading characters that appear in the Delimiters string, and ending the
118 /// token at any of the characters that appear in the Delimiters string. If
119 /// there are no tokens in the source string, an empty string is returned.
120 /// The Source source string is updated in place to remove the returned string
121 /// and any delimiter prefix from it.
122 std::string getToken(std::string &Source,
123 const char *Delimiters = " \t\n\v\f\r");
125 } // End llvm namespace