2#include "gaia/config/config.h"
7#include "gaia/cnt/darray.h"
8#include "gaia/core/span.h"
14 const char* m_data =
nullptr;
29 constexpr str_view(
const char (&lit)[N]): m_data(lit), m_size((uint32_t)(N - 1)) {
35 GAIA_NODISCARD
constexpr const char*
data()
const {
41 GAIA_NODISCARD
constexpr uint32_t
size()
const {
47 GAIA_NODISCARD
constexpr bool empty()
const {
55 GAIA_NODISCARD
constexpr uint32_t
find(
str_view value, uint32_t pos = 0)
const {
64 GAIA_NODISCARD
constexpr uint32_t
find(
const char* value, uint32_t len, uint32_t pos)
const {
69 if (len > m_size - pos)
72 for (uint32_t i = pos; i + len <= m_size; ++i) {
73 if (equal_bytes(m_data + i, value, len))
85 GAIA_NODISCARD
constexpr uint32_t
find(
const char (&lit)[N], uint32_t pos = 0)
const {
94 GAIA_NODISCARD
constexpr uint32_t
find(
char ch, uint32_t pos = 0)
const {
97 for (uint32_t i = pos; i < m_size; ++i) {
109 if (pos >= m_size || chars.
empty())
111 for (uint32_t i = pos; i < m_size; ++i) {
112 if (contains(chars, m_data[i]))
122 GAIA_NODISCARD
constexpr uint32_t
find_first_of(
char ch, uint32_t pos = 0)
const {
123 return find(ch, pos);
132 GAIA_NODISCARD
constexpr uint32_t
find_first_of(
const char (&lit)[N], uint32_t pos = 0)
const {
141 if (m_size == 0 || chars.
empty())
145 if (i == BadIndex || i >= m_size)
149 if (contains(chars, m_data[i]))
162 GAIA_NODISCARD
constexpr uint32_t
find_last_of(
char ch, uint32_t pos = BadIndex)
const {
167 if (i == BadIndex || i >= m_size)
186 GAIA_NODISCARD
constexpr uint32_t
find_last_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
200 for (uint32_t i = pos; i < m_size; ++i) {
201 if (!contains(chars, m_data[i]))
214 for (uint32_t i = pos; i < m_size; ++i) {
227 GAIA_NODISCARD
constexpr uint32_t
find_first_not_of(
const char (&lit)[N], uint32_t pos = 0)
const {
240 if (i == BadIndex || i >= m_size)
247 if (!contains(chars, m_data[i]))
260 GAIA_NODISCARD
constexpr uint32_t
find_last_not_of(
char ch, uint32_t pos = BadIndex)
const {
265 if (i == BadIndex || i >= m_size)
284 GAIA_NODISCARD
constexpr uint32_t
find_last_not_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
293 GAIA_NODISCARD
constexpr bool operator==(
const char (&lit)[N])
const {
294 static_assert(N > 0);
295 return m_size == (uint32_t)(N - 1) && equal_bytes(m_data, lit, m_size);
302 return m_size == other.m_size && equal_bytes(m_data, other.m_data, m_size);
313 GAIA_NODISCARD
static constexpr bool contains(
str_view set,
char value) {
314 for (uint32_t i = 0; i < set.m_size; ++i) {
315 if (set.m_data[i] == value)
321 GAIA_NODISCARD
static constexpr bool equal_bytes(
const char* left,
const char* right, uint32_t
size) {
322 for (uint32_t i = 0; i <
size; ++i) {
323 if (left[i] != right[i])
347 explicit str(
const char (&lit)[N]) {
382 static_assert(N > 0);
383 assign(lit, (uint32_t)(N - 1));
390 const auto oldSize = this->
size();
391 m_data.resize(oldSize +
size);
393 memcpy(m_data.data() + oldSize,
data,
size);
407 static_assert(N > 0);
408 append(lit, (uint32_t)(N - 1));
414 m_data.push_back(ch);
419 GAIA_NODISCARD
const char*
data()
const {
420 return m_data.data();
426 return m_data.data();
431 GAIA_NODISCARD uint32_t
size()
const {
432 return (uint32_t)m_data.size();
438 return m_data.empty();
459 static_assert(N > 0);
460 const auto len = (uint32_t)(N - 1);
461 return size() == len && (len == 0 || memcmp(
data(), lit, len) == 0);
491 GAIA_NODISCARD uint32_t
find(
const char* value, uint32_t len, uint32_t pos)
const {
492 return view().
find(value, len, pos);
501 GAIA_NODISCARD uint32_t
find(
const char (&lit)[N], uint32_t pos = 0)
const {
502 static_assert(N > 0);
510 GAIA_NODISCARD uint32_t
find(
char ch, uint32_t pos = 0)
const {
536 GAIA_NODISCARD uint32_t
find_first_of(
const char (&lit)[N], uint32_t pos = 0)
const {
552 GAIA_NODISCARD uint32_t
find_last_of(
char ch, uint32_t pos = BadIndex)
const {
562 GAIA_NODISCARD uint32_t
find_last_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
614 GAIA_NODISCARD uint32_t
find_last_not_of(
const char (&lit)[N], uint32_t pos = BadIndex)
const {
622 GAIA_NODISCARD
constexpr bool is_whitespace(
char c) {
623 return c ==
' ' || (c >=
'\t' && c <=
'\r');
629 GAIA_NODISCARD
constexpr str_view trim(str_view expr) {
630 const auto len = expr.
size();
635 while (beg < len && is_whitespace(expr.data()[beg]))
640 uint32_t end = len - 1;
641 while (end > beg && is_whitespace(expr.data()[end]))
643 return str_view(expr.data() + beg, end - beg + 1);
650 const auto trimmed = trim(str_view(expr.data(), (uint32_t)expr.size()));
Array with variable size of elements of type.
Definition darray_impl.h:25
Definition span_impl.h:99
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
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:186
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:122
GAIA_NODISCARD constexpr uint32_t size() const
Returns the number of characters in the view.
Definition str.h:41
GAIA_NODISCARD constexpr bool operator==(const char(&lit)[N]) const
Compares this view with literal lit for exact byte equality.
Definition str.h:293
constexpr str_view(const char *data, uint32_t size)
Constructs a string view from a pointer and an explicit length.
Definition str.h:23
GAIA_NODISCARD constexpr bool operator==(str_view other) const
Compares this view with view other for exact byte equality.
Definition str.h:301
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:94
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:284
GAIA_NODISCARD constexpr bool empty() const
Checks whether the view contains no characters.
Definition str.h:47
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:140
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:194
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:235
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:108
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:260
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:132
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:162
constexpr str_view(const char(&lit)[N])
Constructs a string view from a literal, excluding its trailing null terminator.
Definition str.h:29
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:85
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:64
GAIA_NODISCARD constexpr bool operator!=(str_view other) const
Compares this view with view other for exact byte inequality.
Definition str.h:308
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:227
GAIA_NODISCARD constexpr const char * data() const
Returns the underlying character pointer.
Definition str.h:35
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:55
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:211
str_view()=default
Constructs an empty string view.
Lightweight owning string container with explicit length semantics (no implicit null terminator).
Definition str.h:331
str()=default
Constructs an empty string.
void append(str_view view)
Appends view contents.
Definition str.h:398
GAIA_NODISCARD uint32_t size() const
Returns number of characters stored in the string.
Definition str.h:431
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:482
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:510
GAIA_NODISCARD char * data()
Returns mutable pointer to internal data.
Definition str.h:425
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:588
GAIA_NODISCARD bool operator==(const char(&lit)[N]) const
Compares this string with literal lit for exact byte equality.
Definition str.h:458
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:518
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:544
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:570
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:578
void assign(const char(&lit)[N])
Replaces contents with literal lit.
Definition str.h:381
void append(const char *data, uint32_t size)
Appends size characters from data.
Definition str.h:389
void append(const char(&lit)[N])
Appends literal lit.
Definition str.h:406
GAIA_NODISCARD bool operator==(const str &other) const
Compares this string with string other for exact byte equality.
Definition str.h:474
void clear()
Removes all characters from the string.
Definition str.h:352
GAIA_NODISCARD bool empty() const
Checks whether the string contains no characters.
Definition str.h:437
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:491
str(str_view view)
Constructs a string by copying view contents.
Definition str.h:339
GAIA_NODISCARD str_view view() const
Returns a non-owning view over the current contents.
Definition str.h:443
void reserve(uint32_t len)
Reserves capacity for at least len characters.
Definition str.h:358
void assign(const char *data, uint32_t size)
Replaces contents with size characters from data.
Definition str.h:365
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:501
GAIA_NODISCARD const char * data() const
Returns read-only pointer to internal data.
Definition str.h:419
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:614
void assign(str_view view)
Replaces contents with view contents.
Definition str.h:373
GAIA_NODISCARD bool operator==(str_view other) const
Compares this string with view other for exact byte equality.
Definition str.h:467
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:562
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:596
GAIA_NODISCARD uint32_t find_first_of(char ch, uint32_t pos=0) const
Finds the first occurrence of character ch.
Definition str.h:526
str(const char(&lit)[N])
Constructs a string from literal lit, excluding trailing null terminator.
Definition str.h:347
void append(char ch)
Appends a single character.
Definition str.h:413
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:536
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:604
GAIA_NODISCARD uint32_t find_last_of(char ch, uint32_t pos=BadIndex) const
Finds the last occurrence of character ch.
Definition str.h:552