StringExtras.h revision ddcdcc88631c6bd4ad43d9198b98bc9a829be036
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 "llvm/ADT/StringRef.h"
20#include <cctype>
21#include <cstdio>
22#include <string>
23
24namespace llvm {
25template<typename T> class SmallVectorImpl;
26
27/// hexdigit - Return the hexadecimal character for the
28/// given number \arg X (which should be less than 16).
29static inline char hexdigit(unsigned X, bool LowerCase = false) {
30  const char HexChar = LowerCase ? 'a' : 'A';
31  return X < 10 ? '0' + X : HexChar + X - 10;
32}
33
34/// utohex_buffer - Emit the specified number into the buffer specified by
35/// BufferEnd, returning a pointer to the start of the string.  This can be used
36/// like this: (note that the buffer must be large enough to handle any number):
37///    char Buffer[40];
38///    printf("0x%s", utohex_buffer(X, Buffer+40));
39///
40/// This should only be used with unsigned types.
41///
42template<typename IntTy>
43static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
44  char *BufPtr = BufferEnd;
45  *--BufPtr = 0;      // Null terminate buffer.
46  if (X == 0) {
47    *--BufPtr = '0';  // Handle special case.
48    return BufPtr;
49  }
50
51  while (X) {
52    unsigned char Mod = static_cast<unsigned char>(X) & 15;
53    *--BufPtr = hexdigit(Mod);
54    X >>= 4;
55  }
56  return BufPtr;
57}
58
59static inline std::string utohexstr(uint64_t X) {
60  char Buffer[17];
61  return utohex_buffer(X, Buffer+17);
62}
63
64static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
65  char Buffer[11];
66  char *BufPtr = Buffer+11;
67
68  if (X == 0) *--BufPtr = '0';  // Handle special case...
69
70  while (X) {
71    *--BufPtr = '0' + char(X % 10);
72    X /= 10;
73  }
74
75  if (isNeg) *--BufPtr = '-';   // Add negative sign...
76
77  return std::string(BufPtr, Buffer+11);
78}
79
80static inline std::string utostr(uint64_t X, bool isNeg = false) {
81  char Buffer[21];
82  char *BufPtr = Buffer+21;
83
84  if (X == 0) *--BufPtr = '0';  // Handle special case...
85
86  while (X) {
87    *--BufPtr = '0' + char(X % 10);
88    X /= 10;
89  }
90
91  if (isNeg) *--BufPtr = '-';   // Add negative sign...
92  return std::string(BufPtr, Buffer+21);
93}
94
95
96static inline std::string itostr(int64_t X) {
97  if (X < 0)
98    return utostr(static_cast<uint64_t>(-X), true);
99  else
100    return utostr(static_cast<uint64_t>(X));
101}
102
103static inline std::string ftostr(double V) {
104  char Buffer[200];
105  sprintf(Buffer, "%20.6e", V);
106  char *B = Buffer;
107  while (*B == ' ') ++B;
108  return B;
109}
110
111static inline std::string ftostr(const APFloat& V) {
112  if (&V.getSemantics() == &APFloat::IEEEdouble)
113    return ftostr(V.convertToDouble());
114  else if (&V.getSemantics() == &APFloat::IEEEsingle)
115    return ftostr((double)V.convertToFloat());
116  return "<unknown format in ftostr>"; // error
117}
118
119static inline std::string LowercaseString(const std::string &S) {
120  std::string result(S);
121  for (unsigned i = 0; i < S.length(); ++i)
122    if (isupper(result[i]))
123      result[i] = char(tolower(result[i]));
124  return result;
125}
126
127static inline std::string UppercaseString(const std::string &S) {
128  std::string result(S);
129  for (unsigned i = 0; i < S.length(); ++i)
130    if (islower(result[i]))
131      result[i] = char(toupper(result[i]));
132  return result;
133}
134
135/// StrInStrNoCase - Portable version of strcasestr.  Locates the first
136/// occurrence of string 's1' in string 's2', ignoring case.  Returns
137/// the offset of s2 in s1 or npos if s2 cannot be found.
138StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
139
140/// getToken - This function extracts one token from source, ignoring any
141/// leading characters that appear in the Delimiters string, and ending the
142/// token at any of the characters that appear in the Delimiters string.  If
143/// there are no tokens in the source string, an empty string is returned.
144/// The function returns a pair containing the extracted token and the
145/// remaining tail string.
146std::pair<StringRef, StringRef> getToken(StringRef Source,
147                                         StringRef Delimiters = " \t\n\v\f\r");
148
149/// SplitString - Split up the specified string according to the specified
150/// delimiters, appending the result fragments to the output list.
151void SplitString(StringRef Source,
152                 SmallVectorImpl<StringRef> &OutFragments,
153                 StringRef Delimiters = " \t\n\v\f\r");
154
155/// HashString - Hash function for strings.
156///
157/// This is the Bernstein hash function.
158//
159// FIXME: Investigate whether a modified bernstein hash function performs
160// better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
161//   X*33+c -> X*33^c
162static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
163  for (unsigned i = 0, e = Str.size(); i != e; ++i)
164    Result = Result * 33 + Str[i];
165  return Result;
166}
167
168} // End llvm namespace
169
170#endif
171