WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 }
56 return (std::numeric_limits<unsigned>::max)();
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 {
66 return hexDigitValue(C) != (std::numeric_limits<unsigned>::max)();
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 */
141int compare_lower(std::string_view lhs, std::string_view rhs) noexcept;
142
143/**
144 * equals_lower - Check for string equality, ignoring case.
145 */
146constexpr bool equals_lower(std::string_view lhs,
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 */
224constexpr std::string_view substr(
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 */
257bool starts_with_lower(std::string_view str, std::string_view prefix) noexcept;
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 */
269inline bool starts_with_lower(std::string_view str,
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 */
301bool ends_with_lower(std::string_view str, std::string_view suffix) noexcept;
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 */
341inline bool contains_lower(std::string_view str,
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 an optional containing @p str but with @p prefix removed if the string
362 * starts with the prefix. If the string does not start with the prefix, return
363 * an empty optional.
364 */
365constexpr std::optional<std::string_view> remove_prefix(std::string_view str, std::string_view prefix) noexcept {
366 if (str.starts_with(prefix)) {
367 str.remove_prefix(prefix.size());
368 return str;
369 }
370 return std::nullopt;
371}
372
373/**
374 * Return an optional containing @p str but with @p suffix removed if the
375 * string ends with the suffix. If the string does not end with the suffix,
376 * return an empty optional.
377 */
378constexpr std::optional<std::string_view> remove_suffix(std::string_view str, std::string_view suffix) noexcept {
379 if (str.ends_with(suffix)) {
380 str.remove_suffix(suffix.size());
381 return str;
382 }
383 return std::nullopt;
384}
385
386/**
387 * Return a string_view equal to @p str but with the first @p n elements
388 * dropped.
389 */
390constexpr std::string_view drop_front(
391 std::string_view str, std::string_view::size_type n = 1) noexcept {
392 str.remove_prefix(n);
393 return str;
394}
395
396/**
397 * Return a string_view equal to @p str but with the last @p n elements dropped.
398 */
399constexpr std::string_view drop_back(
400 std::string_view str, std::string_view::size_type n = 1) noexcept {
401 str.remove_suffix(n);
402 return str;
403}
404
405/**
406 * Returns a view equal to @p str but with only the first @p n
407 * elements remaining. If @p n is greater than the length of the
408 * string, the entire string is returned.
409 */
410constexpr std::string_view take_front(
411 std::string_view str, std::string_view::size_type n = 1) noexcept {
412 auto length = str.size();
413 if (n >= length) {
414 return str;
415 }
416 return drop_back(str, length - n);
417}
418
419/**
420 * Returns a view equal to @p str but with only the last @p n
421 * elements remaining. If @p n is greater than the length of the
422 * string, the entire string is returned.
423 */
424constexpr std::string_view take_back(
425 std::string_view str, std::string_view::size_type n = 1) noexcept {
426 auto length = str.size();
427 if (n >= length) {
428 return str;
429 }
430 return drop_front(str, length - n);
431}
432
433/**
434 * Returns a reference to the substring of @p str from [start, end).
435 *
436 * @param start The index of the starting character in the substring; if
437 * the index is npos or greater than the length of the string then the
438 * empty substring will be returned.
439 *
440 * @param end The index following the last character to include in the
441 * substring. If this is npos or exceeds the number of characters
442 * remaining in the string, the string suffix (starting with @p start)
443 * will be returned. If this is less than @p start, an empty string will
444 * be returned.
445 */
446constexpr std::string_view slice(std::string_view str,
447 std::string_view::size_type start,
448 std::string_view::size_type end) noexcept {
449 auto length = str.size();
450 start = (std::min)(start, length);
451 end = (std::min)((std::max)(start, end), length);
452 return {str.data() + start, end - start};
453}
454
455/**
456 * Splits @p str into two substrings around the first occurrence of a separator
457 * character.
458 *
459 * If @p separator is in the string, then the result is a pair (LHS, RHS)
460 * such that (str == LHS + separator + RHS) is true and RHS is
461 * maximal. If @p separator is not in the string, then the result is a
462 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
463 *
464 * @param separator The character to split on.
465 * @returns The split substrings.
466 */
467constexpr std::pair<std::string_view, std::string_view> split(
468 std::string_view str, char separator) noexcept {
469 auto idx = str.find(separator);
470 if (idx == std::string_view::npos) {
471 return {str, {}};
472 }
473 return {slice(str, 0, idx), slice(str, idx + 1, std::string_view::npos)};
474}
475
476/**
477 * Splits @p str into two substrings around the first occurrence of a separator
478 * string.
479 *
480 * If @p separator is in the string, then the result is a pair (LHS, RHS)
481 * such that (str == LHS + separator + RHS) is true and RHS is
482 * maximal. If @p separator is not in the string, then the result is a
483 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
484 *
485 * @param separator The string to split on.
486 * @return The split substrings.
487 */
488constexpr std::pair<std::string_view, std::string_view> split(
489 std::string_view str, std::string_view separator) noexcept {
490 auto idx = str.find(separator);
491 if (idx == std::string_view::npos) {
492 return {str, {}};
493 }
494 return {slice(str, 0, idx),
495 slice(str, idx + separator.size(), std::string_view::npos)};
496}
497
498/**
499 * Splits @p str into two substrings around the last occurrence of a separator
500 * character.
501 *
502 * If @p separator is in the string, then the result is a pair (LHS, RHS)
503 * such that (str == LHS + separator + RHS) is true and RHS is
504 * minimal. If @p separator is not in the string, then the result is a
505 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
506 *
507 * @param separator The string to split on.
508 * @return The split substrings.
509 */
510constexpr std::pair<std::string_view, std::string_view> rsplit(
511 std::string_view str, char separator) noexcept {
512 auto idx = str.rfind(separator);
513 if (idx == std::string_view::npos) {
514 return {str, {}};
515 }
516 return {slice(str, 0, idx), slice(str, idx + 1, std::string_view::npos)};
517}
518
519/**
520 * Splits @p str into two substrings around the last occurrence of a separator
521 * string.
522 *
523 * If @p separator is in the string, then the result is a pair (LHS, RHS)
524 * such that (str == LHS + separator + RHS) is true and RHS is
525 * minimal. If @p separator is not in the string, then the result is a
526 * pair (LHS, RHS) where (str == LHS) and (RHS == "").
527 *
528 * @param separator The string to split on.
529 * @return The split substrings.
530 */
531constexpr std::pair<std::string_view, std::string_view> rsplit(
532 std::string_view str, std::string_view separator) noexcept {
533 auto idx = str.rfind(separator);
534 if (idx == std::string_view::npos) {
535 return {str, {}};
536 }
537 return {slice(str, 0, idx),
538 slice(str, idx + separator.size(), std::string_view::npos)};
539}
540
541/**
542 * Splits @p str into substrings around the occurrences of a separator string.
543 *
544 * Each substring is stored in @p arr. If @p maxSplit is >= 0, at most
545 * @p maxSplit splits are done and consequently <= @p maxSplit + 1
546 * elements are added to arr.
547 * If @p keepEmpty is false, empty strings are not added to @p arr. They
548 * still count when considering @p maxSplit
549 * An useful invariant is that
550 * separator.join(arr) == str if maxSplit == -1 and keepEmpty == true
551 *
552 * @param arr Where to put the substrings.
553 * @param separator The string to split on.
554 * @param maxSplit The maximum number of times the string is split.
555 * @param keepEmpty True if empty substring should be added.
556 */
557void split(std::string_view str, SmallVectorImpl<std::string_view>& arr,
558 std::string_view separator, int maxSplit = -1,
559 bool keepEmpty = true) noexcept;
560
561/**
562 * Splits @p str into substrings around the occurrences of a separator
563 * character.
564 *
565 * Each substring is stored in @p arr. If @p maxSplit is >= 0, at most
566 * @p maxSplit splits are done and consequently <= @p maxSplit + 1
567 * elements are added to arr.
568 * If @p keepEmpty is false, empty strings are not added to @p arr. They
569 * still count when considering @p maxSplit
570 * An useful invariant is that
571 * separator.join(arr) == str if maxSplit == -1 and keepEmpty == true
572 *
573 * @param arr Where to put the substrings.
574 * @param separator The character to split on.
575 * @param maxSplit The maximum number of times the string is split.
576 * @param keepEmpty True if empty substring should be added.
577 */
579 char separator, int maxSplit = -1, bool keepEmpty = true) noexcept;
580
581/**
582 * Returns @p str with consecutive @p ch characters starting from the
583 * the left removed.
584 */
585constexpr std::string_view ltrim(std::string_view str, char ch) noexcept {
586 return drop_front(str, (std::min)(str.size(), str.find_first_not_of(ch)));
587}
588
589/**
590 * Returns @p str with consecutive characters in @p chars starting from
591 * the left removed.
592 */
593constexpr std::string_view ltrim(
594 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
595 return drop_front(str, (std::min)(str.size(), str.find_first_not_of(chars)));
596}
597
598/**
599 * Returns @p str with consecutive @p Char characters starting from the
600 * right removed.
601 */
602constexpr std::string_view rtrim(std::string_view str, char ch) noexcept {
603 return drop_back(
604 str, str.size() - (std::min)(str.size(), str.find_last_not_of(ch) + 1));
605}
606
607/**
608 * Returns @p str with consecutive characters in @p chars starting from
609 * the right removed.
610 */
611constexpr std::string_view rtrim(
612 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
613 return drop_back(
614 str,
615 str.size() - (std::min)(str.size(), str.find_last_not_of(chars) + 1));
616}
617
618/**
619 * Returns @p str with consecutive @p ch characters starting from the
620 * left and right removed.
621 */
622constexpr std::string_view trim(std::string_view str, char ch) noexcept {
623 return rtrim(ltrim(str, ch), ch);
624}
625
626/**
627 * Returns @p str with consecutive characters in @p chars starting from
628 * the left and right removed.
629 */
630constexpr std::string_view trim(
631 std::string_view str, std::string_view chars = " \t\n\v\f\r") noexcept {
632 return rtrim(ltrim(str, chars), chars);
633}
634
635namespace detail {
637 std::string_view str, unsigned radix,
638 unsigned long long& result) noexcept; // NOLINT(runtime/int)
639bool GetAsSignedInteger(std::string_view str, unsigned radix,
640 long long& result) noexcept; // NOLINT(runtime/int)
641
643 std::string_view& str, unsigned radix,
644 unsigned long long& result) noexcept; // NOLINT(runtime/int)
645bool ConsumeSignedInteger(std::string_view& str, unsigned radix,
646 long long& result) noexcept; // NOLINT(runtime/int)
647} // namespace detail
648
649/**
650 * Parses the string @p str as an integer of the specified radix. If
651 * @p radix is specified as zero, this does radix autosensing using
652 * extended C rules: 0 is octal, 0x is hex, 0b is binary.
653 *
654 * If the string is invalid or if only a subset of the string is valid,
655 * this returns nullopt to signify the error. The string is considered
656 * erroneous if empty or if it overflows T.
657 */
658template <typename T,
659 std::enable_if_t<std::numeric_limits<T>::is_signed, bool> = true>
660inline std::optional<T> parse_integer(std::string_view str,
661 unsigned radix) noexcept {
662 long long val; // NOLINT(runtime/int)
663 if (detail::GetAsSignedInteger(str, radix, val) ||
664 static_cast<T>(val) != val) {
665 return std::nullopt;
666 }
667 return val;
668}
669
670template <typename T,
671 std::enable_if_t<!std::numeric_limits<T>::is_signed, bool> = true>
672inline std::optional<T> parse_integer(std::string_view str,
673 unsigned radix) noexcept {
674 using Int = unsigned long long; // NOLINT(runtime/int)
675 Int val;
676 // The additional cast to unsigned long long is required to avoid the
677 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
678 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
679 if (detail::GetAsUnsignedInteger(str, radix, val) ||
680 static_cast<Int>(static_cast<T>(val)) != val) {
681 return std::nullopt;
682 }
683 return static_cast<T>(val);
684}
685
686/**
687 * Parses the string @p str as an integer of the specified radix. If
688 * @p radix is specified as zero, this does radix autosensing using
689 * extended C rules: 0 is octal, 0x is hex, 0b is binary.
690 *
691 * If the string does not begin with a number of the specified radix,
692 * this returns nullopt to signify the error. The string is considered
693 * erroneous if empty or if it overflows T.
694 * The portion of the string representing the discovered numeric value
695 * is removed from the beginning of the string.
696 */
697template <typename T,
698 std::enable_if_t<std::numeric_limits<T>::is_signed, bool> = true>
699inline std::optional<T> consume_integer(std::string_view* str,
700 unsigned radix) noexcept {
701 using Int = long long; // NOLINT(runtime/int)
702 Int val;
703 if (detail::ConsumeSignedInteger(*str, radix, val) ||
704 static_cast<Int>(static_cast<T>(val)) != val) {
705 return std::nullopt;
706 }
707 return val;
708}
709
710template <typename T,
711 std::enable_if_t<!std::numeric_limits<T>::is_signed, bool> = true>
712inline std::optional<T> consume_integer(std::string_view* str,
713 unsigned radix) noexcept {
714 using Int = unsigned long long; // NOLINT(runtime/int)
715 Int val;
716 if (detail::ConsumeUnsignedInteger(*str, radix, val) ||
717 static_cast<Int>(static_cast<T>(val)) != val) {
718 return std::nullopt;
719 }
720 return val;
721}
722
723/**
724 * Parses the string @p str as a floating point value.
725 *
726 * If the string is invalid or if only a subset of the string is valid,
727 * this returns nullopt to signify the error. The string is considered
728 * erroneous if empty or if it overflows T.
729 */
730template <typename T>
731std::optional<T> parse_float(std::string_view str) noexcept;
732
733template <>
734std::optional<float> parse_float<float>(std::string_view str) noexcept;
735template <>
736std::optional<double> parse_float<double>(std::string_view str) noexcept;
737template <>
738std::optional<long double> parse_float<long double>(
739 std::string_view str) noexcept;
740
741/**
742 * Unescapes a C-style string (reverse operation to raw_ostream::write_escaped).
743 * Scans through @p str until either the end is reached or an unescaped double
744 * quote character is found.
745 *
746 * @param str input string
747 * @param buf buffer for unescaped characters
748 * @return pair of the unescaped string and any remaining input
749 */
750std::pair<std::string_view, std::string_view> UnescapeCString(
751 std::string_view str, SmallVectorImpl<char>& buf);
752
753/**
754 * Like std::format_to_n() in that it writes at most n bytes to the output
755 * buffer, but also includes a terminating null byte in n.
756 *
757 * This is essentially a more performant replacement for std::snprintf().
758 *
759 * @param out The output buffer.
760 * @param n The size of the output buffer.
761 * @param fmt The format string.
762 * @param args The format string arguments.
763 */
764template <class OutputIt, class... Args>
765inline void format_to_n_c_str(OutputIt out, std::iter_difference_t<OutputIt> n,
766 fmt::format_string<Args...> fmt, Args&&... args) {
767 const auto result =
768 fmt::format_to_n(out, n - 1, fmt, std::forward<Args>(args)...);
769 *result.out = '\0';
770}
771
772} // 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 and nanopb were all modified for use in 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:141
FMT_CONSTEXPR void remove_prefix(size_t n) noexcept
Definition base.h:558
FMT_CONSTEXPR auto compare(basic_string_view other) const -> int
Definition base.h:575
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
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
Foonathan namespace.
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:446
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:467
int compare_lower(std::string_view lhs, std::string_view rhs) noexcept
compare_lower - Compare two strings, ignoring case.
constexpr std::optional< std::string_view > remove_prefix(std::string_view str, std::string_view prefix) noexcept
Return an optional containing str but with prefix removed if the string starts with the prefix.
Definition StringExtras.h:365
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:585
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:699
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:660
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::optional< std::string_view > remove_suffix(std::string_view str, std::string_view suffix) noexcept
Return an optional containing str but with suffix removed if the string ends with the suffix.
Definition StringExtras.h:378
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:622
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:510
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 T mod(U Numerator, V Denominator)
Returns the remainder of the Euclidean division of LHS by RHS.
Definition MathExtras.h:432
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:765
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:602
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