19#ifndef FENNEC_STRING_WSTRING_H
20#define FENNEC_STRING_WSTRING_H
22#include <fennec/string/detail/_ctype.h>
23#include <fennec/string/wcstring.h>
36template<
typename AllocT = allocator<
wchar_t>>
struct _wstring;
39using wstring = _wstring<>;
45template<
typename AllocT>
49 static constexpr size_t npos = -1;
50 using char_t = wchar_t;
70 fennec::wmemset(_str, c, n);
73 constexpr _wstring(
const alloc_t& alloc)
77 constexpr _wstring(
size_t n,
wchar_t c,
const alloc_t& alloc)
78 : _str(n + 1, alloc) {
79 fennec::wmemset(_str, c, n);
82 constexpr _wstring(
const wcstring& cstr)
83 : _str(cstr, cstr.
size() + 1) {
91 explicit constexpr _wstring(
const wchar_t (&str)[n])
92 : _str(str[n - 1] !=
'\0' ? n + 1 : n) {
93 fennec::wmemcpy(_str, str, n);
94 if (str[n - 1] !=
'\0') {
104 : _str(buf[n - 1] !=
'\0' ? n + 1 : n) {
105 fennec::wmemcpy(_str, buf, n);
106 if (buf[n - 1] !=
'\0') {
129 fennec::wmemcpy(_str, cstr, cstr.
capacity());
140 constexpr size_t size()
const {
150 constexpr bool empty()
const {
172 constexpr wchar_t* data() {
176 constexpr const wchar_t* data()
const {
180 constexpr const wchar_t* cstr()
const {
203 return ::wcsncmp(_str + i, str, n);
217 return ::wcsncmp(_str + i, str.data(), n);
220 constexpr bool operator==(
const wcstring& str)
const {
224 constexpr bool operator==(
const _wstring& str)
const {
233 constexpr bool contains(
char c,
size_t i = 0)
const {
241 constexpr size_t find(
wchar_t c,
size_t i = 0)
const {
246 const wchar_t* loc = ::wcschr(_str + i, c);
247 return loc ? loc - _str :
size();
259 const wchar_t* loc = ::wcsstr(_str, str);
260 return loc ? loc - _str :
size();
272 const wchar_t* loc = ::wcsstr(_str + i, str);
273 return loc ? loc - _str :
size();
281 constexpr size_t rfind(
wchar_t c,
size_t i = npos)
const {
303 const wchar_t first = str[0];
306 if(_str[i] == first) {
320 constexpr size_t rfind(
const string& str,
size_t i = npos)
const {
324 const wchar_t first = str[0];
327 if(_str[i] == first) {
348 fennec::wmemcpy(res.data(), _str + i, n);
356 constexpr void resize(
size_t n) {
361 constexpr _wstring operator+(
wchar_t c)
const {
362 if (_str ==
nullptr) {
367 fennec::wmemcpy(res.data(), _str,
size());
377 constexpr _wstring operator+(
const wcstring& cstr)
const {
378 if (_str ==
nullptr) {
382 res._str.callocate(
size() + cstr.size() + 1);
383 fennec::wmemcpy(res.data(), _str,
size());
384 fennec::wmemcpy(res.data() +
size(), cstr, cstr.size());
389 if (_str ==
nullptr) {
392 if (str.data() ==
nullptr) {
396 res._str.callocate(
size() + str.size() + 1);
397 fennec::wmemcpy(res.data(), _str,
size());
398 fennec::wmemcpy(res.data() +
size(), str.data(), str.size());
402 constexpr _wstring& operator+=(
wchar_t c) {
403 if (_str ==
nullptr) {
409 _str[
size() - 1] = c;
413 constexpr _wstring& operator+=(
const wcstring& cstr) {
414 if (_str ==
nullptr) {
417 size_t middle =
size();
419 fennec::wmemcpy(_str + middle, cstr, cstr.size());
424 if (_str ==
nullptr) {
427 if (str.data() ==
nullptr) {
430 size_t middle =
size();
432 fennec::wmemcpy(_str + middle, str.data(), str.size());
442struct hash<wstring> : hash<byte_array> {
443 constexpr size_t operator()(
const string& str)
const {
444 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 size_t size() const
Definition string.h:150
Struct for wrapping c-style strings.
Definition wstring.h:47
constexpr _wstring(const _wstring &str)=default
String Copy Constructor.
constexpr _wstring substring(size_t i, size_t n=npos) const
Retrieve a substring of a string.
Definition wstring.h:341
constexpr size_t find(wchar_t c, size_t i=0) const
Finds the index of the first occurrence of c in the string.
Definition wstring.h:241
constexpr size_t find(const wcstring &str, size_t i=0) const
Finds the index of the first occurrence of str in the string.
Definition wstring.h:267
constexpr int compare(const _wstring &str, size_t i=0, size_t n=npos) const
String Comparison.
Definition wstring.h:211
constexpr ~_wstring()=default
Destructor, cleans up underlying allocation.
constexpr bool contains(char c, size_t i=0) const
Check if the string contains a character.
Definition wstring.h:233
constexpr const wchar_t & operator[](size_t i) const
Const-Array Access Operator.
Definition wstring.h:168
constexpr _wstring(const wchar_t(&str)[n])
Buffer Constructor, wraps the provided C-Style string, appending a null-terminator if not present.
Definition wstring.h:91
constexpr wchar_t & operator[](size_t i)
Array Access Operator.
Definition wstring.h:160
constexpr size_t find(const _wstring &str, size_t i=0) const
Finds the index of the first occurrence of str in the string.
Definition wstring.h:254
constexpr size_t length() const
Definition wstring.h:188
constexpr _wstring(_wstring &&str) noexcept=default
String Move Constructor.
constexpr _wstring(size_t n, wchar_t c='\0')
Sized Constructor, initializes a null-terminated string of size n with ‘'c’...`.
Definition wstring.h:68
constexpr size_t size() const
Definition wstring.h:140
constexpr size_t capacity() const
Definition wstring.h:146
constexpr size_t rfind(const wcstring &str, size_t i=npos) const
Finds the index of the last occurrence of str in the string.
Definition wstring.h:299
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 wstring.h:320
constexpr _wstring(const wchar_t *buf, size_t n)
Buffer Constructor, wraps the provided C-Style string, appending a null-terminator if not present.
Definition wstring.h:103
constexpr _wstring()
Default Constructor, initializes empty string.
Definition wstring.h:58
constexpr int compare(const wcstring &str, size_t i=0, size_t n=npos) const
String Comparison.
Definition wstring.h:197
constexpr size_t rfind(wchar_t c, size_t i=npos) const
Finds the index of the last occurrence of c in the string.
Definition wstring.h:281
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 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 wcstring.h:60
constexpr size_t capacity() const
Definition wcstring.h:179
constexpr size_t size() const
Definition wcstring.h:175