-
Notifications
You must be signed in to change notification settings - Fork 2
The `utf::string` class API
- Member types
-
Public member functions
-
Constructors
string ()
string (string const&)
string (string&&)
string (const char*)
-
string (const char8_t*)
string (string_view const&)
string (string::char_type, string::size_type)
from_bytes (std::vector<string::unit> const&) -> string
from_file (Args&&...) -> string
from_std_string (std::string const&) -> string
from_unicode (std::initializer_list<string::char_type>) -> string
- Destructor
- Convertors
-
Transforming algorithms
clear () -> string&
erase (string::size_type, string::size_type) -> string&
erase (string_view const&) -> string&
erase (string_view::iterator const&) -> string&
insert (string::size_type, string::char_type) -> string&
insert (string::size_type, string_view const&) -> string&
insert (string_view::iterator const&, string::char_type) -> string&
insert (string_view::iterator const&, string_view const&) -> string&
pop () -> string::char_type
push (string::char_type) -> string&
push (string_view const&) -> string&
remove_if (Functor&&) -> string&
remove (Char...) -> string&
remove (View const&...) -> string&
replace_all_if (Functor&&, string::char_type) -> string&
replace_all (string::char_type, string::char_type) -> string&
replace_all (string_view const&, string_view const&) -> string&
replace (string::size_type, char_type) -> string&
replace (string::size_type, string::size_type, string_view const&) -> string&
replace (string_view const&, string_view const&) -> string&
replace (string_view::iterator const&, char_type) -> string&
reserve (string::size_type) -> string&
shrink_to_fit () -> string&
simplify () -> string&
split_off (string::size_type) -> string
to_ascii_lower () -> string&
to_ascii_upper () -> string&
transform (Functor&&) -> string&
trim_if (Functor&&) -> string&
trim () -> string&
trim (string::char_type) -> string&
- Properties
-
Operators
operator ! () -> bool
operator != (string_view const&) -> bool
operator < (string_view const&) -> bool
operator <= (string_view const&) -> bool
operator = (const char*) -> string&
operator = (string&&) -> string&
operator = (string const&) -> string&
operator == (string_view const&) -> bool
operator > (string_view const&) -> bool
operator >= (string_view const&) -> bool
-
Constructors
Name | Defined as |
---|---|
string::char_type |
uint32_t |
string::difference_type |
ptrdiff_t |
string::unit |
uint8_t |
string::pointer |
string::unit* |
string::size_type |
ptrdiff_t |
string::value_type |
Same as char_type |
string_view |
string::view |
Default constructor. Does not allocate any memory in the heap.
Copy constructor.
Move constructor.
Converting constructor from a C-string.
Converting constructor from an UTF-8 string literal. Requires C++20 or higher.
Converting constructor from a string view. The effect is equivalent to invoke view::to_string ()
.
Constructs a string as a copy of Ch
character N
times.
Converting constructor from a vector of UTF-8 bytes. For example,
utf::string::from_bytes({ 0b11000010, 0b10100101, '1', '0' })
is equal to "¥10"
.
Converting constructor from the contents of an std::ifstream
. The args
parameters pack is passing to the local filestream's constructor.
Converting constructor from an STL string object.
Converting constructor from a list of Unicode codepoints. For example,
utf::string::from_unicode({ 0xA5, '1', '0' });
is equal to "¥10"
.
Frees an allocated memory chunk.
Creates and returns an std::vector
object containing UTF-8-encoded bytes of the string.
Creates and returns an std::vector
object containing the Unicode codepoints of the string's characters.
Returns the copy of the original string. Useful for chaining, e.g.,
auto ModifiedCopy = StayOriginal.clone().insert(5, "substring");
Creates an iterable span over entire string.
This is an
O(1)
operation.
Creates an iterable span over a substring in range [shift; shift + N)
. If N > this->length() - shift
, takes all characters to a string's end.
⚠ Throws:
-
utf::invalid_argument
ifshift
is negative; -
utf::length_error
ifN
is negative.
Creates an iterable span over N
first characters of a string.
⚠ Throws utf::length_error
if N
is negative.
Creates an iterable span over N
last characters of a string.
⚠ Throws utf::length_error
if N
is negative.
Completely clears the string by deallocating its owned memory.
Returns *this
.
Removes N
characters starting from the given index pos
.
⚠ Throws:
-
utf::invalid_argument
ifpos
is negative; -
utf::length_error
ifN
is negative.
Returns *this
.
Removes all characters in the given range.
⚠ Throws utf::out_of_range
if vi
⊄ *this
.
Returns *this
.
Removes one character by an iterator iter
.
⚠ Throws utf::out_of_range
if iter
∉ *this
.
Returns *this
.
Inserts a character ucode
into the string before the pos
-th.
⚠ Throws:
-
utf::invalid_argument
ifpos
is negative; -
utf::unicode_error
in case ofucode
's invalid codepoint.
Returns *this
.
Inserts a substring into the string before the pos
-th.
⚠ Throws utf::invalid_argument
if pos
is negative.
Returns *this
.
Inserts a character ucode
into the string before the character pointing by iter
.
⚠ Throws:
-
utf::out_of_range
ifiter
∉*this
; -
utf::unicode_error
in case ofucode
's invalid codepoint.
Returns *this
.
Inserts a substring into the string before the character pointing by iter
.
⚠ Throws utf::out_of_range
if iter
∉ *this
.
Returns *this
.
Removes the last character from the string and returns its codepoint.
⚠ Throws utf::underflow_error
if *this
was empty before modifying.
Appends a given Unicode character to the end of the string.
⚠ Throws utf::unicode_error
in case of ucode
's invalid codepoint.
Returns *this
.
Appends a given substring to the end of current string.
Returns *this
.
Removes all characters satisfying specified criteria from the string.
Template constraints:
-
std::is_invocable_r<bool, Functor, string::char_type>
; -
std::predicate<Functor, string::char_type>
.
Returns *this
.
Removes all occurrences of the every character in the string.
Template constraints:
-
std::is_convertible<Char, string::char_type>...
; -
std::convertible_to<Char, string::char_type>...
.
⚠ Throws utf::unicode_error
in case of any character's invalid codepoint.
Returns *this
.
Removes all occurences of the every substring in the string.
Template constraints:
-
std::is_convertible<View, string_view>...
; -
std::convertible_to<View, string_view>...
.
Returns *this
.
Replaces all characters satisfying specified criteria by another one.
Template constraints:
-
std::is_invocable_r<bool, Functor, string::char_type>
; -
std::predicate<Functor, string::char_type>
.
⚠ Throws utf::unicode_error
in case of ucode
's invalid codepoint.
Returns *this
.
Replaces all occurences of the character what
by ucode
.
⚠ Throws utf::unicode_error
in case of ucode
's/what
's invalid codepoint.
Returns *this
.
Replaces all occurences of the substring vi
by other
.
Returns *this
.
Replaces the pos
-th character by ucode
.
⚠ Throws:
-
utf::invalid_argument
ifpos
is negative; -
utf::unicode_error
in case ofucode
's invalid codepoint.
Returns *this
.
Replaces N
characters starting from the given index pos
by other substring.
⚠ Throws:
-
utf::invalid_argument
ifpos
is negative; -
utf::length_error
ifN
is negative.
Returns *this
.
Replaces the characters in the given range by other substring.
⚠ Throws utf::out_of_range
if vi
⊄ *this
.
Returns *this
.
Replaces a character (by its iterator) by ucode
.
⚠ Throws:
-
utf::out_of_range
ifiter
∉*this
; -
utf::unicode_error
in case ofucode
's invalid codepoint.
Returns *this
.
Checks the current capacity of the string and reallocates if it's less than ordered. For example:
utf::string Str { "Errare humanum est" };
assert(Str.capacity() == 18);
assert(Str.reserve(50).capacity() == 50); // Good
//assert(Str.reserve(30).capacity() == 30); // No way!
assert(Str.reserve(30).capacity() == 50); // Same as before
Returns *this
.
Reallocates the memory buffer to make capacity() == size()
. For example:
utf::string Str { "Errare humanum est" };
Str.reserve(50); // Now, the capacity is 50
assert(Str.shrink_to_fit().capacity() == 18); // Same as before
Returns *this
.
Removes the whitespaces from the start and the end and replaces all sequences of internal whitespace with a single space.
Returns *this
.
Splits the string into 2 strings at specified position pos
.
⚠ Throws utf::invalid_argument
if pos
is negative.
Returns right side-copy of original string, length length() - pos
. If pos >= length()
, returns an empty string.
Converts all ASCII characters in the string into its lowercase.
Returns *this
.
Converts all ASCII characters in the string into its uppercase.
Returns *this
.
Applies given mutator to every character in the string.
Template constraints:
-
std::is_invocable_r<string::char_type, Functor, string::char_type>
.
Returns *this
.
Removes all characters satisfying specified criteria from both sides of the string.
Template constraints:
-
std::is_invocable_r<bool, Functor, string::char_type>
; -
std::predicate<Functor, string::char_type>
.
Returns *this
.
Removes all whitespace-like characters from both sides of the string.
The effect is equivalent to trim(utf::is_space)
.
Returns *this
.
Removes all occurrences of the given character from both sides of the string.
⚠ Throws utf::unicode_error
in case of ucode
's invalid codepoint.
Returns *this
.
Returns a pointer to the beginning of the string's data.
Returns a pointer to the ending of the string's data. I.e., bytes() + size() == bytes_end()
.
Returns the full size of the allocated buffer's memory.
Predicate. Returns true
if the string does not contains any characters. The effect is equivalent to string::operator ! ()
.
Returns the number of Unicode characters in the string.
This is an
O(n)
operation as it requires iteration over every UTF-8 character of the string.
Returns the number of the UTF-8 bytes data used by the string.