1 //===-- Support/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 SUPPORT_STRINGEXTRAS_H
15 #define SUPPORT_STRINGEXTRAS_H
17 #include "Support/DataTypes.h"
23 static inline std::string utohexstr(uint64_t X) {
25 char *BufPtr = Buffer+39;
27 *BufPtr = 0; // Null terminate buffer...
28 if (X == 0) *--BufPtr = '0'; // Handle special case...
31 unsigned Mod = X & 15;
33 *--BufPtr = '0' + Mod;
35 *--BufPtr = 'A' + Mod-10;
38 return std::string(BufPtr);
41 static inline std::string utostr(unsigned long long X, bool isNeg = false) {
43 char *BufPtr = Buffer+39;
45 *BufPtr = 0; // Null terminate buffer...
46 if (X == 0) *--BufPtr = '0'; // Handle special case...
49 *--BufPtr = '0' + (X % 10);
53 if (isNeg) *--BufPtr = '-'; // Add negative sign...
55 return std::string(BufPtr);
58 static inline std::string itostr(int64_t X) {
60 return utostr(static_cast<uint64_t>(-X), true);
62 return utostr(static_cast<uint64_t>(X));
66 static inline std::string utostr(unsigned long X, bool isNeg = false) {
67 return utostr(static_cast<unsigned long long>(X), isNeg);
70 static inline std::string utostr(unsigned X, bool isNeg = false) {
72 char *BufPtr = Buffer+19;
74 *BufPtr = 0; // Null terminate buffer...
75 if (X == 0) *--BufPtr = '0'; // Handle special case...
78 *--BufPtr = '0' + (X % 10);
82 if (isNeg) *--BufPtr = '-'; // Add negative sign...
84 return std::string(BufPtr);
87 static inline std::string itostr(int X) {
89 return utostr(static_cast<unsigned>(-X), true);
91 return utostr(static_cast<unsigned>(X));
94 static inline std::string ftostr(double V) {
96 snprintf(Buffer, 200, "%20.6e", V);
101 /// getToken - This function extracts one token from source, ignoring any
102 /// leading characters that appear in the Delimiters string, and ending the
103 /// token at any of the characters that appear in the Delimiters string. If
104 /// there are no tokens in the source string, an empty string is returned.
105 /// The Source source string is updated in place to remove the returned string
106 /// and any delimiter prefix from it.
107 std::string getToken(std::string &Source,
108 const char *Delimiters = " \t\n\v\f\r");
110 } // End llvm namespace