Added function printIndent.
[oota-llvm.git] / include / llvm / Support / StringExtras.h
1 //===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=//
2 //
3 // This file contains some functions that are useful when dealing with strings.
4 // No library is required when using these functinons.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_TOOLS_STRING_EXTRAS_H
9 #define LLVM_TOOLS_STRING_EXTRAS_H
10
11 #include <string>
12 #include <stdio.h>
13 #include "llvm/Support/DataTypes.h"
14
15 static inline string utostr(uint64_t X, bool isNeg = false) {
16   char Buffer[40];
17   char *BufPtr = Buffer+39;
18
19   *BufPtr = 0;                  // Null terminate buffer...
20   if (X == 0) *--BufPtr = '0';  // Handle special case...
21
22   while (X) {
23     *--BufPtr = '0' + (X % 10);
24     X /= 10;
25   }
26
27   if (isNeg) *--BufPtr = '-';   // Add negative sign...
28
29   return string(BufPtr);
30 }
31
32 static inline string itostr(int64_t X) {
33   if (X < 0) 
34     return utostr((uint64_t)-X, true);
35   else
36     return utostr((uint64_t)X);
37 }
38
39
40 static inline string utostr(unsigned X, bool isNeg = false) {
41   char Buffer[20];
42   char *BufPtr = Buffer+19;
43
44   *BufPtr = 0;                  // Null terminate buffer...
45   if (X == 0) *--BufPtr = '0';  // Handle special case...
46
47   while (X) {
48     *--BufPtr = '0' + (X % 10);
49     X /= 10;
50   }
51
52   if (isNeg) *--BufPtr = '-';   // Add negative sign...
53
54   return string(BufPtr);
55 }
56
57 static inline string itostr(int X) {
58   if (X < 0) 
59     return utostr((unsigned)-X, true);
60   else
61     return utostr((unsigned)X);
62 }
63
64 static inline string ftostr(double V) {
65   char Buffer[200];
66   snprintf(Buffer, 200, "%f", V);
67   return Buffer;
68 }
69
70 static inline void
71 printIndent(unsigned int indent, ostream& os=cout, const char* const istr="  ")
72 {
73   for (unsigned i=0; i < indent; i++)
74     os << istr;
75 }
76 #endif