19#ifndef FENNEC_STRING_STRING_H
20#define FENNEC_STRING_STRING_H
22#include <fennec/string/cstring.h>
33template<
typename AllocT = allocator<
char>>
struct _string;
42template<
typename AllocT>
46 static constexpr size_t npos = -1;
65 constexpr _string(
size_t n,
char c =
'\0')
67 fennec::memset(_str, c, n);
70 constexpr _string(
const alloc_t& alloc)
74 constexpr _string(
size_t n,
char c,
const alloc_t& alloc)
75 : _str(n + 1, alloc) {
76 fennec::memset(_str, c, n);
79 constexpr _string(
const cstring& cstr)
80 : _str(cstr, cstr.
size() + 1) {
91 explicit constexpr _string(
const char (&str)[n])
92 : _str(str[n - 1] !=
'\0' ? n + 1 : n) {
93 fennec::memcpy(_str, str, n);
94 if (str[n - 1] !=
'\0') {
104 : _str(buf[n - 1] !=
'\0' ? n + 1 : n) {
105 fennec::memcpy(_str, buf, n);
106 if (buf[n - 1] !=
'\0') {
111 constexpr _string(
const string_view& view)
112 :
_string(view.str, view.len) {
133 fennec::memcpy(_str, cstr, cstr.
capacity());
142 _str = fennec::move(str._str);
150 constexpr size_t size()
const {
160 constexpr bool empty()
const {
182 constexpr char* data() {
186 constexpr const char* data()
const {
190 constexpr const char* cstr()
const {
213 return ::strncmp(_str + i, str, n);
221 constexpr int compare(
const string& str,
size_t i = 0,
size_t n = npos)
const {
227 return ::strncmp(_str + i, str.data(), n);
230 constexpr bool operator==(
const cstring& str)
const {
234 constexpr bool operator==(
const _string& str)
const {
243 constexpr bool contains(
char c,
size_t i = 0)
const {
251 constexpr size_t find(
char c,
size_t i = 0)
const {
256 const char* loc = ::strchr(_str + i, c);
257 return loc ? loc - _str :
size();
264 constexpr size_t find(
const string& str,
size_t i = 0)
const {
269 const char* loc = ::strstr(_str, str._str);
270 return loc ? loc - _str :
size();
282 const char* loc = ::strstr(_str + i, str);
283 return loc ? loc - _str :
size();
291 constexpr size_t rfind(
char c,
size_t i = npos)
const {
313 const char first = str[0];
316 if(_str[i] == first) {
330 constexpr size_t rfind(
const string& str,
size_t i = npos)
const {
334 const char first = str[0];
337 if(_str[i] == first) {
352 if (i >=
size() || n == 0) {
358 fennec::memcpy(res.data(), _str + i, n);
366 constexpr void resize(
size_t n) {
371 constexpr _string operator+(
char c)
const {
372 if (_str ==
nullptr) {
377 fennec::memcpy(res.data(), _str,
size());
387 constexpr _string operator+(
const cstring& cstr)
const {
388 if (_str ==
nullptr) {
392 res._str.callocate(
size() + cstr.size() + 1);
393 fennec::memcpy(res.data(), _str,
size());
394 fennec::memcpy(res.data() +
size(), cstr, cstr.size());
399 if (_str ==
nullptr) {
402 if (str.data() ==
nullptr) {
406 res._str.callocate(
size() + str.size() + 1);
407 fennec::memcpy(res.data(), _str,
size());
408 fennec::memcpy(res.data() +
size(), str.data(), str.size());
412 constexpr _string& operator+=(
char c) {
413 if (_str ==
nullptr) {
419 _str[
size() - 1] = c;
423 constexpr _string& operator+=(
const cstring& cstr) {
424 if (_str ==
nullptr) {
427 size_t middle =
size();
429 fennec::memcpy(_str + middle, cstr, cstr.size());
434 if (_str ==
nullptr) {
437 if (str.data() ==
nullptr) {
440 size_t middle =
size();
442 fennec::memcpy(_str + middle, str.data(), str.size());
449 constexpr char* begin() {
453 constexpr const char* begin()
const {
457 constexpr char* end() {
461 constexpr const char* end()
const {
471struct hash<string> : hash<byte_array> {
472 constexpr size_t operator()(
const string& str)
const {
473 return hash<byte_array>::operator()(byte_array(str.data(), str.size()));
This header contains structures and classes related to allocating blocks of memory.
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 header contains functions related to analyzing, modifying or copying buffers interpreted as byte...
constexpr char & operator[](size_t i)
Array Access Operator.
Definition string.h:170
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 string.h:277
constexpr size_t find(const string &str, size_t i=0) const
Finds the index of the first occurrence of str in the string.
Definition string.h:264
constexpr _string(const _string &str)=default
String Copy Constructor.
constexpr size_t rfind(const string &str, size_t i=npos) const
Finds the index of the last occurrence of str in the string.
Definition string.h:330
constexpr bool contains(char c, size_t i=0) const
Check if the string contains a character.
Definition string.h:243
constexpr _string(const char(&str)[n])
Buffer Constructor, wraps the provided C-Style string, appending a null-terminator if not present.
Definition string.h:91
constexpr _string(_string &&str) noexcept=default
String Move Constructor.
constexpr const char & operator[](size_t i) const
Const-Array Access Operator.
Definition string.h:178
constexpr _string()
Default Constructor, initializes empty string.
Definition string.h:55
constexpr size_t rfind(char c, size_t i=npos) const
Finds the index of the last occurrence of c in the string.
Definition string.h:291
constexpr _string(size_t n, char c='\0')
Sized Constructor, initializes a null-terminated string of size n with ‘'c’...`.
Definition string.h:65
constexpr size_t length() const
Definition string.h:198
constexpr ~_string()=default
Destructor, cleans up underlying allocation.
constexpr size_t size() const
Definition string.h:150
constexpr _string substring(size_t i, size_t n=npos) const
Retrieve a substring of a string.
Definition string.h:351
constexpr size_t capacity() const
Definition string.h:156
constexpr size_t find(char c, size_t i=0) const
Finds the index of the first occurrence of c in the string.
Definition string.h:251
constexpr _string(const char *buf, size_t n)
Buffer Constructor, wraps the provided C-Style string, appending a null-terminator if not present.
Definition string.h:103
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 string.h:309
constexpr int compare(const string &str, size_t i=0, size_t n=npos) const
String Comparison.
Definition string.h:221
constexpr int compare(const cstring &str, size_t i=0, size_t n=npos) const
String Comparison.
Definition string.h:207
constexpr void callocate(size_t n, align_t align=zero< align_t >()) noexcept
Allocate a block of memory for the allocation. If there is already an allocated block of memory,...
Definition allocator.h:498
constexpr value_t * data()
Getter for the real pointer to the allocated block of memory.
Definition allocator.h:624
constexpr size_t capacity() const
Getter for the number of elements n of type T that the allocation can hold.
Definition allocator.h:617
constexpr void creallocate(size_t n, align_t align=zero< align_t >()) noexcept
Reallocate the block with a new size. Contents are copied to the new allocation.
Definition allocator.h:541
This struct wraps c-style strings.
Definition cstring.h:64
constexpr size_t capacity() const
Definition cstring.h:193
constexpr size_t size() const
Definition cstring.h:187