1 //===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=//
3 // This file contains some functions that are useful when dealing with strings.
5 //===----------------------------------------------------------------------===//
7 #ifndef SUPPORT_STRING_EXTRAS_H
8 #define SUPPORT_STRING_EXTRAS_H
10 #include "Support/DataTypes.h"
14 static inline std::string utohexstr(uint64_t X) {
16 char *BufPtr = Buffer+39;
18 *BufPtr = 0; // Null terminate buffer...
19 if (X == 0) *--BufPtr = '0'; // Handle special case...
22 unsigned Mod = X & 15;
24 *--BufPtr = '0' + Mod;
26 *--BufPtr = 'A' + Mod-10;
29 return std::string(BufPtr);
32 static inline std::string utostr(uint64_t X, bool isNeg = false) {
34 char *BufPtr = Buffer+39;
36 *BufPtr = 0; // Null terminate buffer...
37 if (X == 0) *--BufPtr = '0'; // Handle special case...
40 *--BufPtr = '0' + (X % 10);
44 if (isNeg) *--BufPtr = '-'; // Add negative sign...
46 return std::string(BufPtr);
49 static inline std::string itostr(int64_t X) {
51 return utostr((uint64_t)-X, true);
53 return utostr((uint64_t)X);
57 static inline std::string utostr(unsigned X, bool isNeg = false) {
59 char *BufPtr = Buffer+19;
61 *BufPtr = 0; // Null terminate buffer...
62 if (X == 0) *--BufPtr = '0'; // Handle special case...
65 *--BufPtr = '0' + (X % 10);
69 if (isNeg) *--BufPtr = '-'; // Add negative sign...
71 return std::string(BufPtr);
74 static inline std::string itostr(int X) {
76 return utostr((unsigned)-X, true);
78 return utostr((unsigned)X);
81 static inline std::string ftostr(double V) {
83 snprintf(Buffer, 200, "%e", V);