2003-09-30 18:37:50 +00:00
|
|
|
//===-- Support/StringExtras.h - Useful string functions --------*- C++ -*-===//
|
2003-10-20 19:46:57 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file contains some functions that are useful when dealing with strings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-06-17 00:35:55 +00:00
|
|
|
#ifndef SUPPORT_STRINGEXTRAS_H
|
|
|
|
#define SUPPORT_STRINGEXTRAS_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
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
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) {
|
2004-06-04 20:21:53 +00:00
|
|
|
unsigned char Mod = (unsigned char)X & 15;
|
2002-04-07 08:36:19 +00:00
|
|
|
if (Mod < 10)
|
|
|
|
*--BufPtr = '0' + Mod;
|
|
|
|
else
|
|
|
|
*--BufPtr = 'A' + Mod-10;
|
|
|
|
X >>= 4;
|
|
|
|
}
|
|
|
|
return std::string(BufPtr);
|
|
|
|
}
|
|
|
|
|
2003-10-30 15:03:49 +00:00
|
|
|
static inline std::string utostr(unsigned long long 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) {
|
2004-06-04 19:10:30 +00:00
|
|
|
*--BufPtr = '0' + char(X % 10);
|
2001-06-06 20:29:01 +00:00
|
|
|
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)
|
2003-11-16 20:21:15 +00:00
|
|
|
return utostr(static_cast<uint64_t>(-X), true);
|
2001-06-06 20:29:01 +00:00
|
|
|
else
|
2003-11-16 20:21:15 +00:00
|
|
|
return utostr(static_cast<uint64_t>(X));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-29 20:06:19 +00:00
|
|
|
static inline std::string utostr(unsigned long X, bool isNeg = false) {
|
2003-11-16 20:21:15 +00:00
|
|
|
return utostr(static_cast<unsigned long long>(X), isNeg);
|
2003-10-29 20:06:19 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2004-06-04 19:10:30 +00:00
|
|
|
*--BufPtr = '0' + char(X % 10);
|
2001-06-06 20:29:01 +00:00
|
|
|
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)
|
2003-11-16 20:21:15 +00:00
|
|
|
return utostr(static_cast<unsigned>(-X), true);
|
2001-06-06 20:29:01 +00:00
|
|
|
else
|
2003-11-16 20:21:15 +00:00
|
|
|
return utostr(static_cast<unsigned>(X));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
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];
|
2004-06-04 19:10:30 +00:00
|
|
|
sprintf(Buffer, "%20.6e", V);
|
2001-07-15 00:16:38 +00:00
|
|
|
return Buffer;
|
|
|
|
}
|
|
|
|
|
2003-12-29 05:06:38 +00:00
|
|
|
|
|
|
|
/// getToken - This function extracts one token from source, ignoring any
|
|
|
|
/// leading characters that appear in the Delimiters string, and ending the
|
|
|
|
/// token at any of the characters that appear in the Delimiters string. If
|
|
|
|
/// there are no tokens in the source string, an empty string is returned.
|
|
|
|
/// The Source source string is updated in place to remove the returned string
|
|
|
|
/// and any delimiter prefix from it.
|
|
|
|
std::string getToken(std::string &Source,
|
|
|
|
const char *Delimiters = " \t\n\v\f\r");
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|