add new function
[oota-llvm.git] / include / llvm / ADT / StringExtras.h
1 //===-- Support/StringExtras.h - Useful string functions --------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains some functions that are useful when dealing with strings.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SUPPORT_STRINGEXTRAS_H
15 #define SUPPORT_STRINGEXTRAS_H
16
17 #include "Support/DataTypes.h"
18 #include <string>
19 #include <stdio.h>
20
21 namespace llvm {
22
23 static inline std::string utohexstr(uint64_t X) {
24   char Buffer[40];
25   char *BufPtr = Buffer+39;
26
27   *BufPtr = 0;                  // Null terminate buffer...
28   if (X == 0) *--BufPtr = '0';  // Handle special case...
29
30   while (X) {
31     unsigned Mod = X & 15;
32     if (Mod < 10)
33       *--BufPtr = '0' + Mod;
34     else
35       *--BufPtr = 'A' + Mod-10;
36     X >>= 4;
37   }
38   return std::string(BufPtr);
39 }
40
41 static inline std::string utostr(unsigned long long X, bool isNeg = false) {
42   char Buffer[40];
43   char *BufPtr = Buffer+39;
44
45   *BufPtr = 0;                  // Null terminate buffer...
46   if (X == 0) *--BufPtr = '0';  // Handle special case...
47
48   while (X) {
49     *--BufPtr = '0' + (X % 10);
50     X /= 10;
51   }
52
53   if (isNeg) *--BufPtr = '-';   // Add negative sign...
54
55   return std::string(BufPtr);
56 }
57
58 static inline std::string itostr(int64_t X) {
59   if (X < 0) 
60     return utostr(static_cast<uint64_t>(-X), true);
61   else
62     return utostr(static_cast<uint64_t>(X));
63 }
64
65
66 static inline std::string utostr(unsigned long X, bool isNeg = false) {
67   return utostr(static_cast<unsigned long long>(X), isNeg);
68 }
69
70 static inline std::string utostr(unsigned X, bool isNeg = false) {
71   char Buffer[20];
72   char *BufPtr = Buffer+19;
73
74   *BufPtr = 0;                  // Null terminate buffer...
75   if (X == 0) *--BufPtr = '0';  // Handle special case...
76
77   while (X) {
78     *--BufPtr = '0' + (X % 10);
79     X /= 10;
80   }
81
82   if (isNeg) *--BufPtr = '-';   // Add negative sign...
83
84   return std::string(BufPtr);
85 }
86
87 static inline std::string itostr(int X) {
88   if (X < 0) 
89     return utostr(static_cast<unsigned>(-X), true);
90   else
91     return utostr(static_cast<unsigned>(X));
92 }
93
94 static inline std::string ftostr(double V) {
95   char Buffer[200];
96   snprintf(Buffer, 200, "%20.6e", V);
97   return Buffer;
98 }
99
100
101 /// getToken - This function extracts one token from source, ignoring any
102 /// leading characters that appear in the Delimiters string, and ending the
103 /// token at any of the characters that appear in the Delimiters string.  If
104 /// there are no tokens in the source string, an empty string is returned.
105 /// The Source source string is updated in place to remove the returned string
106 /// and any delimiter prefix from it.
107 std::string getToken(std::string &Source,
108                      const char *Delimiters = " \t\n\v\f\r");
109
110 } // End llvm namespace
111
112 #endif