19#ifndef FENNEC_STRING_CSTRING_H
20#define FENNEC_STRING_CSTRING_H
22#include <fennec/string/detail/_ctype.h>
23#include <fennec/memory/detail/_string.h>
28#include <fennec/memory/bytes.h>
66 static constexpr size_t npos = -1;
74 : _str(nullptr), _size(0), _const(true) {
80 : _str(nullptr), _size(0), _const(true) {
91 if constexpr(not is_constant_evaluated()) {
92 assert(_str[n - 1] ==
'\0',
"Invalid NTBS.");
105 if constexpr(not is_constant_evaluated()) {
106 assert(_str[n - 1] ==
'\0',
"Invalid NTBS.");
118 if constexpr(not is_constant_evaluated()) {
119 assert(_str[n - 1] ==
'\0',
"Invalid NTBS.");
132 if constexpr(not is_constant_evaluated()) {
133 assert(_str[n - 1] ==
'\0',
"Invalid NTBS.");
143 , _const(str._const) {
154 _str =
nullptr, _size = 0, _const =
true;
160 constexpr cstring& operator=(
char(&str)[n]) {
161 assert(_str[n - 1] ==
'\0',
"Invalid NTBS.");
162 _str = str, _size = n - 1, _const =
false;
168 constexpr cstring& operator=(
const char(&str)[n]) {
169 assert(str[n - 1] ==
'\0',
"Invalid NTBS.");
170 _cstr = str; _size = n - 1; _const =
true;
176 _cstr = str._cstr; str._cstr =
nullptr;
177 _size = str._size; str._size = 0;
178 _const = str._const; str._const =
true;
187 constexpr size_t size()
const {
197 constexpr bool empty()
const {
198 return _cstr ==
nullptr || _size == 0;
209 assertd(not _const,
"Attempted to Access Const-Qualified Memory as Non-Const");
210 assertd(i <
size(),
"Array Out of Bounds");
219 assertd(i <
size(),
"Array Out of Bounds");
233 constexpr const char*
data()
const {
239 constexpr operator const char*()
const {
264 n =
min(n,
max(_size, str._size) + 1);
265 return ::strncmp(_cstr + i, str, n);
290 constexpr size_t find(
char c,
size_t i = 0)
const {
295 const char* loc = ::strchr(_cstr + i, c);
296 return loc ? loc - _cstr : _size;
305 if (i + str._size > _size) {
309 const char* loc = ::strstr(_cstr + i, str);
310 return loc ? loc - _cstr : _size;
318 constexpr size_t rfind(
char c,
size_t i = npos)
const {
323 i =
min(i, _size - 1);
325 if (_cstr[i] == c)
return i;
340 const char first = str[0];
341 i =
min(i, _size - str._size);
343 if(_cstr[i] == first) {
344 if (
compare(str, i, str._size - 1) == 0) {
362struct hash<cstring> : hash<byte_array> {
363 constexpr size_t operator()(
const cstring& str)
const {
364 return hash<byte_array>::operator()(byte_array(str, str.size()));
constexpr genType min(genType x, genType y)
Returns if otherwise it returns .
Definition common.h:688
constexpr genType max(genType x, genType y)
Returns if , otherwise it returns .
Definition common.h:705
This struct wraps c-style strings.
Definition cstring.h:64
constexpr size_t find(const cstring &str, size_t i=0) const
Finds the index of the first occurrence of str in the string.
Definition cstring.h:304
constexpr cstring(char *str, size_t n)
Buffer Constructor, wraps the provided C-Style string.
Definition cstring.h:87
constexpr cstring(const char *str, size_t n)
Const Buffer Constructor, wraps the provided C-Style string.
Definition cstring.h:114
constexpr size_t capacity() const
Definition cstring.h:193
constexpr size_t find(char c, size_t i=0) const
Finds the index of the first occurrence of c in the string.
Definition cstring.h:290
constexpr int compare(const cstring &str, size_t i=0, size_t n=npos) const
String Comparison.
Definition cstring.h:259
constexpr cstring(const char(&str)[n])
Buffer Constructor, wraps the provided C-Style string.
Definition cstring.h:128
constexpr cstring(char(&str)[n])
Buffer Constructor, wraps the provided C-Style string.
Definition cstring.h:101
constexpr cstring()
Default Constructor, initializes with nullptr.
Definition cstring.h:73
constexpr size_t size() const
Definition cstring.h:187
constexpr cstring(nullptr_t)
Default Constructor, initializes with nullptr.
Definition cstring.h:79
constexpr char & operator[](size_t i)
Array Access Operator.
Definition cstring.h:208
constexpr size_t rfind(const cstring &str, size_t i=npos) const
Finds the index of the last occurrence of str in the string.
Definition cstring.h:335
constexpr char operator[](size_t i) const
Const-Array Access Operator.
Definition cstring.h:218
constexpr bool operator==(const cstring &str) const
String Equality.
Definition cstring.h:281
constexpr size_t length() const
Definition cstring.h:248
constexpr cstring(cstring &&str) noexcept
Move Constructor.
Definition cstring.h:140
constexpr bool operator==(const char(&str)[n]) const
String Equality.
Definition cstring.h:273
constexpr size_t rfind(char c, size_t i=npos) const
Finds the index of the last occurrence of c in the string.
Definition cstring.h:318
constexpr char * data()
Data Access.
Definition cstring.h:226
constexpr const char * data() const
Data Access.
Definition cstring.h:233
decltype(nullptr) nullptr_t
Null Pointer Type.
Definition types.h:245