17#include "AUI/Traits/values.h"
18#include <AUI/Common/ASet.h>
20#include <AUI/Common/AOptional.h>
39 friend struct std::hash<
AString>;
40 using super = std::u16string;
44 using iterator = super::iterator;
45 using value_type = super::value_type;
46 using const_iterator = super::const_iterator;
47 using reverse_iterator = super::reverse_iterator;
48 using const_reverse_iterator = super::const_reverse_iterator;
49 auto constexpr static NPOS = super::npos;
53 : std::u16string(
static_cast<basic_string&&
>(other))
60 AString(
const basic_string& other) noexcept
61 : basic_string<char16_t>(other)
68 AString(
const std::string& utf8)
noexcept;
71 : super(other.c_str())
75 AString(
const basic_string& rhs,
const std::allocator<char16_t>& allocator) noexcept
76 : basic_string<char16_t>(rhs, allocator)
80 template <
class Iterator>
81 AString(Iterator first, Iterator last) noexcept : super(first, last) {}
87 AString(char16_t c) noexcept : super(&c, &c + 1)
95 AString(
const char* utf8)
noexcept;
100 AString(std::string_view utf8)
noexcept;
102 explicit AString(
const std::allocator<char16_t>& allocator) noexcept
103 : basic_string<char16_t>(allocator)
107 AString(
const basic_string& rhs, size_type offset,
const std::allocator<char16_t>& allocator) noexcept
108 : basic_string<char16_t>(rhs, offset, allocator)
112 AString(
const basic_string& rhs, size_type offset, size_type count,
const std::allocator<char16_t>& allocator) noexcept
113 : basic_string<char16_t>(rhs, offset, count, allocator)
117 AString(
const char16_t* cStyleString, size_type count) noexcept
118 : basic_string<char16_t>(cStyleString, count)
122 AString(
const char16_t* cStyleString, size_type count,
const std::allocator<char16_t>& allocator) noexcept
123 : basic_string<char16_t>(cStyleString, count, allocator)
127 AString(
const char16_t* cStyleString) noexcept
128 : basic_string<char16_t>(cStyleString)
132 AString(
const char16_t* cStyleString,
const std::allocator<char16_t>& allocator) noexcept
133 : basic_string<char16_t>(cStyleString, allocator)
137 AString(size_type count, char16_t _Ch) noexcept
138 : basic_string<char16_t>(count, _Ch)
142 AString(size_type count, char16_t _Ch,
const std::allocator<char16_t>& allocator) noexcept
143 : basic_string<char16_t>(count, _Ch, allocator)
147 AString(basic_string&& rhs) noexcept
148 : basic_string<char16_t>(std::move(rhs))
152 AString(basic_string&& rhs,
const std::allocator<char16_t>& allocator) noexcept
153 : basic_string<char16_t>(std::move(rhs), allocator)
157 AString(std::initializer_list<char16_t> _Ilist) noexcept
158 : basic_string<char16_t>(_Ilist)
165 void push_back(char16_t c)
noexcept
169 void pop_back() noexcept
177 bool startsWith(
const AString& other)
const noexcept
179 return rfind(other, 0) == 0;
181 bool startsWith(char16_t c)
const noexcept
183 return rfind(c, 0) == 0;
185 bool endsWith(
const AString& other)
const noexcept
187 if (length() < other.length())
191 size_t offset = length() - other.length();
192 return super::find(other, offset) == offset;
194 bool endsWith(char16_t c)
const noexcept
196 size_t offset = length() - 1;
197 return super::find(c, offset) == offset;
202 size_type find(
char c, size_type offset = 0) const noexcept
204 return super::find(c, offset);
206 size_type find(char16_t c, size_type offset = 0) const noexcept
208 return super::find(c, offset);
210 size_type find(
const AString& str, size_type offset = 0) const noexcept
212 return super::find(str, offset);
214 size_type rfind(
char c, size_type offset = NPOS)
const noexcept
216 return super::rfind(c, offset);
218 size_type rfind(char16_t c, size_type offset = NPOS)
const noexcept
220 return super::rfind(c, offset);
222 size_type rfind(
const AString& str, size_type offset = NPOS)
const noexcept
224 return super::rfind(str, offset);
226 size_type length() const noexcept
228 return super::length();
230 AString trimLeft(char16_t symbol =
' ') const noexcept;
231 AString trimRight(char16_t symbol = ' ') const noexcept;
233 AString trim(char16_t symbol = ' ') const noexcept
235 return trimRight(symbol).trimLeft(symbol);
238 void reserve(
size_t s)
242 void resize(
size_t s)
247 AString restrictLength(
size_t s,
const AString& stringAtEnd =
"...")
const;
249 char16_t* data() noexcept
251 return super::data();
254 const char16_t* data() const noexcept
256 return super::data();
260 [[nodiscard]]
inline AString replacedAll(char16_t from, char16_t to)
const noexcept {
262 copy.reserve(length() + 10);
263 for (
auto c : *
this) {
274 copy.reserve(length() + 10);
275 for (
auto c : *
this) {
276 if (from.contains(c)) {
284 AString& replaceAll(char16_t from, char16_t to)
noexcept;
293 template<
typename OtherContainer>
295 super::insert(super::end(), c.begin(), c.end());
314 AOptional<
double> toDouble() const noexcept;
323 double toDoubleOrException() const noexcept {
324 return toDouble().
valueOrException(fmt::format(
"bad double: {}", toStdString()).c_str());
345 int toIntOrException()
const {
346 return toInt().
valueOrException(fmt::format(
"bad int: {}", toStdString()).c_str());
367 int64_t toLongIntOrException()
const {
368 return toLongInt().
valueOrException(fmt::format(
"bad to number conversion: {}", toStdString()).c_str());
389 unsigned toUIntOrException()
const {
390 return toUInt().
valueOrException(fmt::format(
"bad to number conversion: {}", toStdString()).c_str());
399 return *
this ==
"true";
403 bool contains(char16_t c)
const noexcept
405 return find(c) != npos;
410 return find(other) != npos;
415 static AString fromUtf8(
const char* buffer,
size_t length);
416 static AString fromLatin1(
const char* buffer);
418 static AString numberHex(
int i)
noexcept;
420 template<
typename T, std::enable_if_t<std::is_
integral_v<std::decay_t<T>> || std::is_
floating_po
int_v<std::decay_t<T>>,
int> = 0>
421 static AString number(T i)
noexcept {
422 if constexpr (std::is_same_v<bool, std::decay_t<T>>) {
427 auto v = std::to_string(i);
428 if constexpr (std::is_floating_point_v<T>) {
430 v.erase(v.find_last_not_of(
'0') + 1, std::u16string::npos);
431 v.erase(v.find_last_not_of(
'.') + 1, std::u16string::npos);
437 static constexpr auto TO_NUMBER_BASE_BIN = 2;
438 static constexpr auto TO_NUMBER_BASE_OCT = 8;
439 static constexpr auto TO_NUMBER_BASE_DEC = 10;
440 static constexpr auto TO_NUMBER_BASE_HEX = 16;
454 return toNumber(base).valueOrException(fmt::format(
"bad to number conversion: {}", toStdString()).c_str());
461 std::string toStdString() const noexcept;
463 void resizeToNullTerminator();
465 iterator erase(const_iterator begin, const_iterator end) noexcept
467 return super::erase(begin, end);
469 iterator erase(const_iterator begin)
noexcept
471 return super::erase(begin);
474 AString& erase(size_type offset)
noexcept
476 super::erase(offset);
479 AString& erase(size_type offset, size_type count)
noexcept
481 super::erase(offset, count);
487 void removeAt(
unsigned at) noexcept
492 AString excessSpacesRemoved() const noexcept;
494 iterator insert(size_type at, char16_t c) noexcept
497 return super::insert(begin() + at, 1, c);
499 iterator insert(size_type at,
const AString& c)
noexcept
502 return super::insert(begin() + at, c.begin(), c.end());
505 template<
typename Iterator>
506 iterator insert(const_iterator at, Iterator begin, Iterator end)
noexcept
508 AUI_ASSERT(std::distance(super::cbegin(), at) <= length());
509 return super::insert(at, begin, end);
512 AString& operator<<(
char c)
noexcept
517 AString& operator<<(char16_t c)
noexcept
523 inline ::AString& operator+=(
const AString& str)
noexcept
528 inline ::AString& operator+=(
const char* str)
noexcept
534 [[nodiscard]]
bool empty() const noexcept {
535 return super::empty();
537 [[nodiscard]] size_type size() const noexcept {
538 return super::size();
540 char16_t operator[](size_type index)
const
542 return super::at(index);
544 char16_t& operator[](size_type index)
546 return super::at(index);
548 bool operator<(
const AString& other)
const noexcept
550 return compare(other) < 0;
553 void clear() noexcept
558 char16_t& front() noexcept
560 return super::front();
562 char16_t& back() noexcept
564 return super::back();
566 const char16_t& front() const noexcept
568 return super::front();
570 const char16_t& back() const noexcept
572 return super::back();
574 char16_t& first() noexcept
576 return super::front();
578 char16_t& last() noexcept
580 return super::back();
582 const char16_t& first() const noexcept
584 return super::front();
586 const char16_t& last() const noexcept
588 return super::back();
591 const char16_t* c_str()
const
593 return super::c_str();
596 iterator begin() noexcept
598 return super::begin();
600 iterator end() noexcept
605 const_iterator begin() const noexcept
607 return super::begin();
609 const_iterator end() const noexcept
614 reverse_iterator rbegin() noexcept
616 return super::rbegin();
618 reverse_iterator rend() noexcept
620 return super::rend();
623 const_reverse_iterator rbegin() const noexcept
625 return super::rbegin();
627 const_reverse_iterator rend() const noexcept
629 return super::rend();
638 AString& append(
size_t count, char16_t ch)
noexcept
640 super::append(count, ch);
646 super::operator=(value);
652 super::operator=(value);
656 bool operator==(
const AString& other)
const noexcept
658 if (size() != other.size()) {
661 return std::memcmp(data(), other.data(), sizeInBytes()) == 0;
663 bool operator==(
const char16_t* other)
const noexcept
666 for (; it != end(); ++it, ++other) {
670 if (*other ==
'\0') {
674 return *other ==
'\0';
678 size_t sizeInBytes() const noexcept {
679 return size() *
sizeof(super::value_type);
682 bool operator!=(
const AString& other)
const noexcept
684 return !operator==(other);
686 bool operator!=(
const char16_t* other)
const noexcept
688 return !operator==(other);
691 bool operator==(
const char* other)
const noexcept
693 return *
this ==
AString(other);
696 bool operator!=(
const char* other)
const noexcept
698 return *
this !=
AString(other);
701 template<
typename... Args>
704 AString processEscapes()
const;
706 AString& removeAll(char16_t c)
noexcept {
707 erase(std::remove(begin(), end(), c));
712 AString substr(std::size_t offset, std::size_t count = npos)
const {
713 return super::substr(offset, count);
736inline AString operator+(
const AString& one,
const char* other)
noexcept
741inline AString operator+(
const char* other,
const AString& one)
noexcept {
752inline AString operator""_as(
const char* str,
size_t len)
757inline std::ostream& operator<<(std::ostream& o,
const AString& s)
766 size_t operator()(
const AString& t)
const
768 return std::hash<std::u16string>()(t);
773namespace aui::win32 {
779 inline const wchar_t* toWchar(
const AString&
string) {
781 return reinterpret_cast<const wchar_t *const
>(
string.data());
784 inline wchar_t* toWchar(
AString&
string) {
786 return reinterpret_cast<wchar_t*
>(
string.data());
789 inline std::wstring_view toWcharView(
const AString&
string) {
790 return {toWchar(
string),
string.length() };
793 inline AString fromWchar(std::wstring_view
string) {
795 return {
reinterpret_cast<const char16_t *
>(
string.data()),
string.size()};
800template <>
struct fmt::detail::is_string<
AString>: std::false_type {};
802template <>
struct fmt::formatter<
AString>: fmt::formatter<std::string> {
803 auto format(
const AString& s, format_context& ctx)
const {
804 return fmt::formatter<std::string>::format(s.
toStdString(), ctx);
810inline void PrintTo(
const AString& s, std::ostream* stream) {
Acts like std::string_view but for AByteBuffer.
Definition: AByteBufferView.h:24
std::vector-like growing array for byte storage.
Definition: AByteBuffer.h:31
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
T & valueOrException(const char *message="empty optional")
value or exception
Definition: AOptional.h:207
A std::set with AUI extensions.
Definition: ASet.h:25
An AVector with string-related functions.
Definition: AStringVector.h:22
Represents a Unicode character string.
Definition: AString.h:37
AString(const basic_string &other) noexcept
Definition: AString.h:60
bool toBool() const noexcept
Converts the string to boolean value.
Definition: AString.h:398
void insertAll(const OtherContainer &c) noexcept
Definition: AString.h:294
std::string toStdString() const noexcept
Definition: AString.cpp:338
int toNumberOrException(aui::ranged_number< int, 2, 36 > base=TO_NUMBER_BASE_DEC) const
Returns the string converted to an int using base. Throws an exception if the conversion fails.
Definition: AString.h:453
API_AUI_CORE const ACommandLineArgs & args() noexcept
Definition: OSAndroid.cpp:29
bool contains(const Container &c, const typename Container::const_reference value) noexcept
Definition: containers.h:153
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition: Assert.h:55
Clamps the possible values for a number to the specified range: [min;max].
Definition: values.h:452