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