6#include <initializer_list>
14#include "charFunctions.hpp"
18#include "arrayLike.hpp"
21#include "stringIterator.hpp"
24#if __has_include(<cereal/cereal.hpp>)
25#include <cereal/types/string.hpp>
61template <encoding E = utf8>
73 void initChar(uint32_t,
int)
noexcept;
74 int charSize()
const noexcept;
76 void initInt(
long long,
int,
int)
noexcept;
77 void initFloat(
double,
int,
int,
bool,
int)
noexcept;
78 void initPointer(
void *)
noexcept;
79 void initComplex(
const std::complex<double> &,
int,
int,
bool,
int)
noexcept;
92 explicit
string(
char chr) noexcept;
101 explicit
string(
wchar_t chr) noexcept;
112 string(const uint32_t &chr) noexcept;
121 string(const
char *
str) noexcept;
131 string(const
char *
str,
size_t len) noexcept;
133#if __cplusplus >= 202002L
135 string(
const char8_t *
const str,
size_t len) noexcept :
string((
const char *)
str, len) {}
181 template <typename INT, typename = typename std::enable_if<std::is_integral<INT>::value && !std::is_same<INT, char>::value && !std::is_same<INT, wchar_t>::value, INT>
::type>
182 string(INT value,
int base = 10,
int padSize = 0) noexcept {
183 this->initInt((
long long)value, base, padSize);
186#if __cplusplus < 202002L
197 template <typename PTR, typename = typename std::enable_if<std::is_pointer<PTR>::value && !std::is_same<PTR, char *>::value && !std::is_same<PTR, wchar_t *>::value, PTR>
::type>
199 this->initPointer((
void *)pointer);
212 template <typename PTR, typename = typename std::enable_if<std::is_pointer<PTR>::value && !std::is_same<PTR, char *>::value && !std::is_same<PTR, wchar_t *>::value && !std::is_same<PTR, char8_t *>::value, PTR>
::type>
213 string(PTR pointer)
noexcept {
214 this->initPointer((
void *)pointer);
237 template <typename FLT, typename = typename std::enable_if<std::is_floating_point<FLT>::value, FLT>
::type>
238 string(FLT value,
int base = 10,
int precision = 0,
bool scientific =
true,
int padSize = 0) noexcept {
239 this->initFloat((
double)value, base,
precision, scientific, padSize);
259 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>
::type>
260 string(
const std::complex<T> &value,
int base = 10,
int precision = 0,
bool scientific =
true,
int padSize = 0) noexcept {
261 this->initComplex((std::complex<double>)value, base,
precision, scientific, padSize);
313 data_len = other.data_len;
314 character_ct = other.character_ct;
327 if (&other !=
this) {
330 data_len = other.data_len;
331 character_ct = other.character_ct;
371 uint32_t
at(
int index)
const noexcept override;
378 size_t size() const noexcept override;
453 long integer(
int base = 10, uint32_t decimal =
'.') const noexcept;
459 template <typename INT, typename = typename std::enable_if<std::is_integral<INT>::value, INT>::
type>
460 explicit operator INT() const noexcept {
475 double floating(
int base = 10, uint32_t decimal =
'.') const noexcept;
481 explicit operator
double() const noexcept {
489 explicit operator float() const noexcept {
497 constexpr explicit operator bool() const noexcept {
512 std::complex<double>
complex(
int base = 10, uint32_t decimal =
'.') const noexcept;
522 int count(const
string &other) const noexcept;
537 int find(const
string &other,
int occurrence = 1) const noexcept {
538 return this->
findAfter(other, 0, occurrence);
553 int found(
const string &other,
int occurrence = 1) const noexcept {
554 return this->
findAfter(other, 0, occurrence) >= 0;
570 int findLast(
const string &other,
int occurrence = 1) const noexcept {
571 return this->
findBefore(other, this->character_ct, occurrence);
588 int findAfter(
const string &other,
int index,
int occurrence = 1) const noexcept {
590 for (
int i = index; i < character_ct; i++) {
613 int findBefore(
const string &other,
int index,
int occurrence = 1) const noexcept;
624 bool foundAt(const
string &other,
int index) const noexcept;
635 bool foundEndAt(const
string &other,
int index) const noexcept;
646 return this->
foundAt(other, 0);
657 bool endsWith(
const string &other)
const noexcept {
658 return this->
foundEndAt(other, character_ct - 1);
675 return type(base) & (zstr::integer);
694 bool isFloating(
int base = 10, uint32_t decimal =
'.') const noexcept {
695 return type(base, decimal) & (zstr::integer | zstr::floating);
714 bool isComplex(
int base = 10, uint32_t decimal =
'.') const noexcept {
715 return type(base, decimal) & (zstr::integer | zstr::floating | zstr::complex);
731 int type(
int base = 10, uint32_t decimal =
'.') const noexcept;
755 inline
string substr(
int index) const noexcept {
756 return substr(index, character_ct - index);
768 string &
append(
const string &other)
noexcept {
798 string &
insert(
const string &other,
int index)
noexcept;
813 string &
remove(
const string &other,
int occurrence = 0) noexcept;
855 string &
replace(const
string &findStr, const
string &replStr,
int occurrence = 0) noexcept;
884 string padLeft(const
string &other,
int padSize) const noexcept {
885 string newString = *
this;
886 return newString.
padLeftIn(other, padSize);
902 string &
padLeftIn(
const string &other,
int padSize)
noexcept;
917 string padRight(
const string &other,
int padSize)
const noexcept {
918 string newString = *
this;
935 string &
padRightIn(
const string &other,
int padSize)
noexcept;
947 for (
int i = 0; i <
count; ++i) {
963 string trimLeft(
const string &other =
"") const noexcept {
964 if ((character_ct < other.character_ct) || !character_ct) {
969 if (other.length()) {
970 while (
foundAt(other, index)) {
971 index += other.character_ct;
979 return substr(index, character_ct - index);
992 string trimRight(
const string &other =
"") const noexcept {
993 if ((character_ct < other.character_ct) || !character_ct) {
997 int index = character_ct - 1;
998 if (other.length()) {
1000 index -= other.character_ct;
1008 return substr(0, index + 1);
1022 if ((character_ct < other.character_ct) || !character_ct) {
1027 if (other.length()) {
1028 while (
foundAt(other, index)) {
1029 index += other.character_ct;
1051 if ((character_ct < other.character_ct) || !character_ct) {
1055 int index = character_ct - 1;
1056 if (other.length()) {
1058 index -= other.character_ct;
1066 return remove(index + 1, character_ct);
1079 string trim(
const string &other =
"") const noexcept {
1081 return result.trimRightIn(other);
1094 string &
trimIn(
const string &other =
"") noexcept {
1124 return string(*this).toUpper();
1188 string filter(uint32_t first, uint32_t last,
bool invert = false) const noexcept {
1189 return filter({first, last}, invert);
1203 string filter(
const std::pair<uint32_t, uint32_t> &
range,
bool invert =
false) const noexcept {
1218 string filter(
const std::initializer_list<
const std::pair<uint32_t, uint32_t>> &list,
bool invert =
false) const noexcept;
1230 string filter(const
string &list,
bool invert = false) const noexcept;
1242 string filter(std::function<
bool(uint32_t)> lambda) const noexcept;
1257 bool contains(uint32_t first, uint32_t last,
bool exclusive = false) const noexcept {
1258 return contains({first, last}, exclusive);
1273 bool contains(
const std::pair<uint32_t, uint32_t> &
range,
bool exclusive =
false) const noexcept {
1289 bool contains(
const std::initializer_list<
const std::pair<uint32_t, uint32_t>> &list,
bool exclusive =
false) const noexcept;
1302 bool contains(const
string &list,
bool exclusive = false) const noexcept;
1315 string cipher(const
string &keys, const
string &values) const noexcept;
1328 string cipher(std::function<uint32_t(uint32_t)> lambda) const noexcept;
1371 static string numberFormat(
double value,
int precision = 0,
bool forcePrecision =
false,
char decimal =
'.',
char thousands =
',') noexcept;
1385 string concat(const
string &other) const noexcept {
1386 return *
this + other;
1496 string &
read(std::istream &stream, uint32_t delim = 0) noexcept;
1508 string &
readln(std::istream &stream) noexcept;
1527 void write(std::ostream &stream) const noexcept {
1528 stream << *
this << std::flush;
1538 void writeln(std::ostream &stream)
const noexcept {
1539 stream << *
this << std::endl;
1567#if __has_include(<cereal/cereal.hpp>)
1574 template <
class archive>
1575 std::string save_minimal(archive &ar)
const {
1585 template <
class archive>
1586 void load_minimal(archive
const &ar, std::string
const &value) {
1600 return ostr << string<utf8>(
str).cstring();
1623 inline std::string
str() const noexcept {
1636typedef z::
core::
string<> zstring;
1637typedef z::
core::
string<z::utf8> zpath;
1640z::
core::
string<z::utf32> operator
""_u32(
char value);
1641z::
core::
string<z::utf32> operator
""_u32(
wchar_t value);
1642z::
core::
string<z::utf32> operator
""_u32(const
char *value);
1643z::
core::
string<z::utf32> operator
""_u32(const
char *value,
size_t);
1644z::
core::
string<z::utf32> operator
""_u32(const
wchar_t *value,
size_t);
1646z::
core::
string<z::utf16> operator
""_u16(
char value);
1647z::
core::
string<z::utf16> operator
""_u16(
wchar_t value);
1648z::
core::
string<z::utf16> operator
""_u16(const
char *value);
1649z::
core::
string<z::utf16> operator
""_u16(const
char *value,
size_t);
1650z::
core::
string<z::utf16> operator
""_u16(const
wchar_t *value,
size_t);
1652z::
core::
string<z::utf8> operator
""_u8(
char value);
1653z::
core::
string<z::utf8> operator
""_u8(
wchar_t value);
1654z::
core::
string<z::utf8> operator
""_u8(const
char *value);
1655z::
core::
string<z::utf8> operator
""_u8(const
char *value,
size_t);
1656z::
core::
string<z::utf8> operator
""_u8(const
wchar_t *value,
size_t);
1658z::
core::
string<z::ascii> operator
""_asc(
char value);
1659z::
core::
string<z::ascii> operator
""_asc(
wchar_t value);
1660z::
core::
string<z::ascii> operator
""_asc(const
char *value);
1661z::
core::
string<z::ascii> operator
""_asc(const
char *value,
size_t);
1662z::
core::
string<z::ascii> operator
""_asc(const
wchar_t *value,
size_t);
1664zstring operator
""_zs(
char value);
1665zstring operator
""_zs(
wchar_t value);
1666zstring operator
""_zs(const
char *value);
1667zstring operator
""_zs(const
char *value,
size_t);
1668zstring operator
""_zs(const
wchar_t *value,
size_t);
An interface for all objects that can be both iterated over and directly indexed.
Definition arrayLike.hpp:13
An interface for getting an object's size.
Definition sizable.hpp:13
A class for iterating over each character in a string.
Definition stringIterator.hpp:17
A template class for character strings.
Definition string.hpp:62
bool isInteger(int base=10) const noexcept
Check if this string can convert to an integer.
Definition string.hpp:674
string(const std::complex< T > &value, int base=10, int precision=0, bool scientific=true, int padSize=0) noexcept
Construct from complex number.
Definition string.hpp:260
static string words(long long value, bool ordinal=false) noexcept
string(const std::string &str) noexcept
Construct from a std::string.
Definition string.hpp:165
string & trimRightIn(const string &other="") noexcept
Remove padding from the right side of this string.
Definition string.hpp:1050
string & toLower() noexcept
Convert all characters in the string to lowercase.
int count(const string &other) const noexcept
string & cutDuplicates(const string &other) noexcept
int find(const string &other, int occurrence=1) const noexcept
Definition string.hpp:537
string & padRightIn(const string &other, int padSize) noexcept
Right-pad this string up to a given character count.
size_t size() const noexcept override
Get the size of the string in memory.
bool operator!=(const string &other) const noexcept
Inequality comparison.
Definition string.hpp:1440
string padLeft(const string &other, int padSize) const noexcept
Definition string.hpp:884
std::string str() const noexcept
Definition string.hpp:1623
string(INT value, int base=10, int padSize=0) noexcept
Construct from an integer.
Definition string.hpp:182
bool operator>=(const string &other) const noexcept
Greater-than or equal comparison.
Definition string.hpp:1460
string operator+(const string &other) const noexcept
Concatenate two strings.
string(const string< ascii > &other) noexcept
Construct from an ASCII string.
string & toCamel() noexcept
Convert all characters in the string to camelcase.
bool foundEndAt(const string &other, int index) const noexcept
int length() const noexcept override
friend std::ostream & operator<<(std::ostream &ostr, const z::core::string< E > &str)
Stream output operator.
Definition string.hpp:1599
string trim(const string &other="") const noexcept
Copies this string and removes padding from both sides of the result.
Definition string.hpp:1079
static string numberFormat(double value, int precision=0, bool forcePrecision=false, char decimal='.', char thousands=',') noexcept
Create a string from a number with thousands separators.
string & replace(const string &findStr, const string &replStr, int occurrence=0) noexcept
string filter(const std::pair< uint32_t, uint32_t > &range, bool invert=false) const noexcept
Filter out all characters not in the given range.
Definition string.hpp:1203
string trimLeft(const string &other="") const noexcept
Copies this string and removes padding from the left side of the result.
Definition string.hpp:963
double floating(int base=10, uint32_t decimal='.') const noexcept
Convert this string to a floating-point value.
bool endsWith(const string &other) const noexcept
Check if the string ends with a given sub-string.
Definition string.hpp:657
string padRight(const string &other, int padSize) const noexcept
Copy this string, right-padded given character count.
Definition string.hpp:917
long integer(int base=10, uint32_t decimal='.') const noexcept
Convert this string to an integer.
string filter(uint32_t first, uint32_t last, bool invert=false) const noexcept
Definition string.hpp:1188
const uint16_t * nstring() const noexcept
bool contains(const std::pair< uint32_t, uint32_t > &range, bool exclusive=false) const noexcept
Check if this string contains any characters in the given range.
Definition string.hpp:1273
uint32_t at(int index) const noexcept override
Get the character at the given index.
string & readln(std::istream &stream) noexcept
stringIterator< E > end() const noexcept override
Get an iterator for the end of the string.
Definition string.hpp:1562
string camel() const noexcept
Get a camelcase version of this string.
Definition string.hpp:1145
int findLast(const string &other, int occurrence=1) const noexcept
Reverse-find a specific occurrence of a sub-string.
Definition string.hpp:570
static string ordinal(long long value) noexcept
bool operator>(const string &other) const noexcept
Greater-than comparison.
string(const string< utf32 > &other) noexcept
Construct from a UTF32 string.
const uint32_t * wstring() const noexcept
string & operator=(const string &other) noexcept
Assign the contents of this string.
string lower() const noexcept
Get a lowercase version of this string.
Definition string.hpp:1134
string & append(const string &other) noexcept
Append another string to the end of this one.
Definition string.hpp:768
string & remove(const string &other, int occurrence=0) noexcept
Remove occurrences of the given sub-string.
const char * cstring() const noexcept
friend std::istream & operator>>(std::istream &istr, z::core::string< E > &str)
Stream input operator.
Definition string.hpp:1609
~string() noexcept
Destructor.
Definition string.hpp:339
bool contains(uint32_t first, uint32_t last, bool exclusive=false) const noexcept
Definition string.hpp:1257
void write(std::ostream &stream) const noexcept
Definition string.hpp:1527
bool isComplex(int base=10, uint32_t decimal='.') const noexcept
Check if this string can convert to a complex value.
Definition string.hpp:714
string cipher(const string &keys, const string &values) const noexcept
string() noexcept
Default string constructor.
string(const string< utf16 > &other) noexcept
Construct from a UTF16 string.
string & operator+=(const string &other) noexcept
Append another string to the end of this one.
string(const wchar_t *str, size_t len) noexcept
Construct from a cstring of wide characters, with a known length.
string & trimIn(const string &other="") noexcept
Remove padding from the both sides of this string.
Definition string.hpp:1094
int type(int base=10, uint32_t decimal='.') const noexcept
Determine the most basic type that this string can cast to.
string(FLT value, int base=10, int precision=0, bool scientific=true, int padSize=0) noexcept
Construct from floating-point.
Definition string.hpp:238
string(const string< utf8 > &other) noexcept
Construct from a UTF-8 string.
string concat(const string &other) const noexcept
Definition string.hpp:1385
string & append(uint32_t chr) noexcept
Append a single character to the end of the string.
string & readall(std::istream &stream) noexcept
int findAfter(const string &other, int index, int occurrence=1) const noexcept
Find a specific occurrence of a sub-string.
Definition string.hpp:588
stringIterator< E > begin() const noexcept override
Get an iterator for the beginning of the string.
Definition string.hpp:1550
string filter(const std::initializer_list< const std::pair< uint32_t, uint32_t > > &list, bool invert=false) const noexcept
Filter out all characters not in the given range.
string trimRight(const string &other="") const noexcept
Copies this string and removes padding from the right side of the result.
Definition string.hpp:992
int found(const string &other, int occurrence=1) const noexcept
Check if a specific occurrence of a sub-string exists.
Definition string.hpp:553
int findBefore(const string &other, int index, int occurrence=1) const noexcept
Reverse-find a specific occurrence of a sub-string.
string substr(int index, int count) const noexcept
std::complex< double > complex(int base=10, uint32_t decimal='.') const noexcept
Convert this string to a complex value.
bool operator<(const string &other) const noexcept
Less-than comparison.
string repeat(int count) const noexcept
Repeat this string a specific number of times.
Definition string.hpp:944
string & operator=(string &&other) noexcept
Move assignment operator.
Definition string.hpp:326
bool contains(const std::initializer_list< const std::pair< uint32_t, uint32_t > > &list, bool exclusive=false) const noexcept
Check if this string contains any characters in the given ranges.
void clear() noexcept
Reset to a null string.
string & toUpper() noexcept
Convert all characters in the string to uppercase.
string(PTR pointer) noexcept
Construct from a pointer.
Definition string.hpp:198
bool beginsWith(const string &other) const noexcept
Definition string.hpp:645
string upper() const noexcept
Definition string.hpp:1123
void increase(int charCount) noexcept
Increase the space allocated for this string.
string & trimLeftIn(const string &other="") noexcept
Remove padding from the left side of this string.
Definition string.hpp:1021
bool operator<=(const string &other) const noexcept
Less-than or equal comparison.
Definition string.hpp:1480
string & read(std::istream &stream, uint32_t delim=0) noexcept
Read string data from a stream until the given delimiter is encountered.
string & truncate(int index) noexcept
bool foundAt(const string &other, int index) const noexcept
Check if a sub-string is found at the given index.
int chars() const noexcept
static string precision(double value, int precision, bool forcePrecision=false) noexcept
Definition string.hpp:1357
string(string &&other) noexcept
Move constructor.
Definition string.hpp:311
bool operator==(const string &other) const noexcept
Equality comparison.
string(const wchar_t *str) noexcept
Construct from a cstring of wide characters.
string & insert(const string &other, int index) noexcept
Insert another string into this one.
constexpr encoding format() const noexcept
Definition string.hpp:435
void writeln(std::ostream &stream) const noexcept
Write string data to a stream in its format, appending a newline.
Definition string.hpp:1538
string & padLeftIn(const string &other, int padSize) noexcept
Left-pad this string up to a given character count.
hash32 hash() const noexcept
Compute the CRC32 hash of this string.
bool isFloating(int base=10, uint32_t decimal='.') const noexcept
Check if this string can convert to a floating-point value.
Definition string.hpp:694
encoding
Supported string encoding schemes.
Definition encoding.hpp:12
Section containing core functions and classes.
bool isWhiteSpace(uint32_t ch) noexcept
Check if the given character is white space.
generator< long, long > range(long begin, long end, long step=1) noexcept
Generate a sequence of integers in a specified range.