36 static constexpr size_t npos = SIZE_MAX;
38 kmp_str_ref(
const char *str) : data(str), len(str ? strlen(str) : 0) {}
39 kmp_str_ref(
const char *str,
size_t len) : data(str), len(len) {
40 assert((data || !len) &&
"len must be 0 for nullptr data");
53 if (memcmp(data, prefix.data, prefix.len) != 0)
63 bool allow_negative =
false);
71 template <
typename Fn>
size_t count_while(
const Fn &predicate)
const {
72 static_assert(std::is_invocable_r_v<bool, Fn, char>,
73 "predicate must be callable as bool(char)");
75 return n ==
npos ? len : n;
88 template <
typename Fn>
void drop_while(
const Fn &predicate) {
89 static_assert(std::is_invocable_r_v<bool, Fn, char>,
90 "predicate must be callable as bool(char)");
95 bool empty()
const {
return len == 0; }
100 template <
typename Fn>
size_t find_if(
const Fn &predicate)
const {
101 static_assert(std::is_invocable_r_v<bool, Fn, char>,
102 "predicate must be callable as bool(char)");
104 while (
i < len && !predicate(data[
i]))
106 return i < len ?
i :
npos;
112 template <
typename Fn>
size_t find_if_not(
const Fn &predicate)
const {
113 static_assert(std::is_invocable_r_v<bool, Fn, char>,
114 "predicate must be callable as bool(char)");
115 return find_if([predicate](
char c) {
return !predicate(c); });
125 return static_cast<bool>(isspace(
static_cast<unsigned char>(c)));
132 static_assert(std::is_invocable_r_v<bool, Fn, char>,
133 "predicate must be callable as bool(char)");
138 const char *
begin()
const {
return data; }
139 const char *
end()
const {
return data + len; }
kmp_str_ref take_while(const Fn &predicate) const
Construct a new string with the longest prefix of the original string that satisfies the predicate.
size_t find_if_not(const Fn &predicate) const
Return the index of the first character in the string for which the predicate returns false.
kmp_str_ref(const kmp_str_ref &other)=default
static constexpr size_t npos
kmp_str_ref(const char *str)
size_t length() const
Get the length of the string.
bool consume_front(kmp_str_ref prefix)
Check if the string starts with the given prefix and remove it from the string afterwards.
char * copy() const
Get an own duplicate of the string.
bool consume_integer(int &value, bool allow_zero=true, bool allow_negative=false)
Start consuming an integer from the start of the string and remove it from the string afterwards.
kmp_str_ref & operator=(const kmp_str_ref &other)=default
bool empty() const
Check if the string is empty.
kmp_str_ref(const char *str, size_t len)
size_t find_if(const Fn &predicate) const
Return the index of the first character in the string for which the predicate returns true.
size_t count_while(const Fn &predicate) const
Count the number of characters in the string while the predicate returns true.
const char * begin() const
Iterator support (raw pointers work as iterators for contiguous storage)
void skip_space()
Drop space from the start of the string.
void drop_front(size_t n)
Drop the first n characters from the string.
void drop_while(const Fn &predicate)
Drop characters from the string while the predicate returns true.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value