2001-11-27 00:03:19 +00:00
|
|
|
//===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file contains some functions that are useful when dealing with strings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-11-27 00:03:19 +00:00
|
|
|
#ifndef SUPPORT_STRING_EXTRAS_H
|
|
|
|
#define SUPPORT_STRING_EXTRAS_H
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/DataTypes.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <string>
|
2001-07-15 00:16:38 +00:00
|
|
|
#include <stdio.h>
|
2001-10-29 13:24:31 +00:00
|
|
|
|
2002-04-07 08:36:19 +00:00
|
|
|
static inline std::string utohexstr(uint64_t X) {
|
|
|
|
char Buffer[40];
|
|
|
|
char *BufPtr = Buffer+39;
|
|
|
|
|
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
|
|
|
|
|
|
|
while (X) {
|
|
|
|
unsigned Mod = X & 15;
|
|
|
|
if (Mod < 10)
|
|
|
|
*--BufPtr = '0' + Mod;
|
|
|
|
else
|
|
|
|
*--BufPtr = 'A' + Mod-10;
|
|
|
|
X >>= 4;
|
|
|
|
}
|
|
|
|
return std::string(BufPtr);
|
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string utostr(uint64_t X, bool isNeg = false) {
|
2001-06-06 20:29:01 +00:00
|
|
|
char Buffer[40];
|
|
|
|
char *BufPtr = Buffer+39;
|
|
|
|
|
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
|
|
|
|
|
|
|
while (X) {
|
|
|
|
*--BufPtr = '0' + (X % 10);
|
|
|
|
X /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNeg) *--BufPtr = '-'; // Add negative sign...
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
return std::string(BufPtr);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string itostr(int64_t X) {
|
2001-06-06 20:29:01 +00:00
|
|
|
if (X < 0)
|
|
|
|
return utostr((uint64_t)-X, true);
|
|
|
|
else
|
|
|
|
return utostr((uint64_t)X);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string utostr(unsigned X, bool isNeg = false) {
|
2001-06-06 20:29:01 +00:00
|
|
|
char Buffer[20];
|
|
|
|
char *BufPtr = Buffer+19;
|
|
|
|
|
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
|
|
|
|
|
|
|
while (X) {
|
|
|
|
*--BufPtr = '0' + (X % 10);
|
|
|
|
X /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNeg) *--BufPtr = '-'; // Add negative sign...
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
return std::string(BufPtr);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string itostr(int X) {
|
2001-06-06 20:29:01 +00:00
|
|
|
if (X < 0)
|
|
|
|
return utostr((unsigned)-X, true);
|
|
|
|
else
|
|
|
|
return utostr((unsigned)X);
|
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string ftostr(double V) {
|
2001-07-15 00:16:38 +00:00
|
|
|
char Buffer[200];
|
2001-11-01 22:06:00 +00:00
|
|
|
snprintf(Buffer, 200, "%e", V);
|
2001-07-15 00:16:38 +00:00
|
|
|
return Buffer;
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|