Made error message more comprehensible.
[oota-llvm.git] / include / llvm / ADT / StringExtras.h
1 //===-- Support/StringExtras.h - Useful string functions --------*- C++ -*-===//
2 //
3 // This file contains some functions that are useful when dealing with strings.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef SUPPORT_STRINGEXTRAS_H
8 #define SUPPORT_STRINGEXTRAS_H
9
10 #include "Support/DataTypes.h"
11 #include <string>
12 #include <stdio.h>
13
14 static inline std::string utohexstr(uint64_t X) {
15   char Buffer[40];
16   char *BufPtr = Buffer+39;
17
18   *BufPtr = 0;                  // Null terminate buffer...
19   if (X == 0) *--BufPtr = '0';  // Handle special case...
20
21   while (X) {
22     unsigned Mod = X & 15;
23     if (Mod < 10)
24       *--BufPtr = '0' + Mod;
25     else
26       *--BufPtr = 'A' + Mod-10;
27     X >>= 4;
28   }
29   return std::string(BufPtr);
30 }
31
32 static inline std::string utostr(uint64_t X, bool isNeg = false) {
33   char Buffer[40];
34   char *BufPtr = Buffer+39;
35
36   *BufPtr = 0;                  // Null terminate buffer...
37   if (X == 0) *--BufPtr = '0';  // Handle special case...
38
39   while (X) {
40     *--BufPtr = '0' + (X % 10);
41     X /= 10;
42   }
43
44   if (isNeg) *--BufPtr = '-';   // Add negative sign...
45
46   return std::string(BufPtr);
47 }
48
49 static inline std::string itostr(int64_t X) {
50   if (X < 0) 
51     return utostr((uint64_t)-X, true);
52   else
53     return utostr((uint64_t)X);
54 }
55
56
57 static inline std::string utostr(unsigned X, bool isNeg = false) {
58   char Buffer[20];
59   char *BufPtr = Buffer+19;
60
61   *BufPtr = 0;                  // Null terminate buffer...
62   if (X == 0) *--BufPtr = '0';  // Handle special case...
63
64   while (X) {
65     *--BufPtr = '0' + (X % 10);
66     X /= 10;
67   }
68
69   if (isNeg) *--BufPtr = '-';   // Add negative sign...
70
71   return std::string(BufPtr);
72 }
73
74 static inline std::string itostr(int X) {
75   if (X < 0) 
76     return utostr((unsigned)-X, true);
77   else
78     return utostr((unsigned)X);
79 }
80
81 static inline std::string ftostr(double V) {
82   char Buffer[200];
83   snprintf(Buffer, 200, "%20.6e", V);
84   return Buffer;
85 }
86
87 #endif