WPILibC++ 2024.3.2
StringExtras.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5//===- llvm/ADT/StringExtras.h - Useful string functions --------*- C++ -*-===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is distributed under the University of Illinois Open Source
10// License. See LICENSE.TXT for details.
11//
12//===----------------------------------------------------------------------===//
13//
14// This file contains some functions that are useful when dealing with strings.
15//
16//===----------------------------------------------------------------------===//
17
18#pragma once
19
20#include <iterator>
21#include <limits>
22#include <optional>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <utility>
27
28#include <fmt/format.h>
29
30namespace wpi {
31
32template <typename T>
33class SmallVectorImpl;
34
35/// hexdigit - Return the hexadecimal character for the
36/// given number \p X (which should be less than 16).
37constexpr char hexdigit(unsigned X, bool LowerCase = false) noexcept {
38 const char HexChar = LowerCase ? 'a' : 'A';
39 return X < 10 ? '0' + X : HexChar + X - 10;
40}
41
42/// Interpret the given character \p C as a hexadecimal digit and return its
43/// value.
44///
45/// If \p C is not a valid hex digit, -1U is returned.
46constexpr unsigned hexDigitValue(char C) noexcept {
47 if (C >= '0' && C <= '9') {
48 return C - '0';
49 }
50 if (C >= 'a' && C <= 'f') {
51 return C - 'a' + 10U;
52 }
53 if (C >= 'A' && C <= 'F') {
54 return C - 'A' + 10U;
55 }
57}
58
59/// Checks if character \p C is one of the 10 decimal digits.
60constexpr bool isDigit(char C) noexcept {
61 return C >= '0' && C <= '9';
62}
63
64/// Checks if character \p C is a hexadecimal numeric character.
65constexpr bool isHexDigit(char C) noexcept {
67}
68
69/// Checks if character \p C is a valid letter as classified by "C" locale.
70constexpr bool isAlpha(char C) noexcept {
71 return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z');
72}
73
74/// Checks whether character \p C is either a decimal digit or an uppercase or
75/// lowercase letter as classified by "C" locale.
76constexpr bool isAlnum(char C) noexcept {
77 return isAlpha(C) || isDigit(C);
78}
79
80/// Checks whether character \p C is valid ASCII (high bit is zero).
81constexpr bool isASCII(char C) noexcept {
82 return static_cast<unsigned char>(C) <= 127;
83}
84
85/// Checks whether character \p C is printable.
86///
87/// Locale-independent version of the C standard library isprint whose results
88/// may differ on different platforms.
89constexpr bool isPrint(char C) noexcept {
90 unsigned char UC = static_cast<unsigned char>(C);
91 return (0x20 <= UC) && (UC <= 0x7E);
92}
93
94/// Returns the corresponding lowercase character if \p x is uppercase.
95constexpr char toLower(char x) noexcept {
96 if (x >= 'A' && x <= 'Z') {
97 return x - 'A' + 'a';
98 }
99 return x;
100}
101
102/// Returns the corresponding uppercase character if \p x is lowercase.
103constexpr char toUpper(char x) noexcept {
104 if (x >= 'a' && x <= 'z') {
105 return x - 'a' + 'A';
106 }
107 return x;
108}
109
110inline std::string utohexstr(unsigned long long val, // NOLINT(runtime/int)
111 bool lowerCase = false) {
112 char buf[17];
113 char* bufptr = std::end(buf);
114
115 if (val == 0) {
116 *--bufptr = '0';
117 }
118
119 while (val) {
120 unsigned char mod = static_cast<unsigned char>(val) & 15;
121 *--bufptr = hexdigit(mod, lowerCase);
122 val >>= 4;
123 }
124
125 return std::string(bufptr, std::end(buf));
126}
127
128/**
129 * equals - Check for string equality, this is more efficient than
130 * compare() when the relative ordering of inequal strings isn't needed.
131 */
132constexpr bool equals(std::string_view lhs, std::string_view rhs) noexcept {
133 auto length = lhs.size();
134 return length == rhs.size() && std::string_view::traits_type::compare(
135 lhs.data(), rhs.data(), length) == 0;
136}
137
138/**
139 * compare_lower - Compare two strings, ignoring case.
140 */
142
143/**
144 * equals_lower - Check for string equality, ignoring case.
145 */
147 std::string_view rhs) noexcept {
148 return lhs.size() == rhs.size() && compare_lower(lhs, rhs) == 0;
149}
150
151/**
152 * Search for the first character @p ch in @p str, ignoring case.
153 *
154 * @returns The index of the first occurrence of @p ch, or npos if not
155 * found.
156 */
157std::string_view::size_type find_lower(
158 std::string_view str, char ch,
159 std::string_view::size_type from = 0) noexcept;
160
161/**
162 * Search for the first string @p other in @p str, ignoring case.
163 *
164 * @returns The index of the first occurrence of @p other, or npos if not
165 * found.
166 */
168 std::string_view str, std::string_view other,
169 std::string_view::size_type from = 0) noexcept;
170
171/**
172 * Search for the first string @p other in @p str, ignoring case.
173 *
174 * @returns The index of the first occurrence of @p other, or npos if not
175 * found.
176 */
177inline std::string_view::size_type find_lower(
178 std::string_view str, const char* other,
179 std::string_view::size_type from = 0) noexcept {
180 return find_lower(str, std::string_view{other}, from);
181}
182
183/**
184 * Search for the last character @p ch in @p str, ignoring case.
185 *
186 * @returns The index of the last occurrence of @p ch, or npos if not
187 * found.
188 */
189std::string_view::size_type rfind_lower(
190 std::string_view str, char ch,
191 std::string_view::size_type from = std::string_view::npos) noexcept;
192
193/**
194 * Search for the last string @p other in @p str, ignoring case.
195 *
196 * @returns The index of the last occurrence of @p other, or npos if not
197 * found.
198 */
199std::string_view::size_type rfind_lower(std::string_view str,
200 std::string_view other) noexcept;
201
202/**
203 * Search for the last string @p other in @p str, ignoring case.
204 *
205 * @returns The index of the last occurrence of @p other, or npos if not
206 * found.
207 */
208inline std::string_view::size_type rfind_lower(std::string_view str,
209 const char* other) noexcept {
210 return rfind_lower(str, std::string_view{other});
211}
212
213/**
214 * Returns the substring of @p str from [start, start + n).
215 *
216 * @param start The index of the starting character in the substring; if
217 * the index is npos or greater than the length of the string then the
218 * empty substring will be returned.
219 *
220 * @param n The number of characters to included in the substring. If n
221 * exceeds the number of characters remaining in the string, the string
222 * suffix (starting with @p start) will be returned.
223 */
225 std::string_view str, std::string_view::size_type start,
226 std::string_view::size_type n = std::string_view::npos) noexcept {
227 auto length = str.size();
228 start = (std::min)(start, length);
229 return {str.data() + start, (std::min)(n, length - start)};
230}
231
232/**
233 * Checks if @p str starts with the given @p prefix.
234 */
235constexpr bool starts_with(std::string_view str,
236 std::string_view prefix) noexcept {
237 return substr(str, 0, prefix.size()) == prefix;
238}
239
240/**
241 * Checks if @p str starts with the given @p prefix.
242 */
243constexpr bool starts_with(std::string_view str, char prefix) noexcept {
244 return !str.empty() && std::string_view::traits_type::eq(str.front(), prefix);
245}
246
247/**
248 * Checks if @p str starts with the given @p prefix.
249 */
250constexpr bool starts_with(std::string_view str, const char* prefix) noexcept {
251 return starts_with(str, std::string_view(prefix));
252}
253
254/**
255 * Checks if @p str starts with the given @p prefix, ignoring case.
256 */
258
259/**
260 * Checks if @p str starts with the given @p prefix, ignoring case.
261 */
262constexpr bool starts_with_lower(std::string_view str, char prefix) noexcept {
263 return !str.empty() && toLower(str.front()) == toLower(prefix);
264}
265
266/**
267 * Checks if @p str starts with the given @p prefix, ignoring case.
268 */
270 const char* prefix) noexcept {
271 return starts_with_lower(str, std::string_view(prefix));
272}
273
274/**
275 * Checks if @p str ends with the given @p suffix.
276 */
277constexpr bool ends_with(std::string_view str,
278 std::string_view suffix) noexcept {
279 return str.size() >= suffix.size() &&
280 str.compare(str.size() - suffix.size(), std::string_view::npos,
281 suffix) == 0;
282}
283
284/**
285 * Checks if @p str ends with the given @p suffix.
286 */
287constexpr bool ends_with(std::string_view str, char suffix) noexcept {
288 return !str.empty() && std::string_view::traits_type::eq(str.back(), suffix);
289}
290
291/**
292 * Checks if @p str ends with the given @p suffix.
293 */
294constexpr bool ends_with(std::string_view str, const char* suffix) noexcept {
295 return ends_with(str, std::string_view(suffix));
296}
297
298/**
299 * Checks if @p str ends with the given @p suffix, ignoring case.
300 */
302
303/**
304 * Checks if @p str ends with the given @p suffix, ignoring case.
305 */
306constexpr bool ends_with_lower(std::string_view str, char suffix) noexcept {
307 return !str.empty() && toLower(str.back()) == toLower(suffix);
308}
309
310/**
311 * Checks if @p str ends with the given @p suffix, ignoring case.
312 */
313inline bool ends_with_lower(std::string_view str, const char* suffix) noexcept {
314 return ends_with_lower(str, std::string_view(suffix));
315}
316
317/**
318 * Checks if @p str contains the substring @p other.
319 */
320constexpr bool contains(std::string_view str, std::string_view other) noexcept {
321 return str.find(other) != std::string_view::npos;
322}
323
324/**
325 * Checks if @p str contains the substring @p other.
326 */
327constexpr bool contains(std::string_view str, char ch) noexcept {
328 return str.find(ch) != std::string_view::npos;
329}
330
331/**
332 * Checks if @p str contains the substring @p other.
333 */
334constexpr bool contains(std::string_view str, const char* other) noexcept {
335 return str.find(other) != std::string_view::npos;
336}
337
338/**
339 * Checks if @p str contains the substring @p other, ignoring case.
340 */
342 std::string_view other) noexcept {
343 return find_lower(str, other) != std::string_view::npos;
344}
345
346/**
347 * Checks if @p str contains the substring @p other, ignoring case.
348 */
349inline bool contains_lower(std::string_view str, char ch) noexcept {
350 return find_lower(str, ch) != std::string_view::npos;
351}
352
353/**
354 * Checks if @p str contains the substring @p other, ignoring case.
355 */
356inline bool contains_lower(std::string_view str, const char* other) noexcept {
357 return find_lower(str, other) != std::string_view::npos;
358}
359
360/**
361 * Return a string_view equal to @p str but with the first @p n elements
362 * dropped.
363 */
365 std::string_view str, std::string_view::size_type n = 1) noexcept {
366 str.remove_prefix(n);
367 return str;
368}
369
370/**
371 * Return a string_view equal to @p str but with the last @p n elements dropped.
372 */
374 std::string_view str, std::string_view::size_type n = 1) noexcept {
375 str.remove_suffix(n);
376 return str;
377}
378
379/**
380 * Returns a view equal to @p str but with only the first @p n
381 * elements remaining. If @p n is greater than the length of the
382 * string, the entire string is returned.
383 */
385 std::string_view str, std::string_view::size_type n = 1) noexcept {
386 auto length = str.size();
387 if (n >= length) {
388 return str;
389 }
390 return drop_back(str, length - n);
391}
392
393/**
394 * Returns a view equal to @p str but with only the last @p n
395 * elements remaining. If @p n is greater than the length of the
396 * string, the entire string is returned.
397 */
399 std::string_view str, std::string_view::size_type n = 1) noexcept {
400 auto length = str.size();
401 if (n >= length) {
402 return str;
403 }
404 return drop_front(str, length - n);
405}
406
407/**
408 * Returns a reference to the substring of @p str from [start, end).
409 *
410 * @param start The index of the starting character in the substring; if
411 * the index is npos or greater than the length of the string then the
412 * empty substring will be returned.
413 *
414 * @param end The index following the last character to include in the
415 * substring. If this is npos or exceeds the number of characters
416 * remaining in the string, the string suffix (starting with @p start)
417 * will be returned. If this is less than @p start, an empty string will
418 * be returned.
419 */
421 std::string_view::size_type start,
422 std::string_view::size_type end) noexcept {
423 auto length = str.size();
424 start = (std::min)(start, length);
425 end = (std::min)((std::max)(start, end), length);
426 return {str.data() + start, end - start};
427}
428
429/**
430 * Splits @p str into two substrings around the first occurrence of a separator
431 * character.
432 *
433 * If @p separator is in the string, then the result is a pair (LHS, RHS)
434 * such that (str == LHS + separator + RHS) is true and RHS is
435 * maximal. If @p separator is not in the string, then the result is a
436 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
437 *
438 * @param separator The character to split on.
439 * @returns The split substrings.
440 */
441constexpr std::pair<std::string_view, std::string_view> split(
442 std::string_view str, char separator) noexcept {
443 auto idx = str.find(separator);
444 if (idx == std::string_view::npos) {
445 return {str, {}};
446 }
447 return {slice(str, 0, idx), slice(str, idx + 1, std::string_view::npos)};
448}
449
450/**
451 * Splits @p str into two substrings around the first occurrence of a separator
452 * string.
453 *
454 * If @p separator is in the string, then the result is a pair (LHS, RHS)
455 * such that (str == LHS + separator + RHS) is true and RHS is
456 * maximal. If @p separator is not in the string, then the result is a
457 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
458 *
459 * @param separator The string to split on.
460 * @return The split substrings.
461 */
462constexpr std::pair<std::string_view, std::string_view> split(
463 std::string_view str, std::string_view separator) noexcept {
464 auto idx = str.find(separator);
465 if (idx == std::string_view::npos) {
466 return {str, {}};
467 }
468 return {slice(str, 0, idx),
469 slice(str, idx + separator.size(), std::string_view::npos)};
470}
471
472/**
473 * Splits @p str into two substrings around the last occurrence of a separator
474 * character.
475 *
476 * If @p separator is in the string, then the result is a pair (LHS, RHS)
477 * such that (str == LHS + separator + RHS) is true and RHS is
478 * minimal. If @p separator is not in the string, then the result is a
479 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
480 *
481 * @param separator The string to split on.
482 * @return The split substrings.
483 */
484constexpr std::pair<std::string_view, std::string_view> rsplit(
485 std::string_view str, char separator) noexcept {
486 auto idx = str.rfind(separator);
487 if (idx == std::string_view::npos) {
488 return {str, {}};
489 }
490 return {slice(str, 0, idx), slice(str, idx + 1, std::string_view::npos)};
491}
492
493/**
494 * Splits @p str into two substrings around the last occurrence of a separator
495 * string.
496 *
497 * If @p separator is in the string, then the result is a pair (LHS, RHS)
498 * such that (str == LHS + separator + RHS) is true and RHS is
499 * minimal. If @p separator is not in the string, then the result is a
500 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
501 *
502 * @param separator The string to split on.
503 * @return The split substrings.
504 */
505constexpr std::pair<std::string_view, std::string_view> rsplit(
506 std::string_view str, std::string_view separator) noexcept {
507 auto idx = str.rfind(separator);
508 if (idx == std::string_view::npos) {
509 return {str, {}};
510 }
511 return {slice(str, 0, idx),
512 slice(str, idx + separator.size(), std::string_view::npos)};
513}
514
515/**
516 * Splits @p str into substrings around the occurrences of a separator string.
517 *
518 * Each substring is stored in @p arr. If @p maxSplit is >= 0, at most
519 * @p maxSplit splits are done and consequently <= @p maxSplit + 1
520 * elements are added to arr.
521 * If @p keepEmpty is false, empty strings are not added to @p arr. They
522 * still count when considering @p maxSplit
523 * An useful invariant is that
524 * separator.join(arr) == str if maxSplit == -1 and keepEmpty == true
525 *
526 * @param arr Where to put the substrings.
527 * @param separator The string to split on.
528 * @param maxSplit The maximum number of times the string is split.
529 * @param keepEmpty True if empty substring should be added.
530 */
532 std::string_view separator, int maxSplit = -1,
533 bool keepEmpty = true) noexcept;
534
535/**
536 * Splits @p str into substrings around the occurrences of a separator
537 * character.
538 *
539 * Each substring is stored in @p arr. If @p maxSplit is >= 0, at most
540 * @p maxSplit splits are done and consequently <= @p maxSplit + 1
541 * elements are added to arr.
542 * If @p keepEmpty is false, empty strings are not added to @p arr. They
543 * still count when considering @p maxSplit
544 * An useful invariant is that
545 * separator.join(arr) == str if maxSplit == -1 and keepEmpty == true
546 *
547 * @param arr Where to put the substrings.
548 * @param separator The character to split on.
549 * @param maxSplit The maximum number of times the string is split.
550 * @param keepEmpty True if empty substring should be added.
551 */
553 char separator, int maxSplit = -1, bool keepEmpty = true) noexcept;
554
555/**
556 * Returns @p str with consecutive @p ch characters starting from the
557 * the left removed.
558 */
559constexpr std::string_view ltrim(std::string_view str, char ch) noexcept {
560 return drop_front(str, (std::min)(str.size(), str.find_first_not_of(ch)));
561}
562
563/**
564 * Returns @p str with consecutive characters in @p chars starting from
565 * the left removed.
566 */
568 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
569 return drop_front(str, (std::min)(str.size(), str.find_first_not_of(chars)));
570}
571
572/**
573 * Returns @p str with consecutive @p Char characters starting from the
574 * right removed.
575 */
576constexpr std::string_view rtrim(std::string_view str, char ch) noexcept {
577 return drop_back(
578 str, str.size() - (std::min)(str.size(), str.find_last_not_of(ch) + 1));
579}
580
581/**
582 * Returns @p str with consecutive characters in @p chars starting from
583 * the right removed.
584 */
586 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
587 return drop_back(
588 str,
589 str.size() - (std::min)(str.size(), str.find_last_not_of(chars) + 1));
590}
591
592/**
593 * Returns @p str with consecutive @p ch characters starting from the
594 * left and right removed.
595 */
596constexpr std::string_view trim(std::string_view str, char ch) noexcept {
597 return rtrim(ltrim(str, ch), ch);
598}
599
600/**
601 * Returns @p str with consecutive characters in @p chars starting from
602 * the left and right removed.
603 */
605 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
606 return rtrim(ltrim(str, chars), chars);
607}
608
609namespace detail {
611 std::string_view str, unsigned radix,
612 unsigned long long& result) noexcept; // NOLINT(runtime/int)
613bool GetAsSignedInteger(std::string_view str, unsigned radix,
614 long long& result) noexcept; // NOLINT(runtime/int)
615
617 std::string_view& str, unsigned radix,
618 unsigned long long& result) noexcept; // NOLINT(runtime/int)
619bool ConsumeSignedInteger(std::string_view& str, unsigned radix,
620 long long& result) noexcept; // NOLINT(runtime/int)
621} // namespace detail
622
623/**
624 * Parses the string @p str as an integer of the specified radix. If
625 * @p radix is specified as zero, this does radix autosensing using
626 * extended C rules: 0 is octal, 0x is hex, 0b is binary.
627 *
628 * If the string is invalid or if only a subset of the string is valid,
629 * this returns nullopt to signify the error. The string is considered
630 * erroneous if empty or if it overflows T.
631 */
632template <typename T,
634inline std::optional<T> parse_integer(std::string_view str,
635 unsigned radix) noexcept {
636 long long val; // NOLINT(runtime/int)
637 if (detail::GetAsSignedInteger(str, radix, val) ||
638 static_cast<T>(val) != val) {
639 return std::nullopt;
640 }
641 return val;
642}
643
644template <typename T,
646inline std::optional<T> parse_integer(std::string_view str,
647 unsigned radix) noexcept {
648 using Int = unsigned long long; // NOLINT(runtime/int)
649 Int val;
650 // The additional cast to unsigned long long is required to avoid the
651 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
652 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
653 if (detail::GetAsUnsignedInteger(str, radix, val) ||
654 static_cast<Int>(static_cast<T>(val)) != val) {
655 return std::nullopt;
656 }
657 return val;
658}
659
660/**
661 * Parses the string @p str as an integer of the specified radix. If
662 * @p radix is specified as zero, this does radix autosensing using
663 * extended C rules: 0 is octal, 0x is hex, 0b is binary.
664 *
665 * If the string does not begin with a number of the specified radix,
666 * this returns nullopt to signify the error. The string is considered
667 * erroneous if empty or if it overflows T.
668 * The portion of the string representing the discovered numeric value
669 * is removed from the beginning of the string.
670 */
671template <typename T,
673inline std::optional<T> consume_integer(std::string_view* str,
674 unsigned radix) noexcept {
675 using Int = long long; // NOLINT(runtime/int)
676 Int val;
677 if (detail::ConsumeSignedInteger(*str, radix, val) ||
678 static_cast<Int>(static_cast<T>(val)) != val) {
679 return std::nullopt;
680 }
681 return val;
682}
683
684template <typename T,
686inline std::optional<T> consume_integer(std::string_view* str,
687 unsigned radix) noexcept {
688 using Int = unsigned long long; // NOLINT(runtime/int)
689 Int val;
690 if (detail::ConsumeUnsignedInteger(*str, radix, val) ||
691 static_cast<Int>(static_cast<T>(val)) != val) {
692 return std::nullopt;
693 }
694 return val;
695}
696
697/**
698 * Parses the string @p str as a floating point value.
699 *
700 * If the string is invalid or if only a subset of the string is valid,
701 * this returns nullopt to signify the error. The string is considered
702 * erroneous if empty or if it overflows T.
703 */
704template <typename T>
705std::optional<T> parse_float(std::string_view str) noexcept;
706
707template <>
708std::optional<float> parse_float<float>(std::string_view str) noexcept;
709template <>
710std::optional<double> parse_float<double>(std::string_view str) noexcept;
711template <>
712std::optional<long double> parse_float<long double>(
713 std::string_view str) noexcept;
714
715/**
716 * Unescapes a C-style string (reverse operation to raw_ostream::write_escaped).
717 * Scans through @p str until either the end is reached or an unescaped double
718 * quote character is found.
719 *
720 * @param str input string
721 * @param buf buffer for unescaped characters
722 * @return pair of the unescaped string and any remaining input
723 */
724std::pair<std::string_view, std::string_view> UnescapeCString(
726
727/**
728 * Like std::format_to_n() in that it writes at most n bytes to the output
729 * buffer, but also includes a terminating null byte in n.
730 *
731 * This is essentially a more performant replacement for std::snprintf().
732 *
733 * @param out The output buffer.
734 * @param n The size of the output buffer.
735 * @param fmt The format string.
736 * @param args The format string arguments.
737 */
738template <class OutputIt, class... Args>
739inline void format_to_n_c_str(OutputIt out, std::iter_difference_t<OutputIt> n,
740 fmt::format_string<Args...> fmt, Args&&... args) {
741 const auto result =
742 fmt::format_to_n(out, n - 1, fmt, std::forward<Args>(args)...);
743 *result.out = '\0';
744}
745
746} // namespace wpi
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition: ThirdPartyNotices.txt:128
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:579
basic_string_view< char > string_view
Definition: core.h:501
detail namespace with internal helper functions
Definition: xchar.h:20
std::integral_constant< bool, std::numeric_limits< T >::is_signed||std::is_same< T, int128_opt >::value > is_signed
Definition: format.h:811
T mod(T x, int y)
Definition: chrono.h:1605
Definition: array.h:89
UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3417
UnitTypeLhs() min(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3409
bool GetAsSignedInteger(std::string_view str, unsigned radix, long long &result) noexcept
bool ConsumeUnsignedInteger(std::string_view &str, unsigned radix, unsigned long long &result) noexcept
bool GetAsUnsignedInteger(std::string_view str, unsigned radix, unsigned long long &result) noexcept
bool ConsumeSignedInteger(std::string_view &str, unsigned radix, long long &result) noexcept
Definition: ntcore_cpp.h:26
constexpr char hexdigit(unsigned X, bool LowerCase=false) noexcept
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
Definition: StringExtras.h:37
constexpr std::string_view slice(std::string_view str, std::string_view::size_type start, std::string_view::size_type end) noexcept
Returns a reference to the substring of str from [start, end).
Definition: StringExtras.h:420
constexpr std::pair< std::string_view, std::string_view > split(std::string_view str, char separator) noexcept
Splits str into two substrings around the first occurrence of a separator character.
Definition: StringExtras.h:441
int compare_lower(std::string_view lhs, std::string_view rhs) noexcept
compare_lower - Compare two strings, ignoring case.
constexpr std::string_view ltrim(std::string_view str, char ch) noexcept
Returns str with consecutive ch characters starting from the the left removed.
Definition: StringExtras.h:559
constexpr std::span< T > drop_back(std::span< T, N > in, typename std::span< T >::size_type n=1)
Drop the last N elements of the array.
Definition: SpanExtras.h:22
constexpr std::span< T > drop_front(std::span< T, N > in, typename std::span< T >::size_type n=1)
Drop the first N elements of the array.
Definition: SpanExtras.h:14
std::optional< double > parse_float< double >(std::string_view str) noexcept
constexpr bool isAlnum(char C) noexcept
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
Definition: StringExtras.h:76
std::optional< T > consume_integer(std::string_view *str, unsigned radix) noexcept
Parses the string str as an integer of the specified radix.
Definition: StringExtras.h:673
std::string_view::size_type find_lower(std::string_view str, char ch, std::string_view::size_type from=0) noexcept
Search for the first character ch in str, ignoring case.
constexpr bool isHexDigit(char C) noexcept
Checks if character C is a hexadecimal numeric character.
Definition: StringExtras.h:65
constexpr bool isDigit(char C) noexcept
Checks if character C is one of the 10 decimal digits.
Definition: StringExtras.h:60
std::optional< T > parse_integer(std::string_view str, unsigned radix) noexcept
Parses the string str as an integer of the specified radix.
Definition: StringExtras.h:634
constexpr unsigned hexDigitValue(char C) noexcept
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:46
constexpr char toLower(char x) noexcept
Returns the corresponding lowercase character if x is uppercase.
Definition: StringExtras.h:95
constexpr std::string_view trim(std::string_view str, char ch) noexcept
Returns str with consecutive ch characters starting from the left and right removed.
Definition: StringExtras.h:596
constexpr char toUpper(char x) noexcept
Returns the corresponding uppercase character if x is lowercase.
Definition: StringExtras.h:103
constexpr std::span< T > take_front(std::span< T, N > in, typename std::span< T >::size_type n=1)
Returns a span equal to in but with only the first n elements remaining.
Definition: SpanExtras.h:34
constexpr bool contains(std::string_view str, std::string_view other) noexcept
Checks if str contains the substring other.
Definition: StringExtras.h:320
bool ends_with_lower(std::string_view str, std::string_view suffix) noexcept
Checks if str ends with the given suffix, ignoring case.
std::string_view::size_type rfind_lower(std::string_view str, char ch, std::string_view::size_type from=std::string_view::npos) noexcept
Search for the last character ch in str, ignoring case.
bool starts_with_lower(std::string_view str, std::string_view prefix) noexcept
Checks if str starts with the given prefix, ignoring case.
constexpr bool equals_lower(std::string_view lhs, std::string_view rhs) noexcept
equals_lower - Check for string equality, ignoring case.
Definition: StringExtras.h:146
std::optional< long double > parse_float< long double >(std::string_view str) noexcept
constexpr std::pair< std::string_view, std::string_view > rsplit(std::string_view str, char separator) noexcept
Splits str into two substrings around the last occurrence of a separator character.
Definition: StringExtras.h:484
constexpr std::string_view substr(std::string_view str, std::string_view::size_type start, std::string_view::size_type n=std::string_view::npos) noexcept
Returns the substring of str from [start, start + n).
Definition: StringExtras.h:224
constexpr bool equals(std::string_view lhs, std::string_view rhs) noexcept
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringExtras.h:132
constexpr bool isPrint(char C) noexcept
Checks whether character C is printable.
Definition: StringExtras.h:89
constexpr std::span< T > take_back(std::span< T, N > in, typename std::span< T >::size_type n=1)
Returns a span equal to in but with only the last n elements remaining.
Definition: SpanExtras.h:49
std::pair< std::string_view, std::string_view > UnescapeCString(std::string_view str, SmallVectorImpl< char > &buf)
Unescapes a C-style string (reverse operation to raw_ostream::write_escaped).
void format_to_n_c_str(OutputIt out, std::iter_difference_t< OutputIt > n, fmt::format_string< Args... > fmt, Args &&... args)
Like std::format_to_n() in that it writes at most n bytes to the output buffer, but also includes a t...
Definition: StringExtras.h:739
constexpr bool isASCII(char C) noexcept
Checks whether character C is valid ASCII (high bit is zero).
Definition: StringExtras.h:81
constexpr bool isAlpha(char C) noexcept
Checks if character C is a valid letter as classified by "C" locale.
Definition: StringExtras.h:70
constexpr std::string_view rtrim(std::string_view str, char ch) noexcept
Returns str with consecutive Char characters starting from the right removed.
Definition: StringExtras.h:576
std::optional< T > parse_float(std::string_view str) noexcept
Parses the string str as a floating point value.
constexpr bool ends_with(std::string_view str, std::string_view suffix) noexcept
Checks if str ends with the given suffix.
Definition: StringExtras.h:277
std::string utohexstr(unsigned long long val, bool lowerCase=false)
Definition: StringExtras.h:110
std::optional< float > parse_float< float >(std::string_view str) noexcept
constexpr bool starts_with(std::string_view str, std::string_view prefix) noexcept
Checks if str starts with the given prefix.
Definition: StringExtras.h:235
bool contains_lower(std::string_view str, std::string_view other) noexcept
Checks if str contains the substring other, ignoring case.
Definition: StringExtras.h:341
auto format_to_n(OutputIt out, size_t n, const S &fmt, T &&... args) -> format_to_n_result< OutputIt >
Definition: xchar.h:204