2#include "gaia/config/config.h"
7#include "gaia/cnt/darray.h"
8#include "gaia/core/span.h"
36 GAIA_NODISCARD
constexpr const char*
data()
const {
42 GAIA_NODISCARD
constexpr uint32_t
size()
const {
48 GAIA_NODISCARD
constexpr bool empty()
const {
56 GAIA_NODISCARD
constexpr uint32_t
find(
str_view value, uint32_t pos = 0)
const {
65 GAIA_NODISCARD
constexpr uint32_t
find(
const char* value, uint32_t len, uint32_t pos)
const {
73 for (uint32_t i = pos; i + len <=
m_size; ++i) {
74 if (equal_bytes(
m_data + i, value, len))
86 GAIA_NODISCARD
constexpr uint32_t
find(
const char (&lit)[N], uint32_t pos = 0)
const {
95 GAIA_NODISCARD
constexpr uint32_t
find(
char ch, uint32_t pos = 0)
const {
98 for (uint32_t i = pos; i <
m_size; ++i) {
112 for (uint32_t i = pos; i <
m_size; ++i) {
113 if (contains(chars,
m_data[i]))
123 GAIA_NODISCARD
constexpr uint32_t
find_first_of(
char ch, uint32_t pos = 0)
const {
124 return find(ch, pos);
133 GAIA_NODISCARD
constexpr uint32_t
find_first_of(
const char (&lit)[N], uint32_t pos = 0)
const {
146 if (i == BadIndex || i >=
m_size)
150 if (contains(chars,
m_data[i]))
163 GAIA_NODISCARD
constexpr uint32_t
find_last_of(
char ch, uint32_t pos = BadIndex)
const {
168 if (i == BadIndex || i >=
m_size)
187 GAIA_NODISCARD
constexpr uint32_t
find_last_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
201 for (uint32_t i = pos; i <
m_size; ++i) {
202 if (!contains(chars,
m_data[i]))
215 for (uint32_t i = pos; i <
m_size; ++i) {
228 GAIA_NODISCARD
constexpr uint32_t
find_first_not_of(
const char (&lit)[N], uint32_t pos = 0)
const {
241 if (i == BadIndex || i >=
m_size)
248 if (!contains(chars,
m_data[i]))
261 GAIA_NODISCARD
constexpr uint32_t
find_last_not_of(
char ch, uint32_t pos = BadIndex)
const {
266 if (i == BadIndex || i >=
m_size)
285 GAIA_NODISCARD
constexpr uint32_t
find_last_not_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
294 GAIA_NODISCARD
constexpr bool operator==(
const char (&lit)[N])
const {
295 static_assert(N > 0);
314 GAIA_NODISCARD
static constexpr bool contains(
str_view set,
char value) {
315 for (uint32_t i = 0; i < set.m_size; ++i) {
316 if (set.m_data[i] == value)
322 GAIA_NODISCARD
static constexpr bool equal_bytes(
const char* left,
const char* right, uint32_t
size) {
323 for (uint32_t i = 0; i <
size; ++i) {
324 if (left[i] != right[i])
348 explicit str(
const char (&lit)[N]) {
383 static_assert(N > 0);
384 assign(lit, (uint32_t)(N - 1));
392 const auto oldSize = this->
size();
394 GAIA_ASSERT(
size <= (uint32_t)-1 - oldSize);
395 if (
size == 0 ||
size > (uint32_t)-1 - oldSize)
413 static_assert(N > 0);
414 append(lit, (uint32_t)(N - 1));
425 GAIA_NODISCARD
const char*
data()
const {
437 GAIA_NODISCARD uint32_t
size()
const {
465 static_assert(N > 0);
466 const auto len = (uint32_t)(N - 1);
467 return size() == len && (len == 0 || memcmp(
data(), lit, len) == 0);
497 GAIA_NODISCARD uint32_t
find(
const char* value, uint32_t len, uint32_t pos)
const {
498 return view().
find(value, len, pos);
507 GAIA_NODISCARD uint32_t
find(
const char (&lit)[N], uint32_t pos = 0)
const {
508 static_assert(N > 0);
516 GAIA_NODISCARD uint32_t
find(
char ch, uint32_t pos = 0)
const {
542 GAIA_NODISCARD uint32_t
find_first_of(
const char (&lit)[N], uint32_t pos = 0)
const {
558 GAIA_NODISCARD uint32_t
find_last_of(
char ch, uint32_t pos = BadIndex)
const {
568 GAIA_NODISCARD uint32_t
find_last_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
620 GAIA_NODISCARD uint32_t
find_last_not_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
628 GAIA_NODISCARD
constexpr bool is_whitespace(
char c) {
629 return c ==
' ' || (c >=
'\t' && c <=
'\r');
635 GAIA_NODISCARD
constexpr str_view trim(str_view expr) {
636 const auto len = expr.
size();
641 while (beg < len && is_whitespace(expr.data()[beg]))
646 uint32_t end = len - 1;
647 while (end > beg && is_whitespace(expr.data()[end]))
649 return str_view(expr.data() + beg, end - beg + 1);
655 GAIA_NODISCARD
constexpr std::span<const char> trim(std::span<const char> expr) {
656 const auto trimmed = trim(str_view(expr.data(), (uint32_t)expr.size()));
657 return std::span<const char>(trimmed.data(), trimmed.size());
Array of elements of type.
Definition darray_ext_impl.h:31
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_ext_impl.h:561
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_ext_impl.h:266
void push_back(const T &arg)
Appends an element.
Definition darray_ext_impl.h:360
void resize(size_type count, ResizePolicy policy=ResizePolicy::Exact)
Changes the number of elements.
Definition darray_ext_impl.h:289
void clear() noexcept
Removes all elements.
Definition darray_ext_impl.h:518
GAIA_NODISCARD pointer data() noexcept
Returns a pointer to the element storage.
Definition darray_ext_impl.h:236
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_ext_impl.h:567
Lightweight non-owning string view over a character sequence.
Definition str.h:13
GAIA_NODISCARD constexpr uint32_t find_last_of(const char(&lit)[N], uint32_t pos=BadIndex) const
Finds the last character that is present in literal set lit.
Definition str.h:187
GAIA_NODISCARD constexpr uint32_t find_first_of(char ch, uint32_t pos=0) const
Finds the first occurrence of character ch.
Definition str.h:123
GAIA_NODISCARD constexpr uint32_t size() const
Returns the number of characters in the view.
Definition str.h:42
GAIA_NODISCARD constexpr bool operator==(const char(&lit)[N]) const
Compares this view with literal lit for exact byte equality.
Definition str.h:294
constexpr str_view(const char *data, uint32_t size)
Constructs a string view from a pointer and an explicit length.
Definition str.h:24
GAIA_NODISCARD constexpr bool operator==(str_view other) const
Compares this view with view other for exact byte equality.
Definition str.h:302
GAIA_NODISCARD constexpr uint32_t find(char ch, uint32_t pos=0) const
Finds the first occurrence of character ch starting at index pos.
Definition str.h:95
GAIA_NODISCARD constexpr uint32_t find_last_not_of(const char(&lit)[N], uint32_t pos=BadIndex) const
Finds the last character that is NOT present in literal set lit.
Definition str.h:285
GAIA_NODISCARD constexpr bool empty() const
Checks whether the view contains no characters.
Definition str.h:48
GAIA_NODISCARD constexpr uint32_t find_last_of(str_view chars, uint32_t pos=BadIndex) const
Finds the last character that is present in set chars.
Definition str.h:141
uint32_t m_size
Number of characters in the view.
Definition str.h:17
GAIA_NODISCARD constexpr uint32_t find_first_not_of(str_view chars, uint32_t pos=0) const
Finds the first character that is NOT present in set chars.
Definition str.h:195
GAIA_NODISCARD constexpr uint32_t find_last_not_of(str_view chars, uint32_t pos=BadIndex) const
Finds the last character that is NOT present in set chars.
Definition str.h:236
const char * m_data
Pointer to the first character in the view.
Definition str.h:15
GAIA_NODISCARD constexpr uint32_t find_first_of(str_view chars, uint32_t pos=0) const
Finds the first character that is present in set chars.
Definition str.h:109
GAIA_NODISCARD constexpr uint32_t find_last_not_of(char ch, uint32_t pos=BadIndex) const
Finds the last character that is different from ch.
Definition str.h:261
GAIA_NODISCARD constexpr uint32_t find_first_of(const char(&lit)[N], uint32_t pos=0) const
Finds the first character that is present in literal set lit.
Definition str.h:133
GAIA_NODISCARD constexpr uint32_t find_last_of(char ch, uint32_t pos=BadIndex) const
Finds the last occurrence of character ch.
Definition str.h:163
constexpr str_view(const char(&lit)[N])
Constructs a string view from a literal, excluding its trailing null terminator.
Definition str.h:30
GAIA_NODISCARD constexpr uint32_t find(const char(&lit)[N], uint32_t pos=0) const
Finds the first occurrence of literal lit starting at index pos.
Definition str.h:86
GAIA_NODISCARD constexpr uint32_t find(const char *value, uint32_t len, uint32_t pos) const
Finds the first occurrence of a character sequence starting at index pos.
Definition str.h:65
GAIA_NODISCARD constexpr bool operator!=(str_view other) const
Compares this view with view other for exact byte inequality.
Definition str.h:309
GAIA_NODISCARD constexpr uint32_t find_first_not_of(const char(&lit)[N], uint32_t pos=0) const
Finds the first character that is NOT present in literal set lit.
Definition str.h:228
GAIA_NODISCARD constexpr const char * data() const
Returns the underlying character pointer.
Definition str.h:36
GAIA_NODISCARD constexpr uint32_t find(str_view value, uint32_t pos=0) const
Finds the first occurrence of substring value starting at index pos.
Definition str.h:56
GAIA_NODISCARD constexpr uint32_t find_first_not_of(char ch, uint32_t pos=0) const
Finds the first character that is different from ch.
Definition str.h:212
Lightweight owning string container with explicit length semantics (no implicit null terminator).
Definition str.h:332
void append(str_view view)
Appends view contents.
Definition str.h:404
GAIA_NODISCARD uint32_t size() const
Returns number of characters stored in the string.
Definition str.h:437
GAIA_NODISCARD uint32_t find(str_view value, uint32_t pos=0) const
Finds the first occurrence of substring value starting at index pos.
Definition str.h:488
GAIA_NODISCARD uint32_t find(char ch, uint32_t pos=0) const
Finds the first occurrence of character ch starting at index pos.
Definition str.h:516
GAIA_NODISCARD char * data()
Returns mutable pointer to internal data.
Definition str.h:431
cnt::darray< char > m_data
Contiguous owned character storage.
Definition str.h:334
GAIA_NODISCARD uint32_t find_first_not_of(const char(&lit)[N], uint32_t pos=0) const
Finds the first character that is NOT present in literal set lit.
Definition str.h:594
GAIA_NODISCARD bool operator==(const char(&lit)[N]) const
Compares this string with literal lit for exact byte equality.
Definition str.h:464
GAIA_NODISCARD uint32_t find_first_of(str_view chars, uint32_t pos=0) const
Finds the first character that is present in set chars.
Definition str.h:524
GAIA_NODISCARD uint32_t find_last_of(str_view chars, uint32_t pos=BadIndex) const
Finds the last character that is present in set chars.
Definition str.h:550
GAIA_NODISCARD uint32_t find_first_not_of(str_view chars, uint32_t pos=0) const
Finds the first character that is NOT present in set chars.
Definition str.h:576
GAIA_NODISCARD uint32_t find_first_not_of(char ch, uint32_t pos=0) const
Finds the first character that is different from ch.
Definition str.h:584
void assign(const char(&lit)[N])
Replaces contents with literal lit.
Definition str.h:382
void append(const char *data, uint32_t size)
Appends size characters from data. Appends that would push the length past 32 bits are rejected.
Definition str.h:391
void append(const char(&lit)[N])
Appends literal lit.
Definition str.h:412
GAIA_NODISCARD bool operator==(const str &other) const
Compares this string with string other for exact byte equality.
Definition str.h:480
void clear()
Removes all characters from the string.
Definition str.h:353
GAIA_NODISCARD bool empty() const
Checks whether the string contains no characters.
Definition str.h:443
GAIA_NODISCARD uint32_t find(const char *value, uint32_t len, uint32_t pos) const
Finds the first occurrence of a character sequence starting at index pos.
Definition str.h:497
str(str_view view)
Constructs a string by copying view contents.
Definition str.h:340
GAIA_NODISCARD str_view view() const
Returns a non-owning view over the current contents.
Definition str.h:449
void reserve(uint32_t len)
Reserves capacity for at least len characters.
Definition str.h:359
void assign(const char *data, uint32_t size)
Replaces contents with size characters from data.
Definition str.h:366
GAIA_NODISCARD uint32_t find(const char(&lit)[N], uint32_t pos=0) const
Finds the first occurrence of literal lit starting at index pos.
Definition str.h:507
GAIA_NODISCARD const char * data() const
Returns read-only pointer to internal data.
Definition str.h:425
GAIA_NODISCARD uint32_t find_last_not_of(const char(&lit)[N], uint32_t pos=BadIndex) const
Finds the last character that is NOT present in literal set lit.
Definition str.h:620
void assign(str_view view)
Replaces contents with view contents.
Definition str.h:374
GAIA_NODISCARD bool operator==(str_view other) const
Compares this string with view other for exact byte equality.
Definition str.h:473
GAIA_NODISCARD uint32_t find_last_of(const char(&lit)[N], uint32_t pos=BadIndex) const
Finds the last character that is present in literal set lit.
Definition str.h:568
GAIA_NODISCARD uint32_t find_last_not_of(str_view chars, uint32_t pos=BadIndex) const
Finds the last character that is NOT present in set chars.
Definition str.h:602
GAIA_NODISCARD uint32_t find_first_of(char ch, uint32_t pos=0) const
Finds the first occurrence of character ch.
Definition str.h:532
str(const char(&lit)[N])
Constructs a string from literal lit, excluding trailing null terminator.
Definition str.h:348
void append(char ch)
Appends a single character.
Definition str.h:419
GAIA_NODISCARD uint32_t find_first_of(const char(&lit)[N], uint32_t pos=0) const
Finds the first character that is present in literal set lit.
Definition str.h:542
GAIA_NODISCARD uint32_t find_last_not_of(char ch, uint32_t pos=BadIndex) const
Finds the last character that is different from ch.
Definition str.h:610
GAIA_NODISCARD uint32_t find_last_of(char ch, uint32_t pos=BadIndex) const
Finds the last occurrence of character ch.
Definition str.h:558