Add two helper functions
[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_32(uint32_t X, bool isNeg = false) {
43   char Buffer[20];
44   char *BufPtr = Buffer+19;
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
56   return std::string(BufPtr);
57 }
58
59 static inline std::string utostr(uint64_t X, bool isNeg = false) {
60   if (X == uint32_t(X))
61     return utostr_32(uint32_t(X), isNeg);
62   
63   char Buffer[40];
64   char *BufPtr = Buffer+39;
65   
66   *BufPtr = 0;                  // Null terminate buffer...
67   if (X == 0) *--BufPtr = '0';  // Handle special case...
68   
69   while (X) {
70     *--BufPtr = '0' + char(X % 10);
71     X /= 10;
72   }
73   
74   if (isNeg) *--BufPtr = '-';   // Add negative sign...
75   return std::string(BufPtr);
76 }
77
78
79 static inline std::string itostr(int64_t X) {
80   if (X < 0)
81     return utostr(static_cast<uint64_t>(-X), true);
82   else
83     return utostr(static_cast<uint64_t>(X));
84 }
85
86 static inline std::string ftostr(double V) {
87   char Buffer[200];
88   sprintf(Buffer, "%20.6e", V);
89   char *B = Buffer;
90   while (*B == ' ') ++B;
91   return B;
92 }
93
94 static inline std::string LowercaseString(const std::string &S) {
95   std::string result(S);
96   for (unsigned i = 0; i < S.length(); ++i)
97     if (isupper(result[i]))
98       result[i] = char(tolower(result[i]));
99   return result;
100 }
101
102 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
103 /// case.
104 static inline bool StringsEqualNoCase(const std::string &LHS, 
105                                       const std::string &RHS) {
106   if (LHS.size() != RHS.size()) return false;
107   for (unsigned i = 0, e = LHS.size(); i != e; ++i)
108     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
109   return true;
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 char *RHS) {
116   for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
117     if (RHS[i] == 0) return false;  // RHS too short.
118     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
119   }
120   return RHS[LHS.size()] == 0;  // Not too long?
121 }
122
123 /// getToken - This function extracts one token from source, ignoring any
124 /// leading characters that appear in the Delimiters string, and ending the
125 /// token at any of the characters that appear in the Delimiters string.  If
126 /// there are no tokens in the source string, an empty string is returned.
127 /// The Source source string is updated in place to remove the returned string
128 /// and any delimiter prefix from it.
129 std::string getToken(std::string &Source,
130                      const char *Delimiters = " \t\n\v\f\r");
131
132 /// UnescapeString - Modify the argument string, turning two character sequences
133 /// like '\\' 'n' into '\n'.  This handles: \e \a \b \f \n \r \t \v \' \\ and
134 /// \num (where num is a 1-3 byte octal value).
135 void UnescapeString(std::string &Str);
136
137 /// EscapeString - Modify the argument string, turning '\\' and anything that
138 /// doesn't satisfy std::isprint into an escape sequence.
139 void EscapeString(std::string &Str);
140
141 } // End llvm namespace
142
143 #endif