/src/hermes/external/llvh/include/llvh/ADT/StringExtras.h
Line | Count | Source |
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 "llvh/ADT/ArrayRef.h" |
18 | | #include "llvh/ADT/SmallString.h" |
19 | | #include "llvh/ADT/StringRef.h" |
20 | | #include "llvh/ADT/Twine.h" |
21 | | #include <cassert> |
22 | | #include <cstddef> |
23 | | #include <cstdint> |
24 | | #include <cstdlib> |
25 | | #include <cstring> |
26 | | #include <iterator> |
27 | | #include <string> |
28 | | #include <utility> |
29 | | |
30 | | namespace llvh { |
31 | | |
32 | | template<typename T> class SmallVectorImpl; |
33 | | class raw_ostream; |
34 | | |
35 | | /// hexdigit - Return the hexadecimal character for the |
36 | | /// given number \p X (which should be less than 16). |
37 | 28 | inline char hexdigit(unsigned X, bool LowerCase = false) { |
38 | 28 | const char HexChar = LowerCase ? 'a' : 'A'; |
39 | 28 | return X < 10 ? '0' + X : HexChar + X - 10; |
40 | 28 | } |
41 | | |
42 | | /// Given an array of c-style strings terminated by a null pointer, construct |
43 | | /// a vector of StringRefs representing the same strings without the terminating |
44 | | /// null string. |
45 | 0 | inline std::vector<StringRef> toStringRefArray(const char *const *Strings) { |
46 | 0 | std::vector<StringRef> Result; |
47 | 0 | while (*Strings) |
48 | 0 | Result.push_back(*Strings++); |
49 | 0 | return Result; |
50 | 0 | } |
51 | | |
52 | | /// Construct a string ref from a boolean. |
53 | 0 | inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); } |
54 | | |
55 | | /// Construct a string ref from an array ref of unsigned chars. |
56 | 0 | inline StringRef toStringRef(ArrayRef<uint8_t> Input) { |
57 | 0 | return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size()); |
58 | 0 | } |
59 | | |
60 | | /// Construct a string ref from an array ref of unsigned chars. |
61 | 0 | inline ArrayRef<uint8_t> arrayRefFromStringRef(StringRef Input) { |
62 | 0 | return {Input.bytes_begin(), Input.bytes_end()}; |
63 | 0 | } |
64 | | |
65 | | /// Interpret the given character \p C as a hexadecimal digit and return its |
66 | | /// value. |
67 | | /// |
68 | | /// If \p C is not a valid hex digit, -1U is returned. |
69 | 0 | inline unsigned hexDigitValue(char C) { |
70 | 0 | if (C >= '0' && C <= '9') return C-'0'; |
71 | 0 | if (C >= 'a' && C <= 'f') return C-'a'+10U; |
72 | 0 | if (C >= 'A' && C <= 'F') return C-'A'+10U; |
73 | 0 | return -1U; |
74 | 0 | } |
75 | | |
76 | | /// Checks if character \p C is one of the 10 decimal digits. |
77 | 0 | inline bool isDigit(char C) { return C >= '0' && C <= '9'; } |
78 | | |
79 | | /// Checks if character \p C is a hexadecimal numeric character. |
80 | 0 | inline bool isHexDigit(char C) { return hexDigitValue(C) != -1U; } |
81 | | |
82 | | /// Checks if character \p C is a valid letter as classified by "C" locale. |
83 | 0 | inline bool isAlpha(char C) { |
84 | 0 | return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z'); |
85 | 0 | } |
86 | | |
87 | | /// Checks whether character \p C is either a decimal digit or an uppercase or |
88 | | /// lowercase letter as classified by "C" locale. |
89 | 0 | inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); } |
90 | | |
91 | | /// Checks whether character \p C is valid ASCII (high bit is zero). |
92 | 0 | inline bool isASCII(char C) { return static_cast<unsigned char>(C) <= 127; } |
93 | | |
94 | | /// Checks whether all characters in S are ASCII. |
95 | 0 | inline bool isASCII(llvh::StringRef S) { |
96 | 0 | for (char C : S) |
97 | 0 | if (LLVM_UNLIKELY(!isASCII(C))) |
98 | 0 | return false; |
99 | 0 | return true; |
100 | 0 | } |
101 | | |
102 | | /// Checks whether character \p C is printable. |
103 | | /// |
104 | | /// Locale-independent version of the C standard library isprint whose results |
105 | | /// may differ on different platforms. |
106 | 0 | inline bool isPrint(char C) { |
107 | 0 | unsigned char UC = static_cast<unsigned char>(C); |
108 | 0 | return (0x20 <= UC) && (UC <= 0x7E); |
109 | 0 | } |
110 | | |
111 | | /// Returns the corresponding lowercase character if \p x is uppercase. |
112 | 0 | inline char toLower(char x) { |
113 | 0 | if (x >= 'A' && x <= 'Z') |
114 | 0 | return x - 'A' + 'a'; |
115 | 0 | return x; |
116 | 0 | } |
117 | | |
118 | | /// Returns the corresponding uppercase character if \p x is lowercase. |
119 | 0 | inline char toUpper(char x) { |
120 | 0 | if (x >= 'a' && x <= 'z') |
121 | 0 | return x - 'a' + 'A'; |
122 | 0 | return x; |
123 | 0 | } |
124 | | |
125 | 0 | inline std::string utohexstr(uint64_t X, bool LowerCase = false) { |
126 | 0 | char Buffer[17]; |
127 | 0 | char *BufPtr = std::end(Buffer); |
128 | 0 |
|
129 | 0 | if (X == 0) *--BufPtr = '0'; |
130 | 0 |
|
131 | 0 | while (X) { |
132 | 0 | unsigned char Mod = static_cast<unsigned char>(X) & 15; |
133 | 0 | *--BufPtr = hexdigit(Mod, LowerCase); |
134 | 0 | X >>= 4; |
135 | 0 | } |
136 | 0 |
|
137 | 0 | return std::string(BufPtr, std::end(Buffer)); |
138 | 0 | } |
139 | | |
140 | | /// Convert buffer \p Input to its hexadecimal representation. |
141 | | /// The returned string is double the size of \p Input. |
142 | 0 | inline std::string toHex(StringRef Input, bool LowerCase = false) { |
143 | 0 | static const char *const LUT = "0123456789ABCDEF"; |
144 | 0 | const uint8_t Offset = LowerCase ? 32 : 0; |
145 | 0 | size_t Length = Input.size(); |
146 | 0 |
|
147 | 0 | std::string Output; |
148 | 0 | Output.reserve(2 * Length); |
149 | 0 | for (size_t i = 0; i < Length; ++i) { |
150 | 0 | const unsigned char c = Input[i]; |
151 | 0 | Output.push_back(LUT[c >> 4] | Offset); |
152 | 0 | Output.push_back(LUT[c & 15] | Offset); |
153 | 0 | } |
154 | 0 | return Output; |
155 | 0 | } |
156 | | |
157 | 0 | inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) { |
158 | 0 | return toHex(toStringRef(Input), LowerCase); |
159 | 0 | } |
160 | | |
161 | 0 | inline uint8_t hexFromNibbles(char MSB, char LSB) { |
162 | 0 | unsigned U1 = hexDigitValue(MSB); |
163 | 0 | unsigned U2 = hexDigitValue(LSB); |
164 | 0 | assert(U1 != -1U && U2 != -1U); |
165 | 0 |
|
166 | 0 | return static_cast<uint8_t>((U1 << 4) | U2); |
167 | 0 | } |
168 | | |
169 | | /// Convert hexadecimal string \p Input to its binary representation. |
170 | | /// The return string is half the size of \p Input. |
171 | 0 | inline std::string fromHex(StringRef Input) { |
172 | 0 | if (Input.empty()) |
173 | 0 | return std::string(); |
174 | 0 |
|
175 | 0 | std::string Output; |
176 | 0 | Output.reserve((Input.size() + 1) / 2); |
177 | 0 | if (Input.size() % 2 == 1) { |
178 | 0 | Output.push_back(hexFromNibbles('0', Input.front())); |
179 | 0 | Input = Input.drop_front(); |
180 | 0 | } |
181 | 0 |
|
182 | 0 | assert(Input.size() % 2 == 0); |
183 | 0 | while (!Input.empty()) { |
184 | 0 | uint8_t Hex = hexFromNibbles(Input[0], Input[1]); |
185 | 0 | Output.push_back(Hex); |
186 | 0 | Input = Input.drop_front(2); |
187 | 0 | } |
188 | 0 | return Output; |
189 | 0 | } |
190 | | |
191 | | /// Convert the string \p S to an integer of the specified type using |
192 | | /// the radix \p Base. If \p Base is 0, auto-detects the radix. |
193 | | /// Returns true if the number was successfully converted, false otherwise. |
194 | | template <typename N> bool to_integer(StringRef S, N &Num, unsigned Base = 0) { |
195 | | return !S.getAsInteger(Base, Num); |
196 | | } |
197 | | |
198 | | namespace detail { |
199 | | template <typename N> |
200 | 0 | inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) { |
201 | 0 | SmallString<32> Storage; |
202 | 0 | StringRef S = T.toNullTerminatedStringRef(Storage); |
203 | 0 | char *End; |
204 | 0 | N Temp = StrTo(S.data(), &End); |
205 | 0 | if (*End != '\0') |
206 | 0 | return false; |
207 | 0 | Num = Temp; |
208 | 0 | return true; |
209 | 0 | } Unexecuted instantiation: bool llvh::detail::to_float<float>(llvh::Twine const&, float&, float (*)(char const*, char**)) Unexecuted instantiation: bool llvh::detail::to_float<double>(llvh::Twine const&, double&, double (*)(char const*, char**)) Unexecuted instantiation: bool llvh::detail::to_float<long double>(llvh::Twine const&, long double&, long double (*)(char const*, char**)) |
210 | | } |
211 | | |
212 | 0 | inline bool to_float(const Twine &T, float &Num) { |
213 | 0 | return detail::to_float(T, Num, strtof); |
214 | 0 | } |
215 | | |
216 | 0 | inline bool to_float(const Twine &T, double &Num) { |
217 | 0 | return detail::to_float(T, Num, strtod); |
218 | 0 | } |
219 | | |
220 | | #ifndef __ANDROID__ |
221 | 0 | inline bool to_float(const Twine &T, long double &Num) { |
222 | 0 | return detail::to_float(T, Num, strtold); |
223 | 0 | } |
224 | | #endif |
225 | | |
226 | 0 | inline std::string utostr(uint64_t X, bool isNeg = false) { |
227 | 0 | char Buffer[21]; |
228 | 0 | char *BufPtr = std::end(Buffer); |
229 | |
|
230 | 0 | if (X == 0) *--BufPtr = '0'; // Handle special case... |
231 | |
|
232 | 0 | while (X) { |
233 | 0 | *--BufPtr = '0' + char(X % 10); |
234 | 0 | X /= 10; |
235 | 0 | } |
236 | |
|
237 | 0 | if (isNeg) *--BufPtr = '-'; // Add negative sign... |
238 | 0 | return std::string(BufPtr, std::end(Buffer)); |
239 | 0 | } |
240 | | |
241 | 0 | inline std::string itostr(int64_t X) { |
242 | 0 | if (X < 0) |
243 | 0 | return utostr(static_cast<uint64_t>(-X), true); |
244 | 0 | else |
245 | 0 | return utostr(static_cast<uint64_t>(X)); |
246 | 0 | } |
247 | | |
248 | | /// StrInStrNoCase - Portable version of strcasestr. Locates the first |
249 | | /// occurrence of string 's1' in string 's2', ignoring case. Returns |
250 | | /// the offset of s2 in s1 or npos if s2 cannot be found. |
251 | | StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2); |
252 | | |
253 | | /// getToken - This function extracts one token from source, ignoring any |
254 | | /// leading characters that appear in the Delimiters string, and ending the |
255 | | /// token at any of the characters that appear in the Delimiters string. If |
256 | | /// there are no tokens in the source string, an empty string is returned. |
257 | | /// The function returns a pair containing the extracted token and the |
258 | | /// remaining tail string. |
259 | | std::pair<StringRef, StringRef> getToken(StringRef Source, |
260 | | StringRef Delimiters = " \t\n\v\f\r"); |
261 | | |
262 | | /// SplitString - Split up the specified string according to the specified |
263 | | /// delimiters, appending the result fragments to the output list. |
264 | | void SplitString(StringRef Source, |
265 | | SmallVectorImpl<StringRef> &OutFragments, |
266 | | StringRef Delimiters = " \t\n\v\f\r"); |
267 | | |
268 | | /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th). |
269 | 0 | inline StringRef getOrdinalSuffix(unsigned Val) { |
270 | 0 | // It is critically important that we do this perfectly for |
271 | 0 | // user-written sequences with over 100 elements. |
272 | 0 | switch (Val % 100) { |
273 | 0 | case 11: |
274 | 0 | case 12: |
275 | 0 | case 13: |
276 | 0 | return "th"; |
277 | 0 | default: |
278 | 0 | switch (Val % 10) { |
279 | 0 | case 1: return "st"; |
280 | 0 | case 2: return "nd"; |
281 | 0 | case 3: return "rd"; |
282 | 0 | default: return "th"; |
283 | 0 | } |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | | /// Print each character of the specified string, escaping it if it is not |
288 | | /// printable or if it is an escape char. |
289 | | void printEscapedString(StringRef Name, raw_ostream &Out); |
290 | | |
291 | | /// Print each character of the specified string, escaping HTML special |
292 | | /// characters. |
293 | | void printHTMLEscaped(StringRef String, raw_ostream &Out); |
294 | | |
295 | | /// printLowerCase - Print each character as lowercase if it is uppercase. |
296 | | void printLowerCase(StringRef String, raw_ostream &Out); |
297 | | |
298 | | namespace detail { |
299 | | |
300 | | template <typename IteratorT> |
301 | | inline std::string join_impl(IteratorT Begin, IteratorT End, |
302 | | StringRef Separator, std::input_iterator_tag) { |
303 | | std::string S; |
304 | | if (Begin == End) |
305 | | return S; |
306 | | |
307 | | S += (*Begin); |
308 | | while (++Begin != End) { |
309 | | S += Separator; |
310 | | S += (*Begin); |
311 | | } |
312 | | return S; |
313 | | } |
314 | | |
315 | | template <typename IteratorT> |
316 | | inline std::string join_impl(IteratorT Begin, IteratorT End, |
317 | 0 | StringRef Separator, std::forward_iterator_tag) { |
318 | 0 | std::string S; |
319 | 0 | if (Begin == End) |
320 | 0 | return S; |
321 | | |
322 | 0 | size_t Len = (std::distance(Begin, End) - 1) * Separator.size(); |
323 | 0 | for (IteratorT I = Begin; I != End; ++I) |
324 | 0 | Len += (*Begin).size(); |
325 | 0 | S.reserve(Len); |
326 | 0 | S += (*Begin); |
327 | 0 | while (++Begin != End) { |
328 | 0 | S += Separator; |
329 | 0 | S += (*Begin); |
330 | 0 | } |
331 | 0 | return S; |
332 | 0 | } |
333 | | |
334 | | template <typename Sep> |
335 | | inline void join_items_impl(std::string &Result, Sep Separator) {} |
336 | | |
337 | | template <typename Sep, typename Arg> |
338 | | inline void join_items_impl(std::string &Result, Sep Separator, |
339 | | const Arg &Item) { |
340 | | Result += Item; |
341 | | } |
342 | | |
343 | | template <typename Sep, typename Arg1, typename... Args> |
344 | | inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1, |
345 | | Args &&... Items) { |
346 | | Result += A1; |
347 | | Result += Separator; |
348 | | join_items_impl(Result, Separator, std::forward<Args>(Items)...); |
349 | | } |
350 | | |
351 | 0 | inline size_t join_one_item_size(char C) { return 1; } |
352 | 0 | inline size_t join_one_item_size(const char *S) { return S ? ::strlen(S) : 0; } |
353 | | |
354 | | template <typename T> inline size_t join_one_item_size(const T &Str) { |
355 | | return Str.size(); |
356 | | } |
357 | | |
358 | 0 | inline size_t join_items_size() { return 0; } |
359 | | |
360 | | template <typename A1> inline size_t join_items_size(const A1 &A) { |
361 | | return join_one_item_size(A); |
362 | | } |
363 | | template <typename A1, typename... Args> |
364 | | inline size_t join_items_size(const A1 &A, Args &&... Items) { |
365 | | return join_one_item_size(A) + join_items_size(std::forward<Args>(Items)...); |
366 | | } |
367 | | |
368 | | } // end namespace detail |
369 | | |
370 | | /// Joins the strings in the range [Begin, End), adding Separator between |
371 | | /// the elements. |
372 | | template <typename IteratorT> |
373 | 0 | inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) { |
374 | 0 | using tag = typename std::iterator_traits<IteratorT>::iterator_category; |
375 | 0 | return detail::join_impl(Begin, End, Separator, tag()); |
376 | 0 | } |
377 | | |
378 | | /// Joins the strings in the range [R.begin(), R.end()), adding Separator |
379 | | /// between the elements. |
380 | | template <typename Range> |
381 | | inline std::string join(Range &&R, StringRef Separator) { |
382 | | return join(R.begin(), R.end(), Separator); |
383 | | } |
384 | | |
385 | | /// Joins the strings in the parameter pack \p Items, adding \p Separator |
386 | | /// between the elements. All arguments must be implicitly convertible to |
387 | | /// std::string, or there should be an overload of std::string::operator+=() |
388 | | /// that accepts the argument explicitly. |
389 | | template <typename Sep, typename... Args> |
390 | | inline std::string join_items(Sep Separator, Args &&... Items) { |
391 | | std::string Result; |
392 | | if (sizeof...(Items) == 0) |
393 | | return Result; |
394 | | |
395 | | size_t NS = detail::join_one_item_size(Separator); |
396 | | size_t NI = detail::join_items_size(std::forward<Args>(Items)...); |
397 | | Result.reserve(NI + (sizeof...(Items) - 1) * NS + 1); |
398 | | detail::join_items_impl(Result, Separator, std::forward<Args>(Items)...); |
399 | | return Result; |
400 | | } |
401 | | |
402 | | } // end namespace llvh |
403 | | |
404 | | #endif // LLVM_ADT_STRINGEXTRAS_H |