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