50#if __has_include(<fmt/xchar.h>)
61#include <initializer_list>
167 template<
std::input_iterator InputIt>
244 return data() +
static_cast<std::size_t
>(
size());
252 return data() +
static_cast<std::size_t
>(
size());
329 const
wchar_t*
data() const noexcept;
335 inline const
wchar_t*
c_str() const noexcept {
369 inline bool empty() const noexcept {
396 template<std::input_iterator InputIt>
398 for (; first != last; ++first)
410 template<std::ranges::input_range R>
412 return append(std::ranges::begin(range), std::ranges::end(range));
438 template<std::input_iterator InputIt>
441 return append(first, last);
451 template<std::ranges::input_range R>
453 return assign(std::ranges::begin(range), std::ranges::end(range));
536 template<std::input_iterator InputIt>
538 const Size index{
static_cast<std::size_t
>(pos -
cbegin())};
540 copied.
append(first, last);
541 insert(index,
static_cast<std::wstring_view
>(copied));
542 return begin() +
static_cast<std::size_t
>(index);
564 template<std::ranges::input_range R>
566 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
622 template<
class... Args>
623 requires (
sizeof...(Args) >= 1)
625 constexpr std::size_t count =
sizeof...(Args);
628 std::wstring scratch[count];
629 std::wstring_view tokens[count];
630 std::size_t offsets[count];
631 std::size_t lengths[count];
632 std::size_t index = 0;
633 auto store = [&](
auto&& value) {
636 using Bare = std::remove_cvref_t<
decltype(value)>;
639 scratch[index] =
static_cast<std::wstring
>(std::forward<
decltype(value)>(value));
641 const WString owned(
static_cast<WString>(std::forward<
decltype(value)>(value)));
642 scratch[index].assign(
static_cast<std::wstring_view
>(owned));
644 static_assert(
sizeof(Bare) == 0,
"ReplaceToken argument must be text, a number, a pointer, or convertible to std::wstring");
646 tokens[index] = scratch[index];
647 offsets[index] = places[index].
offset;
648 lengths[index] = places[index].
length;
651 (store(std::forward<Args>(args)), ...);
652 return ReplaceTokens(text, std::span<const std::wstring_view>(tokens, found), std::span<const std::size_t>(offsets, found), std::span<const std::size_t>(lengths, found));
662 assert(index <=
size());
663 return data()[
static_cast<std::size_t
>(index)];
673 assert(index <=
size());
674 return data()[
static_cast<std::size_t
>(index)];
707 inline explicit operator bool() const noexcept {
723 inline operator std::wstring_view() const noexcept {
724 return std::wstring_view(
data(),
static_cast<std::size_t
>(
size()));
732 return std::wstring(
static_cast<std::wstring_view
>(*
this));
740 inline explicit operator const wchar_t*()
const noexcept {
754 inline const wchar_t*
Bytes() const noexcept {
773 return static_cast<std::wstring_view
>(*this).starts_with(text);
782 return static_cast<std::wstring_view
>(*this).starts_with(ch);
790 inline bool ends_with(std::wstring_view text)
const noexcept {
791 return static_cast<std::wstring_view
>(*this).ends_with(text);
800 return static_cast<std::wstring_view
>(*this).ends_with(ch);
808 inline bool contains(std::wstring_view text)
const noexcept {
809 return static_cast<std::wstring_view
>(*this).contains(text);
818 return static_cast<std::wstring_view
>(*this).contains(ch);
827 inline Size find(std::wstring_view text,
Size pos = {})
const noexcept {
828 return FindAt(
static_cast<std::wstring_view
>(*
this), text, pos);
838 return FindAt(
static_cast<std::wstring_view
>(*
this), ch, pos);
849 return find(std::wstring_view(text,
static_cast<std::size_t
>(count)), pos);
859 return RFindAt(
static_cast<std::wstring_view
>(*
this), text, pos);
869 return RFindAt(
static_cast<std::wstring_view
>(*
this), ch, pos);
880 return rfind(std::wstring_view(text,
static_cast<std::size_t
>(count)), pos);
890 const std::wstring_view self = *
this;
891 if (pos >
Size{self.size()})
893 return FromIndex(self.find_first_of(text,
static_cast<std::size_t
>(pos)));
903 return find(ch, pos);
913 const std::wstring_view self = *
this;
914 const std::size_t start = pos ==
npos ? std::wstring_view::npos :
static_cast<std::size_t
>(pos);
915 return FromIndex(self.find_last_of(text, start));
925 return rfind(ch, pos);
935 const std::wstring_view self = *
this;
936 if (pos >
Size{self.size()})
938 return FromIndex(self.find_first_not_of(text,
static_cast<std::size_t
>(pos)));
948 const std::wstring_view self = *
this;
949 if (pos >
Size{self.size()})
951 return FromIndex(self.find_first_not_of(ch,
static_cast<std::size_t
>(pos)));
961 const std::wstring_view self = *
this;
962 const std::size_t start = pos ==
npos ? std::wstring_view::npos :
static_cast<std::size_t
>(pos);
963 return FromIndex(self.find_last_not_of(text, start));
973 const std::wstring_view self = *
this;
974 const std::size_t start = pos ==
npos ? std::wstring_view::npos :
static_cast<std::size_t
>(pos);
975 return FromIndex(self.find_last_not_of(ch, start));
985 const std::wstring_view text = *
this;
986 if (pos >
Size{text.size()})
988 const std::size_t n = count == npos ? std::wstring_view::npos :
static_cast<std::size_t
>(count);
989 return WString(text.substr(
static_cast<std::size_t
>(pos), n));
997 inline int compare(std::wstring_view text)
const noexcept {
998 return static_cast<std::wstring_view
>(*this).compare(text);
1009 return static_cast<std::wstring_view
>(
substr(pos, count)).
compare(text);
1018 return (*
this)[
Size{0}];
1027 return (*
this)[
Size{0}];
1044 inline wchar_t back() const noexcept {
1104 for (
const WString& part : parts)
1139 return ToLower(
static_cast<std::wstring_view
>(*
this));
1147 return ToUpper(
static_cast<std::wstring_view
>(*
this));
1171 return IsInteger(
static_cast<std::wstring_view
>(*
this));
1200 return static_cast<std::wstring_view
>(*this) ==
static_cast<std::wstring_view
>(other);
1209 return !(*
this == other);
1218 return str ?
static_cast<std::wstring_view
>(*this) == std::wstring_view(str) :
empty();
1227 return !(*
this == str);
1236 return static_cast<std::wstring_view
>(*this) <=>
static_cast<std::wstring_view
>(other);
1245 return str ?
static_cast<std::wstring_view
>(*this) <=> std::wstring_view(str)
1246 :
static_cast<std::wstring_view
>(*this) <=> std::wstring_view{};
1264 struct alignas(16) Storage {
1272 static_assert(
sizeof(Storage) >= SSO_CAPACITY *
sizeof(wchar_t));
1273 static_assert(
sizeof(Storage) %
alignof(
void*) == 0);
1274 static_assert(
alignof(Storage) == 16);
1280 bool IsLong() const noexcept;
1285 void ReleaseLong() noexcept;
1290 void SetEmpty() noexcept;
1297 void SetShort(const
wchar_t* text,
std::
size_t count) noexcept;
1304 static
Size FromIndex(
std::
size_t index) noexcept {
1305 return index == std::wstring_view::npos ? npos :
Size{index};
1316 template<
typename Needle>
1317 static Size FindAt(std::wstring_view self, Needle needle, Size pos)
noexcept {
1318 if (pos > Size{self.size()})
1320 return FromIndex(self.find(needle,
static_cast<std::size_t
>(pos)));
1331 template<
typename Needle>
1332 static Size RFindAt(std::wstring_view self, Needle needle, Size pos)
noexcept {
1333 const std::size_t start = pos == npos ? std::wstring_view::npos :
static_cast<std::size_t
>(pos);
1334 return FromIndex(self.rfind(needle, start));
1347 static WString ReplaceTokens(std::wstring_view text, std::span<const std::wstring_view> tokens, std::span<const std::size_t> offsets, std::span<const std::size_t> lengths);
1348 Storage m_storage{};
1358 return stream << static_cast<std::wstring_view>(text);
1413 const std::wstring_view view = text;
1414 return HashBytes(std::as_bytes(std::span<const wchar_t>(view.data(), view.size())));
1422struct std::formatter<
StormByte::Safe::WString, wchar_t>: std::formatter<std::wstring_view, wchar_t> {
1431 template<
typename FormatContext>
1433 const std::wstring_view view = text;
1434 return std::formatter<std::wstring_view, wchar_t>::format(view, context);
1438#if __has_include(<fmt/xchar.h>)
1443struct fmt::formatter<
StormByte::Safe::WString, wchar_t>: fmt::formatter<std::wstring_view, wchar_t> {
1452 template<
typename FormatContext>
1454 const std::wstring_view view = text;
1455 return fmt::formatter<std::wstring_view, wchar_t>::format(view, context);
FIFO sequence stored on Base's heap.
Definition queue.hxx:77
Owned UTF-8 text.
Definition string.hxx:113
Contiguous sequence stored on Base's heap.
Definition vector.hxx:81
void push_back(const T &value)
Append a copy.
Wide counterpart of String.
Definition wstring.hxx:104
static WString RemoveWhitespace(std::wstring_view str)
Drops wide space units.
WString & operator+=(wchar_t character)
Append one wide code unit.
const_reverse_iterator rend() const noexcept
Reverse end.
Definition wstring.hxx:299
void resize(size_type count, wchar_t character=wchar_t{})
Resize the unit sequence, filling new units with character.
Size capacity() const noexcept
Allocated unit capacity, excluding the trailing NUL.
Size max_size() const noexcept
Largest representable unit count.
wchar_t & front() noexcept
First unit.
Definition wstring.hxx:1017
static Queue< WString > Explode(std::wstring_view str, wchar_t delimiter)
Tokens on delimiter, including empty ones.
void shrink_to_fit()
Drop unused long-path capacity.
void clear()
Replace the contents with an empty string, retaining long-path capacity.
reverse_iterator rend() noexcept
Reverse end.
Definition wstring.hxx:291
int compare(std::wstring_view text) const noexcept
Same order as std::wstring_view::compare.
Definition wstring.hxx:997
wchar_t & operator[](const Size &index) noexcept
Code unit at index.
Definition wstring.hxx:661
bool empty() const noexcept
Whether Size is zero.
Definition wstring.hxx:369
std::ptrdiff_t difference_type
Iterator distance type.
Definition wstring.hxx:108
Size find_first_of(wchar_t ch, Size pos={}) const noexcept
First ch at or after pos.
Definition wstring.hxx:902
const wchar_t * Bytes() const noexcept
Non-owning view of the owned units.
Definition wstring.hxx:754
static constexpr size_type npos
Not found.
Definition wstring.hxx:765
bool operator!=(const WString &other) const noexcept
Content inequality.
Definition wstring.hxx:1208
const_iterator begin() const noexcept
First unit.
Definition wstring.hxx:235
const_reverse_iterator crend() const noexcept
Reverse end.
Definition wstring.hxx:315
Size find(std::wstring_view text, Size pos={}) const noexcept
First occurrence of text at or after pos.
Definition wstring.hxx:827
wchar_t front() const noexcept
Read the first unit.
Definition wstring.hxx:1026
bool operator==(const wchar_t *str) const noexcept
Content equality with a wide C string.
Definition wstring.hxx:1217
const wchar_t * c_str() const noexcept
Same pointer as data.
Definition wstring.hxx:335
WString substr(Size pos={}, Size count=npos) const noexcept
Copy of a slice.
Definition wstring.hxx:984
iterator end() noexcept
One past the last unit.
Definition wstring.hxx:243
iterator erase(const_iterator first, const_iterator last)
Erase [first, last).
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const wchar_t *str) const noexcept
Content order against a wide C string.
Definition wstring.hxx:1244
bool contains(wchar_t ch) const noexcept
Whether ch occurs.
Definition wstring.hxx:817
Size find(const wchar_t *text, Size pos, Size count) const noexcept
First occurrence of count units of text.
Definition wstring.hxx:848
Size find_last_not_of(wchar_t ch, Size pos=npos) const noexcept
Last unit other than ch, at or before pos.
Definition wstring.hxx:972
wchar_t & at(size_type index)
Unit at index.
Size find_last_not_of(std::wstring_view text, Size pos=npos) const noexcept
Last unit that is not in text, at or before pos.
Definition wstring.hxx:960
WString & operator+=(std::wstring_view text)
Append a wide view.
WString() noexcept
Empty text.
WString SanitizeNewlines() const
CR LF to LF on this text.
Definition wstring.hxx:1154
WString & assign(size_type count, wchar_t character)
Replace the contents with count copies of a wide code unit.
iterator erase(const_iterator pos)
Erase the unit at pos.
wchar_t operator[](const Size &index) const noexcept
Read-only code unit at index.
Definition wstring.hxx:672
const wchar_t & at(size_type index) const
Read-only unit at index.
reverse_iterator rbegin() noexcept
Reverse begin.
Definition wstring.hxx:275
WString & assign(InputIt first, InputIt last)
Replace the contents with [first, last).
Definition wstring.hxx:439
wchar_t * pointer
Mutable buffer pointer.
Definition wstring.hxx:109
const wchar_t & const_reference
Read-only unit reference.
Definition wstring.hxx:112
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse observer.
Definition wstring.hxx:116
Size rfind(std::wstring_view text, Size pos=npos) const noexcept
Last occurrence of text at or before pos.
Definition wstring.hxx:858
WString ToLower() const
ASCII-letter lower case of this text.
Definition wstring.hxx:1138
WString & replace(size_type position, size_type count, std::wstring_view text)
Replace a wide code-unit range with text.
static WString Implode(const Queue< WString > &parts, std::wstring_view delimiter)
Join parts with delimiter, in queue order.
WString & append(InputIt first, InputIt last)
Append the unit range [first, last).
Definition wstring.hxx:397
Size find(wchar_t ch, Size pos={}) const noexcept
First occurrence of ch at or after pos.
Definition wstring.hxx:837
WString(WString &&other) noexcept
Move constructor.
wchar_t * iterator
Mutable contiguous iterator.
Definition wstring.hxx:113
wchar_t & reference
Mutable unit reference.
Definition wstring.hxx:111
void pop_back()
Remove the final code unit.
void reserve(size_type new_capacity)
Reserve storage for at least new_capacity code units.
WString & append(size_type count, wchar_t character)
Append count copies of a wide code unit.
static Vector< WString > Split(const std::vector< WString > &parts)
Copy caller-owned tokens into Base storage.
Definition wstring.hxx:1102
bool starts_with(std::wstring_view text) const noexcept
Whether the text begins with text.
Definition wstring.hxx:772
Size find_first_of(std::wstring_view text, Size pos={}) const noexcept
First unit that is in text, at or after pos.
Definition wstring.hxx:889
static WString ToUpper(std::wstring_view str)
ASCII-letter upper case; other units copied.
Size rfind(const wchar_t *text, Size pos, Size count) const noexcept
Last occurrence of count units of text.
Definition wstring.hxx:879
const wchar_t * const_pointer
Read-only buffer pointer.
Definition wstring.hxx:110
iterator insert(const_iterator pos, InputIt first, InputIt last)
Insert [first, last) before pos.
Definition wstring.hxx:537
WString & assign_range(R &&range)
Replace the contents with a copy of range.
Definition wstring.hxx:452
Size length() const noexcept
Same as Size.
Definition wstring.hxx:361
Vector< WString > Split() const
Whitespace-separated tokens of this text.
Size find_last_of(std::wstring_view text, Size pos=npos) const noexcept
Last unit that is in text, at or before pos.
Definition wstring.hxx:912
WString & assign(std::wstring_view text)
Replace the contents with a wide text view.
wchar_t value_type
Code unit type.
Definition wstring.hxx:106
const_reverse_iterator rbegin() const noexcept
Reverse begin.
Definition wstring.hxx:283
WString ToUpper() const
ASCII-letter upper case of this text.
Definition wstring.hxx:1146
bool ends_with(std::wstring_view text) const noexcept
Whether the text ends with text.
Definition wstring.hxx:790
int compare(size_type pos, size_type count, std::wstring_view text) const noexcept
Compare a slice with text.
Definition wstring.hxx:1008
~WString() noexcept
Releases a long buffer.
bool starts_with(wchar_t ch) const noexcept
Whether the text begins with ch.
Definition wstring.hxx:781
static WString ToLower(std::wstring_view str)
ASCII-letter lower case; other units copied.
static bool IsInteger(std::wstring_view str) noexcept
Optional sign plus ASCII digits.
iterator begin() noexcept
First unit.
Definition wstring.hxx:227
bool ends_with(wchar_t ch) const noexcept
Whether the text ends with ch.
Definition wstring.hxx:799
WString & replace(const_iterator first, const_iterator last, std::wstring_view text)
Replace [first, last) with text.
Size find_first_not_of(wchar_t ch, Size pos={}) const noexcept
First unit other than ch, at or after pos.
Definition wstring.hxx:947
const_iterator cend() const noexcept
One past the last unit.
Definition wstring.hxx:267
const_iterator cbegin() const noexcept
First unit.
Definition wstring.hxx:259
bool operator!=(const wchar_t *str) const noexcept
Content inequality with a wide C string.
Definition wstring.hxx:1226
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse observer.
Definition wstring.hxx:115
void swap(WString &other) noexcept
Swaps storage with other.
void push_back(wchar_t character)
Append one wide code unit.
static constexpr std::size_t SSO_CAPACITY
Short capacity, excluding the trailing NUL.
Definition wstring.hxx:118
WString & insert(size_type position, size_type count, wchar_t character)
Insert count copies of character at position.
WString(const String &other)
Wide text from UTF-8, including an empty source.
WString & append_range(R &&range)
Append a copy of range.
Definition wstring.hxx:411
wchar_t & back() noexcept
Last unit.
Definition wstring.hxx:1035
Size size_type
Unit count and position type.
Definition wstring.hxx:107
WString & erase(size_type position={}, size_type count=npos)
Erase wide code units starting at position.
Queue< WString > Explode(wchar_t delimiter) const
Tokens of this text on delimiter, including empty ones.
size_type copy(wchar_t *dest, size_type count, size_type pos={}) const
Copy at most count units into dest, starting at pos.
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const WString &other) const noexcept
Content order.
Definition wstring.hxx:1235
wchar_t back() const noexcept
Read the last unit.
Definition wstring.hxx:1044
Size rfind(wchar_t ch, Size pos=npos) const noexcept
Last occurrence of ch at or before pos.
Definition wstring.hxx:868
iterator insert_range(const_iterator pos, R &&range)
Insert a copy of range before pos.
Definition wstring.hxx:565
const_iterator end() const noexcept
One past the last unit.
Definition wstring.hxx:251
Size find_first_not_of(std::wstring_view text, Size pos={}) const noexcept
First unit that is not in text, at or after pos.
Definition wstring.hxx:934
const wchar_t * const_iterator
Contiguous observer.
Definition wstring.hxx:114
WString RemoveWhitespace() const
Drops wide space units from this text.
Definition wstring.hxx:1162
iterator insert(const_iterator pos, std::initializer_list< wchar_t > values)
Insert an initializer list before pos.
static WString Implode(const Vector< WString > &parts, std::wstring_view delimiter)
Join parts with delimiter.
wchar_t * data() noexcept
Contiguous pointer to the first unit, or to the trailing NUL when empty.
static WString SanitizeNewlines(std::wstring_view str)
Turns CR LF into LF.
const_reverse_iterator crbegin() const noexcept
Reverse begin.
Definition wstring.hxx:307
bool contains(std::wstring_view text) const noexcept
Whether text occurs.
Definition wstring.hxx:808
operator std::wstring_view() const noexcept
Non-owning view of the text.
Definition wstring.hxx:723
WString & insert(size_type position, std::wstring_view text)
Insert wide code units at a code-unit position.
static Vector< WString > Split(std::wstring_view str)
Whitespace-separated tokens.
WString(const WString &other)
Copy constructor.
bool operator==(const WString &other) const noexcept
Content equality.
Definition wstring.hxx:1199
Size size() const noexcept
Unit count, excluding the trailing NUL.
static WString ReplaceToken(std::wstring_view text, Args &&... args)
Copy text with each field replaced, in order, by one argument.
Definition wstring.hxx:624
Size find_last_of(wchar_t ch, Size pos=npos) const noexcept
Last ch at or before pos.
Definition wstring.hxx:924
WString & append(std::wstring_view text)
Append wide code units.
bool IsInteger() const noexcept
Whether this text is an integer token.
Definition wstring.hxx:1170
Count of abstract units (uint64_t storage).
Definition size.hxx:107
True when FormatReplaceToken always writes T and never asks for a string conversion.
Definition replace_token.hxx:258
From may be explicitly converted to To with static_cast.
Definition conversions.hxx:86
Owned values that cross a DLL without the caller's CRT.
bool FormatReplaceToken(std::basic_string< Char > &out, T &&value, const ReplaceTokenSpec &spec)
Render one argument when it does not need an owned-string conversion.
Definition replace_token.hxx:279
bool operator==(std::span< const std::byte > bytes, const Binary &data) noexcept
Equality of a span against a sequence.
Definition binary.hxx:770
STORMBYTE_FORCE_INLINE std::ostream & operator<<(std::ostream &stream, const String &text)
Writes text to stream.
Definition string.hxx:1365
std::size_t FindReplaceTokens(std::basic_string_view< Char > text, ReplaceTokenPlace< Char > *places, std::size_t capacity) noexcept
Collect up to capacity fields, in order.
Definition replace_token.hxx:155
bool operator!=(std::span< const std::byte > bytes, const Binary &data) noexcept
Inequality of a span against a sequence.
Definition binary.hxx:780
void swap(Binary &left, Binary &right) noexcept
Exchange two sequences.
Definition binary.hxx:760
String operator+(const String &left, const String &right)
Concatenate two texts.
Definition string.hxx:1396
STORMBYTE_FORCE_INLINE std::size_t HashBytes(std::span< const std::byte > bytes) noexcept
Mix a byte range with FNV-1a.
Definition hash.hxx:66
Root namespace of the StormByte suite.
Host-width-stable count (uint64_t), implicit only to std::size_t.
STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::WString &text) const noexcept
Hash text.
Definition wstring.hxx:1412
Cross-module hash.
Definition hash.hxx:95
Where a field sits in the pattern, and how to render its argument.
Definition replace_token.hxx:81
std::size_t offset
Definition replace_token.hxx:82
std::size_t length
Definition replace_token.hxx:83
std::size_t operator()(const StormByte::Safe::WString &text) const noexcept
Hashes text.
Definition wstring.hxx:1467
#define STORMBYTE_PUBLIC
Definition visibility.h:52