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"
21 static inline std::string utohexstr(uint64_t X) {
23 char *BufPtr = Buffer+39;
25 *BufPtr = 0; // Null terminate buffer...
26 if (X == 0) *--BufPtr = '0'; // Handle special case...
29 unsigned Mod = X & 15;
31 *--BufPtr = '0' + Mod;
33 *--BufPtr = 'A' + Mod-10;
36 return std::string(BufPtr);
39 static inline std::string utostr(uint64_t X, bool isNeg = false) {
41 char *BufPtr = Buffer+39;
43 *BufPtr = 0; // Null terminate buffer...
44 if (X == 0) *--BufPtr = '0'; // Handle special case...
47 *--BufPtr = '0' + (X % 10);
51 if (isNeg) *--BufPtr = '-'; // Add negative sign...
53 return std::string(BufPtr);
56 static inline std::string itostr(int64_t X) {
58 return utostr((uint64_t)-X, true);
60 return utostr((uint64_t)X);
64 static inline std::string utostr(unsigned X, bool isNeg = false) {
66 char *BufPtr = Buffer+19;
68 *BufPtr = 0; // Null terminate buffer...
69 if (X == 0) *--BufPtr = '0'; // Handle special case...
72 *--BufPtr = '0' + (X % 10);
76 if (isNeg) *--BufPtr = '-'; // Add negative sign...
78 return std::string(BufPtr);
81 static inline std::string itostr(int X) {
83 return utostr((unsigned)-X, true);
85 return utostr((unsigned)X);
88 static inline std::string ftostr(double V) {
90 snprintf(Buffer, 200, "%20.6e", V);