StringExtras.h revision 3a54b3dc87a581c203b18050b4f787b4ca28a12c
1//===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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
24namespace llvm {
25
26/// hexdigit - Return the (uppercase) hexadecimal character for the
27/// given number \arg X (which should be less than 16).
28static inline char hexdigit(unsigned X) {
29  return X < 10 ? '0' + X : 'A' + X - 10;
30}
31
32/// utohex_buffer - Emit the specified number into the buffer specified by
33/// BufferEnd, returning a pointer to the start of the string.  This can be used
34/// like this: (note that the buffer must be large enough to handle any number):
35///    char Buffer[40];
36///    printf("0x%s", utohex_buffer(X, Buffer+40));
37///
38/// This should only be used with unsigned types.
39///
40template<typename IntTy>
41static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
42  char *BufPtr = BufferEnd;
43  *--BufPtr = 0;      // Null terminate buffer.
44  if (X == 0) {
45    *--BufPtr = '0';  // Handle special case.
46    return BufPtr;
47  }
48
49  while (X) {
50    unsigned char Mod = static_cast<unsigned char>(X) & 15;
51    *--BufPtr = hexdigit(Mod);
52    X >>= 4;
53  }
54  return BufPtr;
55}
56
57static inline std::string utohexstr(uint64_t X) {
58  char Buffer[40];
59  return utohex_buffer(X, Buffer+40);
60}
61
62static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
63  char Buffer[20];
64  char *BufPtr = Buffer+19;
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
76  return std::string(BufPtr);
77}
78
79static inline std::string utostr(uint64_t X, bool isNeg = false) {
80  if (X == uint32_t(X))
81    return utostr_32(uint32_t(X), isNeg);
82
83  char Buffer[40];
84  char *BufPtr = Buffer+39;
85
86  *BufPtr = 0;                  // Null terminate buffer...
87  if (X == 0) *--BufPtr = '0';  // Handle special case...
88
89  while (X) {
90    *--BufPtr = '0' + char(X % 10);
91    X /= 10;
92  }
93
94  if (isNeg) *--BufPtr = '-';   // Add negative sign...
95  return std::string(BufPtr);
96}
97
98
99static inline std::string itostr(int64_t X) {
100  if (X < 0)
101    return utostr(static_cast<uint64_t>(-X), true);
102  else
103    return utostr(static_cast<uint64_t>(X));
104}
105
106static inline std::string itohexstr(int64_t X) {
107  return utohexstr(static_cast<uint64_t>(X));
108}
109
110static inline std::string ftostr(double V) {
111  char Buffer[200];
112  sprintf(Buffer, "%20.6e", V);
113  char *B = Buffer;
114  while (*B == ' ') ++B;
115  return B;
116}
117
118static inline std::string ftostr(const APFloat& V) {
119  if (&V.getSemantics() == &APFloat::IEEEdouble)
120    return ftostr(V.convertToDouble());
121  else if (&V.getSemantics() == &APFloat::IEEEsingle)
122    return ftostr((double)V.convertToFloat());
123  return "<unknown format in ftostr>"; // error
124}
125
126static inline std::string LowercaseString(const std::string &S) {
127  std::string result(S);
128  for (unsigned i = 0; i < S.length(); ++i)
129    if (isupper(result[i]))
130      result[i] = char(tolower(result[i]));
131  return result;
132}
133
134static inline std::string UppercaseString(const std::string &S) {
135  std::string result(S);
136  for (unsigned i = 0; i < S.length(); ++i)
137    if (islower(result[i]))
138      result[i] = char(toupper(result[i]));
139  return result;
140}
141
142/// StringsEqualNoCase - Return true if the two strings are equal, ignoring
143/// case.
144static inline bool StringsEqualNoCase(const std::string &LHS,
145                                      const std::string &RHS) {
146  if (LHS.size() != RHS.size()) return false;
147  for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
148    if (tolower(LHS[i]) != tolower(RHS[i])) return false;
149  return true;
150}
151
152/// StringsEqualNoCase - Return true if the two strings are equal, ignoring
153/// case.
154static inline bool StringsEqualNoCase(const std::string &LHS,
155                                      const char *RHS) {
156  for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
157    if (RHS[i] == 0) return false;  // RHS too short.
158    if (tolower(LHS[i]) != tolower(RHS[i])) return false;
159  }
160  return RHS[LHS.size()] == 0;  // Not too long?
161}
162
163/// CStrInCStrNoCase - Portable version of strcasestr.  Locates the first
164///  occurance of c-string 's2' in string 's1', ignoring case.  Returns
165///  NULL if 's2' cannot be found.
166static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
167
168  // Are either strings NULL or empty?
169  if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
170    return 0;
171
172  if (s1 == s2)
173    return s1;
174
175  const char *I1=s1, *I2=s2;
176
177  while (*I1 != '\0' && *I2 != '\0' )
178    if (tolower(*I1) != tolower(*I2)) { // No match.  Start over.
179      ++s1; I1 = s1; I2 = s2;
180    }
181    else { // Character match.  Advance to the next character.
182      ++I1; ++I2;
183    }
184
185  // If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
186  return *I2 == '\0' ? s1 : 0;
187}
188
189/// getToken - This function extracts one token from source, ignoring any
190/// leading characters that appear in the Delimiters string, and ending the
191/// token at any of the characters that appear in the Delimiters string.  If
192/// there are no tokens in the source string, an empty string is returned.
193/// The Source source string is updated in place to remove the returned string
194/// and any delimiter prefix from it.
195std::string getToken(std::string &Source,
196                     const char *Delimiters = " \t\n\v\f\r");
197
198/// SplitString - Split up the specified string according to the specified
199/// delimiters, appending the result fragments to the output list.
200void SplitString(const std::string &Source,
201                 std::vector<std::string> &OutFragments,
202                 const char *Delimiters = " \t\n\v\f\r");
203
204/// UnescapeString - Modify the argument string, turning two character sequences
205/// like '\\' 'n' into '\n'.  This handles: \e \a \b \f \n \r \t \v \' \\ and
206/// \num (where num is a 1-3 byte octal value).
207void UnescapeString(std::string &Str);
208
209/// EscapeString - Modify the argument string, turning '\\' and anything that
210/// doesn't satisfy std::isprint into an escape sequence.
211void EscapeString(std::string &Str);
212
213} // End llvm namespace
214
215#endif
216