22 #ifndef CRNSTRING_Header
23 #define CRNSTRING_Header
45 inline wint_t
ToWUpper(wint_t c) {
return towupper(c); }
47 inline wint_t
ToWLower(wint_t c) {
return towlower(c); }
69 virtual ~String()
override =
default;
71 String(
const std::u32string &s):data(s) {}
73 String(std::u32string &&s) noexcept:data(std::move(s)) {}
75 String(char32_t *s) {
if (s) data = s;
else data = U
""; }
77 String(
const char32_t *s) {
if (s) data = s;
else data = U
""; }
81 String(char32_t c,
size_t n = 1);
91 String(
const std::string &s);
93 String(
char c,
size_t n = 1);
97 String(
unsigned int i) { convertFrom(i); }
101 String(
unsigned long i) { convertFrom(i); }
105 String(
double d) { convertFrom(
double(d)); }
107 String(
long double d) { convertFrom(d); }
111 String(
unsigned long long i) { convertFrom(i); }
113 template<
typename T>
String(
const std::complex<T> &c) { *
this =
String(U
"(") + c.real() +
String(U
", ") + c.imag() +
String(U
")"); }
122 std::u32string&
Std() & noexcept {
return data; }
124 const std::u32string&
Std() const & noexcept {
return data; }
126 std::u32string
Std() && {
return std::move(data); }
128 const char32_t*
CWStr() const noexcept {
return data.c_str(); }
130 const char*
CStr()
const;
134 int ToInt()
const {
return convertTo<int>(); }
136 unsigned int ToUInt()
const {
return convertTo<unsigned int>(); }
138 long ToLong()
const {
return convertTo<long>(); }
140 unsigned long ToULong()
const {
return convertTo<unsigned long>(); }
142 float ToFloat()
const {
return convertTo<float>(); }
144 double ToDouble()
const {
return convertTo<double>(); }
148 long long ToLongLong()
const {
return convertTo<long long>(); }
150 unsigned long long ToULongLong()
const {
return convertTo<unsigned long long>(); }
152 int64_t
ToInt64()
const {
return convertTo<int64_t>(); }
154 uint64_t
ToUInt64()
const {
return convertTo<uint64_t>(); }
160 size_t Size() const noexcept {
return data.length(); }
162 size_t Length() const noexcept {
return data.length(); }
164 bool operator!() const noexcept {
return data.empty(); }
166 bool IsEmpty() const noexcept {
return data.empty(); }
173 const char32_t&
operator[](
size_t index)
const;
199 std::vector<String>
WhichSuffixes(
const std::vector<String> &suffixes)
const;
201 std::vector<String>
WhichPrefixes(
const std::vector<String> &prefixes)
const;
203 std::vector<String>
WhichSubstrings(
const std::vector<String> &substrings)
const;
220 size_t Find(
const String &s,
size_t from_pos = 0)
const;
239 bool EndsWith(
const std::vector<String> &suffixes)
const;
242 std::vector<String>
Split(
const String &sep)
const;
257 static size_t NPos() noexcept;
265 xml::Element
Serialize(xml::Element &parent) const;
269 template<typename T> T convertTo()
const { std::stringstream ss(
CStr()); T val; ss >> val;
return val; }
271 template<
typename T>
void convertFrom(T val) { std::stringstream ss; ss << std::setprecision(
Precision()) << val; *
this = ss.str(); }
274 mutable std::string cdata;
279 template<> struct
IsClonable<String> : public std::true_type {};
313 inline String operator"" _s(
const char32_t *str,
size_t len)
315 return String{std::u32string{str, len}};
328 template<>
struct hash<crn::String>
336 template<
typename traits>
inline std::basic_ostream<char, traits>& operator<<(std::basic_ostream<char, traits> &strm,
const crn::String &s)
wint_t ToWLower(wint_t c)
Transforms a character to its lower case.
String(long i)
Constructor from an long.
int64_t ToInt64() const
Conversion to int64_t.
String(int i)
Constructor from an int.
String(const char32_t *s)
Constructor from a wide cstring.
#define CRN_SERIALIZATION_CONSTRUCTOR(classname)
Defines a default constructor from xml element.
size_t BackwardFind(const String &s, size_t last_pos=NPos()) const
Finds the last occurrence of a string.
bool operator>(const String &s1, const String &s2)
Compares two strings.
wint_t ToWUpper(wint_t c)
Transforms a character to its upper case.
std::vector< String > Split(const String &sep) const
Splits the string in multiple strings delimited by a set of separators.
size_t Size() const noexcept
Returns the length of the string.
String(long long i)
Constructor from an long.
float ToFloat() const
Conversion to float.
bool operator>=(const String &s1, const String &s2)
Compares two strings.
bool operator<=(const String &s1, const String &s2)
Compares two strings.
bool StartsWith(const String &s) const
Check if string has a given prefix.
unsigned int ToUInt() const
Conversion to unsigned int.
const char * CStr() const
Conversion to UTF8 cstring.
std::u32string Std()&&
Conversion to std u32string.
String(unsigned long long i)
Constructor from an unsigned long.
double ToDouble() const
Conversion to double.
size_t BackwardFindNotOf(const String &s, size_t from_pos=NPos()) const
Finds the last occurrence of character not in a list.
bool IsEmpty() const noexcept
Checks if the string is empty.
size_t Find(const String &s, size_t from_pos=0) const
Finds the first occurrence of a string.
String()=default
Default constructor (empty string)
const std::u32string & Std() const &noexcept
Conversion to std u32string.
String(unsigned int i)
Constructor from an unsigned int.
A UTF32 character string class.
long double ToLongDouble() const
Conversion to long double.
void Deserialize(xml::Element &el)
Initializes the object from an XML element. Unsafe.
static int & Precision() noexcept
Precision of the floating point conversion.
String(const String &s)
Copy constructor.
String & operator=(String &&)=default
void ShrinkToFit()
Optimizes the memory usage.
void Swap(String &str) noexcept
Swaps two strings.
String & ReplaceSuffix(const String &old_suffix, const String &new_suffix)
Replaces suffix by another pattern if present.
virtual ~String() override=default
String SubString(size_t pos, size_t n=0) const
Extracts a part of the string.
char32_t & operator[](size_t index)
Access to a character.
std::vector< String > WhichSuffixes(const std::vector< String > &suffixes) const
Returns suffixes found within a collection.
String(float f)
Constructor from a float.
String(const std::complex< T > &c)
Constructor from a complex.
String & FirstCharacterToUpper()
Converts the first character of string to uppercase.
BoolNotBoolDummy operator+(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
static String CreateUniqueId(size_t len=8)
Generates an almost unique id.
bool operator!=(const String &s1, const String &s2)
Tests inequality of two strings.
String & operator=(const String &s)
Copies from another string.
size_t Length() const noexcept
Returns the length of the string.
size_t operator()(const crn::String &s) const
int ToInt() const
Conversion to int.
Prop3 ToProp3() const
Conversion to Prop3.
String(const std::u32string &s)
Constructor from a std wide string.
double Distance(const Int &i1, const Int &i2) noexcept
String(unsigned long i)
Constructor from an unsigned long.
unsigned long long ToULongLong() const
Conversion to unsigned long long.
size_t BackwardFindAnyOf(const String &s, size_t from_pos=NPos()) const
Finds the last occurrence of character in a list.
String(char32_t *s)
Constructor from a wide cstring.
String(long double d)
Constructor from a long double.
bool operator!() const noexcept
Checks if the string is empty.
std::vector< String > WhichPrefixes(const std::vector< String > &prefixes) const
Returns prefixes found within a collection.
long long ToLongLong() const
Conversion to long long.
size_t FindAnyOf(const String &s, size_t from_pos=0) const
Finds the first occurrence of character in a list.
String & ToLower()
Converts the string to lowercase.
bool EndsWith(const String &s) const
Check if string has a given suffix.
String(std::u32string &&s) noexcept
Constructor from a std wide string.
std::vector< String > WhichSubstrings(const std::vector< String > &substrings) const
Returns substring found within a collection.
xml::Element Serialize(xml::Element &parent) const
Dumps the object to an XML element. Unsafe.
const char32_t * CWStr() const noexcept
Conversion to wide cstring.
String & Replace(const String &s, size_t pos, size_t n=0)
Replaces a part of the string.
long ToLong() const
Conversion to long.
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
String & operator+=(const String &s)
Appends a string.
A character string class.
bool operator==(const String &s1, const String &s2)
Tests equality of two strings.
unsigned long ToULong() const
Conversion to unsigned long.
int EditDistance(const String &s) const
Edit distance.
String & Erase(size_t pos, size_t n=0)
Erases a part of the string.
uint64_t ToUInt64() const
Conversion to uint64_t.
bool operator<(const String &s1, const String &s2)
Compares two strings.
String & ToUpper()
Converts the string to uppercase.
bool IsNotEmpty() const noexcept
Checks if the string is not empty.
std::u32string & Std()&noexcept
Conversion to std u32string.
String & Insert(size_t pos, const String &s)
Inserts a string.
static size_t NPos() noexcept
Last position in a string.
String & DeleteSuffix(const String &suffix)
Delete suffix if found.
size_t FindNotOf(const String &s, size_t from_pos=0) const
Finds the first occurrence of character not in a list.
String & Crop(size_t pos, size_t n=0)
Crops the string.
String(double d)
Constructor from a double.