Enhance APFloat to retain bits of NaNs (fixes oggenc).
[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 "llvm/ADT/APFloat.h"
19 #include <cctype>
20 #include <cstdio>
21 #include <string>
22 #include <vector>
23
24 namespace llvm {
25
26 static inline std::string utohexstr(uint64_t X) {
27   char Buffer[40];
28   char *BufPtr = Buffer+39;
29
30   *BufPtr = 0;                  // Null terminate buffer...
31   if (X == 0) *--BufPtr = '0';  // Handle special case...
32
33   while (X) {
34     unsigned char Mod = static_cast<unsigned char>(X) & 15;
35     if (Mod < 10)
36       *--BufPtr = '0' + Mod;
37     else
38       *--BufPtr = 'A' + Mod-10;
39     X >>= 4;
40   }
41   return std::string(BufPtr);
42 }
43
44 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
45   char Buffer[20];
46   char *BufPtr = Buffer+19;
47
48   *BufPtr = 0;                  // Null terminate buffer...
49   if (X == 0) *--BufPtr = '0';  // Handle special case...
50
51   while (X) {
52     *--BufPtr = '0' + char(X % 10);
53     X /= 10;
54   }
55
56   if (isNeg) *--BufPtr = '-';   // Add negative sign...
57
58   return std::string(BufPtr);
59 }
60
61 static inline std::string utostr(uint64_t X, bool isNeg = false) {
62   if (X == uint32_t(X))
63     return utostr_32(uint32_t(X), isNeg);
64   
65   char Buffer[40];
66   char *BufPtr = Buffer+39;
67   
68   *BufPtr = 0;                  // Null terminate buffer...
69   if (X == 0) *--BufPtr = '0';  // Handle special case...
70   
71   while (X) {
72     *--BufPtr = '0' + char(X % 10);
73     X /= 10;
74   }
75   
76   if (isNeg) *--BufPtr = '-';   // Add negative sign...
77   return std::string(BufPtr);
78 }
79
80
81 static inline std::string itostr(int64_t X) {
82   if (X < 0)
83     return utostr(static_cast<uint64_t>(-X), true);
84   else
85     return utostr(static_cast<uint64_t>(X));
86 }
87
88 static inline std::string ftostr(double V) {
89   char Buffer[200];
90   sprintf(Buffer, "%20.6e", V);
91   char *B = Buffer;
92   while (*B == ' ') ++B;
93   return B;
94 }
95
96 static inline std::string ftostr(APFloat V) {
97   if (&V.getSemantics() == &APFloat::IEEEsingle)
98     return ftostr(V.convertToDouble());
99   else if (&V.getSemantics() == &APFloat::IEEEdouble)
100     return ftostr((double)V.convertToFloat());
101   return 0; // error
102 }
103
104 static inline std::string LowercaseString(const std::string &S) {
105   std::string result(S);
106   for (unsigned i = 0; i < S.length(); ++i)
107     if (isupper(result[i]))
108       result[i] = char(tolower(result[i]));
109   return result;
110 }
111
112 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
113 /// case.
114 static inline bool StringsEqualNoCase(const std::string &LHS, 
115                                       const std::string &RHS) {
116   if (LHS.size() != RHS.size()) return false;
117   for (unsigned i = 0, e = LHS.size(); i != e; ++i)
118     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
119   return true;
120 }
121
122 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
123 /// case.
124 static inline bool StringsEqualNoCase(const std::string &LHS, 
125                                       const char *RHS) {
126   for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
127     if (RHS[i] == 0) return false;  // RHS too short.
128     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
129   }
130   return RHS[LHS.size()] == 0;  // Not too long?
131 }
132
133 /// getToken - This function extracts one token from source, ignoring any
134 /// leading characters that appear in the Delimiters string, and ending the
135 /// token at any of the characters that appear in the Delimiters string.  If
136 /// there are no tokens in the source string, an empty string is returned.
137 /// The Source source string is updated in place to remove the returned string
138 /// and any delimiter prefix from it.
139 std::string getToken(std::string &Source,
140                      const char *Delimiters = " \t\n\v\f\r");
141
142 /// SplitString - Split up the specified string according to the specified
143 /// delimiters, appending the result fragments to the output list.
144 void SplitString(const std::string &Source,
145                  std::vector<std::string> &OutFragments,
146                  const char *Delimiters = " \t\n\v\f\r");
147
148 /// UnescapeString - Modify the argument string, turning two character sequences
149 /// like '\\' 'n' into '\n'.  This handles: \e \a \b \f \n \r \t \v \' \\ and
150 /// \num (where num is a 1-3 byte octal value).
151 void UnescapeString(std::string &Str);
152
153 /// EscapeString - Modify the argument string, turning '\\' and anything that
154 /// doesn't satisfy std::isprint into an escape sequence.
155 void EscapeString(std::string &Str);
156
157 } // End llvm namespace
158
159 #endif