1 //===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=//
3 // This file contains some functions that are useful when dealing with strings.
4 // No library is required when using these functinons.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_TOOLS_STRING_EXTRAS_H
9 #define LLVM_TOOLS_STRING_EXTRAS_H
13 #include "llvm/Support/DataTypes.h"
15 static inline string utostr(uint64_t X, bool isNeg = false) {
17 char *BufPtr = Buffer+39;
19 *BufPtr = 0; // Null terminate buffer...
20 if (X == 0) *--BufPtr = '0'; // Handle special case...
23 *--BufPtr = '0' + (X % 10);
27 if (isNeg) *--BufPtr = '-'; // Add negative sign...
29 return string(BufPtr);
32 static inline string itostr(int64_t X) {
34 return utostr((uint64_t)-X, true);
36 return utostr((uint64_t)X);
40 static inline string utostr(unsigned X, bool isNeg = false) {
42 char *BufPtr = Buffer+19;
44 *BufPtr = 0; // Null terminate buffer...
45 if (X == 0) *--BufPtr = '0'; // Handle special case...
48 *--BufPtr = '0' + (X % 10);
52 if (isNeg) *--BufPtr = '-'; // Add negative sign...
54 return string(BufPtr);
57 static inline string itostr(int X) {
59 return utostr((unsigned)-X, true);
61 return utostr((unsigned)X);
64 static inline string ftostr(double V) {
66 snprintf(Buffer, 200, "%f", V);
71 printIndent(unsigned int indent, ostream& os=cout, const char* const istr=" ")
73 for (unsigned i=0; i < indent; i++)