Fix build breakage on alpha, without causing it on x86. as a bonus, all platforms...
[oota-llvm.git] / include / llvm / ADT / StringExtras.h
1 //===-- llvm/ADT/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 LLVM_ADT_STRINGEXTRAS_H
15 #define LLVM_ADT_STRINGEXTRAS_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cctype>
19 #include <cstdio>
20 #include <string>
21
22 namespace llvm {
23
24 static inline std::string utohexstr(uint64_t X) {
25   char Buffer[40];
26   char *BufPtr = Buffer+39;
27
28   *BufPtr = 0;                  // Null terminate buffer...
29   if (X == 0) *--BufPtr = '0';  // Handle special case...
30
31   while (X) {
32     unsigned char Mod = static_cast<unsigned char>(X) & 15;
33     if (Mod < 10)
34       *--BufPtr = '0' + Mod;
35     else
36       *--BufPtr = 'A' + Mod-10;
37     X >>= 4;
38   }
39   return std::string(BufPtr);
40 }
41
42 static inline std::string utostr(uint64_t X, bool isNeg = false) {
43   char Buffer[40];
44   char *BufPtr = Buffer+39;
45
46   *BufPtr = 0;                  // Null terminate buffer...
47   if (X == 0) *--BufPtr = '0';  // Handle special case...
48
49   while (X) {
50     *--BufPtr = '0' + char(X % 10);
51     X /= 10;
52   }
53
54   if (isNeg) *--BufPtr = '-';   // Add negative sign...
55   return std::string(BufPtr);
56 }
57
58 static inline std::string utostr(uint32_t X, bool isNeg = false) {
59   char Buffer[20];
60   char *BufPtr = Buffer+19;
61
62   *BufPtr = 0;                  // Null terminate buffer...
63   if (X == 0) *--BufPtr = '0';  // Handle special case...
64
65   while (X) {
66     *--BufPtr = '0' + char(X % 10);
67     X /= 10;
68   }
69
70   if (isNeg) *--BufPtr = '-';   // Add negative sign...
71
72   return std::string(BufPtr);
73 }
74
75 static inline std::string itostr(int64_t X) {
76   if (X < 0)
77     return utostr(static_cast<uint64_t>(-X), true);
78   else
79     return utostr(static_cast<uint64_t>(X));
80 }
81
82 static inline std::string itostr(int32_t X) {
83   if (X < 0)
84     return utostr(static_cast<unsigned>(-X), true);
85   else
86     return utostr(static_cast<unsigned>(X));
87 }
88
89 static inline std::string ftostr(double V) {
90   char Buffer[200];
91   sprintf(Buffer, "%20.6e", V);
92   char *B = Buffer;
93   while (*B == ' ') ++B;
94   return B;
95 }
96
97 static inline std::string LowercaseString(const std::string &S) {
98   std::string result(S);
99   for (unsigned i = 0; i < S.length(); ++i)
100     if (isupper(result[i]))
101       result[i] = char(tolower(result[i]));
102   return result;
103 }
104
105 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
106 /// case.
107 static inline bool StringsEqualNoCase(const std::string &LHS, 
108                                       const std::string &RHS) {
109   if (LHS.size() != RHS.size()) return false;
110   for (unsigned i = 0, e = LHS.size(); i != e; ++i)
111     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
112   return true;
113 }
114
115 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
116 /// case.
117 static inline bool StringsEqualNoCase(const std::string &LHS, 
118                                       const char *RHS) {
119   for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
120     if (RHS[i] == 0) return false;  // RHS too short.
121     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
122   }
123   return RHS[LHS.size()] == 0;  // Not too long?
124 }
125
126 /// getToken - This function extracts one token from source, ignoring any
127 /// leading characters that appear in the Delimiters string, and ending the
128 /// token at any of the characters that appear in the Delimiters string.  If
129 /// there are no tokens in the source string, an empty string is returned.
130 /// The Source source string is updated in place to remove the returned string
131 /// and any delimiter prefix from it.
132 std::string getToken(std::string &Source,
133                      const char *Delimiters = " \t\n\v\f\r");
134
135 } // End llvm namespace
136
137 #endif