WPILibC++  unspecified
StringExtras.h
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/StringRef.h"
18 #include <cstdint>
19 #include <iterator>
20 
21 namespace llvm {
22 template<typename T> class SmallVectorImpl;
23 
26 static inline char hexdigit(unsigned X, bool LowerCase = false) {
27  const char HexChar = LowerCase ? 'a' : 'A';
28  return X < 10 ? '0' + X : HexChar + X - 10;
29 }
30 
32 static inline StringRef toStringRef(bool B) {
33  return StringRef(B ? "true" : "false");
34 }
35 
40 static inline unsigned hexDigitValue(char C) {
41  if (C >= '0' && C <= '9') return C-'0';
42  if (C >= 'a' && C <= 'f') return C-'a'+10U;
43  if (C >= 'A' && C <= 'F') return C-'A'+10U;
44  return -1U;
45 }
46 
47 static inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
48  char Buffer[17];
49  char *BufPtr = std::end(Buffer);
50 
51  if (X == 0) *--BufPtr = '0';
52 
53  while (X) {
54  unsigned char Mod = static_cast<unsigned char>(X) & 15;
55  *--BufPtr = hexdigit(Mod, LowerCase);
56  X >>= 4;
57  }
58 
59  return std::string(BufPtr, std::end(Buffer));
60 }
61 
64 static inline std::string toHex(StringRef Input) {
65  static const char *const LUT = "0123456789ABCDEF";
66  size_t Length = Input.size();
67 
68  std::string Output;
69  Output.reserve(2 * Length);
70  for (size_t i = 0; i < Length; ++i) {
71  const unsigned char c = Input[i];
72  Output.push_back(LUT[c >> 4]);
73  Output.push_back(LUT[c & 15]);
74  }
75  return Output;
76 }
77 
78 static inline std::string utostr(uint64_t X, bool isNeg = false) {
79  char Buffer[21];
80  char *BufPtr = std::end(Buffer);
81 
82  if (X == 0) *--BufPtr = '0'; // Handle special case...
83 
84  while (X) {
85  *--BufPtr = '0' + char(X % 10);
86  X /= 10;
87  }
88 
89  if (isNeg) *--BufPtr = '-'; // Add negative sign...
90  return std::string(BufPtr, std::end(Buffer));
91 }
92 
93 
94 static inline std::string itostr(int64_t X) {
95  if (X < 0)
96  return utostr(static_cast<uint64_t>(-X), true);
97  else
98  return utostr(static_cast<uint64_t>(X));
99 }
100 
104 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
105 
112 std::pair<StringRef, StringRef> getToken(StringRef Source,
113  StringRef Delimiters = " \t\n\v\f\r");
114 
117 void SplitString(StringRef Source,
118  SmallVectorImpl<StringRef> &OutFragments,
119  StringRef Delimiters = " \t\n\v\f\r");
120 
124 //
125 // FIXME: Investigate whether a modified bernstein hash function performs
126 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
127 // X*33+c -> X*33^c
128 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
129  for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i)
130  Result = Result * 33 + (unsigned char)Str[i];
131  return Result;
132 }
133 
135 static inline StringRef getOrdinalSuffix(unsigned Val) {
136  // It is critically important that we do this perfectly for
137  // user-written sequences with over 100 elements.
138  switch (Val % 100) {
139  case 11:
140  case 12:
141  case 13:
142  return "th";
143  default:
144  switch (Val % 10) {
145  case 1: return "st";
146  case 2: return "nd";
147  case 3: return "rd";
148  default: return "th";
149  }
150  }
151 }
152 
153 template <typename IteratorT>
154 inline std::string join_impl(IteratorT Begin, IteratorT End,
155  StringRef Separator, std::input_iterator_tag) {
156  std::string S;
157  if (Begin == End)
158  return S;
159 
160  S += (*Begin);
161  while (++Begin != End) {
162  S += Separator;
163  S += (*Begin);
164  }
165  return S;
166 }
167 
168 template <typename IteratorT>
169 inline std::string join_impl(IteratorT Begin, IteratorT End,
170  StringRef Separator, std::forward_iterator_tag) {
171  std::string S;
172  if (Begin == End)
173  return S;
174 
175  size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
176  for (IteratorT I = Begin; I != End; ++I)
177  Len += (*Begin).size();
178  S.reserve(Len);
179  S += (*Begin);
180  while (++Begin != End) {
181  S += Separator;
182  S += (*Begin);
183  }
184  return S;
185 }
186 
189 template <typename IteratorT>
190 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
191  typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
192  return join_impl(Begin, End, Separator, tag());
193 }
194 
195 } // End llvm namespace
196 
197 #endif
Definition: Path.inc:31