36class API_AUI_CORE AString: std::u16string
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;
52 AString(AString&& other) noexcept
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)
162 ~AString() =
default;
165 void push_back(
char16_t c)
noexcept
169 void pop_back() noexcept
174 AString uppercase()
const;
175 AString lowercase()
const;
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;
200 AStringVector split(
char16_t c)
const noexcept;
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();
258 AString& replaceAll(
const AString& from,
const AString& to);
259 [[nodiscard]] AString replacedAll(
const AString& from,
const AString& to)
const;
260 [[nodiscard]]
inline AString replacedAll(
char16_t from,
char16_t to)
const noexcept {
262 copy.reserve(length() + 10);
263 for (
auto c : *
this) {
272 [[nodiscard]]
inline AString replacedAll(
const ASet<char16_t>& from,
char16_t to)
const noexcept {
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;
292 template<
typename OtherContainer>
294 super::insert(super::end(), c.begin(), c.end());
313 AOptional<
double> toDouble() const noexcept;
345 return toInt().valueOrException(fmt::format(
"bad int: {}",
toStdString()).c_str());
367 return toLongInt().valueOrException(fmt::format(
"bad to number conversion: {}",
toStdString()).c_str());
389 return toUInt().valueOrException(fmt::format(
"bad to number conversion: {}",
toStdString()).c_str());
398 return *
this ==
"true";
402 bool contains(
char16_t c)
const noexcept
404 return find(c) != npos;
407 bool contains(
const AString& other)
const noexcept
409 return find(other) != npos;
412 static AString fromLatin1(
const AByteBuffer& buffer);
413 static AString fromUtf8(
const AByteBufferView& buffer);
414 static AString fromUtf8(
const char* buffer,
size_t length);
415 static AString fromLatin1(
const char* buffer);
417 static AString numberHex(
int i)
noexcept;
419 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>
420 static AString number(T i)
noexcept {
421 if constexpr (std::is_same_v<bool, std::decay_t<T>>) {
426 auto v = std::to_string(i);
427 if constexpr (std::is_floating_point_v<T>) {
429 v.erase(v.find_last_not_of(
'0') + 1, std::u16string::npos);
430 v.erase(v.find_last_not_of(
'.') + 1, std::u16string::npos);
436 static constexpr auto TO_NUMBER_BASE_BIN = 2;
437 static constexpr auto TO_NUMBER_BASE_OCT = 8;
438 static constexpr auto TO_NUMBER_BASE_DEC = 10;
439 static constexpr auto TO_NUMBER_BASE_HEX = 16;
446 AOptional<int> toNumber(aui::ranged_number<int, 2, 36> base = TO_NUMBER_BASE_DEC)
const noexcept;
453 return toNumber(base).valueOrException(fmt::format(
"bad to number conversion: {}",
toStdString()).c_str());
460 std::string toStdString() const noexcept;
462 void resizeToNullTerminator();
464 iterator erase(const_iterator begin, const_iterator end) noexcept
466 return super::erase(begin, end);
468 iterator erase(const_iterator begin)
noexcept
470 return super::erase(begin);
473 AString& erase(size_type offset)
noexcept
475 super::erase(offset);
478 AString& erase(size_type offset, size_type count)
noexcept
480 super::erase(offset, count);
484 AByteBuffer toUtf8() const noexcept;
486 void removeAt(
unsigned at) noexcept
491 AString excessSpacesRemoved() const noexcept;
493 iterator insert(size_type at,
char16_t c) noexcept
496 return super::insert(begin() + at, 1, c);
498 iterator insert(size_type at,
const AString& c)
noexcept
501 return super::insert(begin() + at,
c.begin(),
c.end());
504 template<
typename Iterator>
505 iterator insert(const_iterator at, Iterator begin, Iterator end)
noexcept
507 AUI_ASSERT(std::distance(super::cbegin(), at) <= length());
508 return super::insert(at, begin, end);
511 AString& operator<<(
char c)
noexcept
516 AString& operator<<(
char16_t c)
noexcept
522 inline ::AString& operator+=(
const AString& str)
noexcept
527 inline ::AString& operator+=(
const char* str)
noexcept
529 *
this += AString(str);
533 [[nodiscard]]
bool empty() const noexcept {
534 return super::empty();
536 [[nodiscard]] size_type size() const noexcept {
537 return super::size();
539 char16_t operator[](size_type index)
const
541 return super::at(index);
543 char16_t& operator[](size_type index)
545 return super::at(index);
547 bool operator<(
const AString& other)
const noexcept
549 return compare(other) < 0;
552 void clear() noexcept
557 char16_t& front() noexcept
559 return super::front();
561 char16_t& back() noexcept
563 return super::back();
565 const char16_t& front() const noexcept
567 return super::front();
569 const char16_t& back() const noexcept
571 return super::back();
573 char16_t& first() noexcept
575 return super::front();
577 char16_t& last() noexcept
579 return super::back();
581 const char16_t& first() const noexcept
583 return super::front();
585 const char16_t& last() const noexcept
587 return super::back();
590 const char16_t* c_str()
const
592 return super::c_str();
595 iterator begin() noexcept
597 return super::begin();
599 iterator end() noexcept
604 const_iterator begin() const noexcept
606 return super::begin();
608 const_iterator end() const noexcept
613 reverse_iterator rbegin() noexcept
615 return super::rbegin();
617 reverse_iterator rend() noexcept
619 return super::rend();
622 const_reverse_iterator rbegin() const noexcept
624 return super::rbegin();
626 const_reverse_iterator rend() const noexcept
628 return super::rend();
631 AString& append(
const AString& s)
noexcept
637 AString& append(
size_t count,
char16_t ch)
noexcept
639 super::append(count, ch);
643 const AString& operator=(
const AString& value)
noexcept
645 super::operator=(value);
649 const AString& operator=(AString&& value)
noexcept
651 super::operator=(value);
655 bool operator==(
const AString& other)
const noexcept
657 if (size() != other.size()) {
660 return std::memcmp(data(), other.data(), sizeInBytes()) == 0;
662 bool operator==(
const char16_t* other)
const noexcept
665 for (; it != end(); ++it, ++other) {
669 if (*other ==
'\0') {
673 return *other ==
'\0';
677 size_t sizeInBytes() const noexcept {
678 return size() *
sizeof(super::value_type);
681 bool operator!=(
const AString& other)
const noexcept
683 return !operator==(other);
685 bool operator!=(
const char16_t* other)
const noexcept
687 return !operator==(other);
690 bool operator==(
const char* other)
const noexcept
692 return *
this == AString(other);
695 bool operator!=(
const char* other)
const noexcept
697 return *
this != AString(other);
700 template<
typename... Args>
701 inline AString format(Args&&... args)
const;
703 AString processEscapes()
const;
705 AString& removeAll(
char16_t c)
noexcept {
706 erase(std::remove(begin(), end(), c));
711 AString substr(std::size_t offset, std::size_t count = npos)
const {
712 return super::substr(offset, count);
720 AOptional<T> toNumberImpl() const noexcept;