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