50#if __has_include(<fmt/format.h>)
51#include <fmt/format.h>
61#include <initializer_list>
176 template<
std::input_iterator InputIt>
253 return data() +
static_cast<std::size_t
>(
size());
261 return data() +
static_cast<std::size_t
>(
size());
338 const
char*
data() const noexcept;
344 inline const
char*
c_str() const noexcept {
378 inline bool empty() const noexcept {
405 template<std::input_iterator InputIt>
407 for (; first != last; ++first)
419 template<std::ranges::input_range R>
421 return append(std::ranges::begin(range), std::ranges::end(range));
447 template<std::input_iterator InputIt>
450 return append(first, last);
460 template<std::ranges::input_range R>
462 return assign(std::ranges::begin(range), std::ranges::end(range));
545 template<std::input_iterator InputIt>
547 const Size index{
static_cast<std::size_t
>(pos -
cbegin())};
549 copied.
append(first, last);
550 insert(index,
static_cast<std::string_view
>(copied));
551 return begin() +
static_cast<std::size_t
>(index);
573 template<std::ranges::input_range R>
575 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
631 template<
class... Args>
632 requires (
sizeof...(Args) >= 1)
634 constexpr std::size_t count =
sizeof...(Args);
637 std::string scratch[count];
638 std::string_view tokens[count];
639 std::size_t offsets[count];
640 std::size_t lengths[count];
641 std::size_t index = 0;
642 auto store = [&](
auto&& value) {
645 using Bare = std::remove_cvref_t<
decltype(value)>;
648 scratch[index] =
static_cast<std::string
>(std::forward<
decltype(value)>(value));
650 const String owned(
static_cast<String>(std::forward<
decltype(value)>(value)));
651 scratch[index].assign(
static_cast<std::string_view
>(owned));
653 static_assert(
sizeof(Bare) == 0,
"ReplaceToken argument must be text, a number, a pointer, or convertible to std::string");
655 tokens[index] = scratch[index];
656 offsets[index] = places[index].
offset;
657 lengths[index] = places[index].
length;
660 (store(std::forward<Args>(args)), ...);
661 return ReplaceTokens(text, std::span<const std::string_view>(tokens, found), std::span<const std::size_t>(offsets, found), std::span<const std::size_t>(lengths, found));
671 assert(index <=
size());
672 return data()[
static_cast<std::size_t
>(index)];
682 assert(index <=
size());
683 return data()[
static_cast<std::size_t
>(index)];
716 inline explicit operator bool() const noexcept {
732 inline operator std::string_view() const noexcept {
733 return std::string_view(
data(),
static_cast<std::size_t
>(
size()));
741 return std::string(
static_cast<std::string_view
>(*
this));
749 inline explicit operator const char*()
const noexcept {
763 inline const char*
Bytes() const noexcept {
782 return static_cast<std::string_view
>(*this).starts_with(text);
791 return static_cast<std::string_view
>(*this).starts_with(ch);
799 inline bool ends_with(std::string_view text)
const noexcept {
800 return static_cast<std::string_view
>(*this).ends_with(text);
809 return static_cast<std::string_view
>(*this).ends_with(ch);
817 inline bool contains(std::string_view text)
const noexcept {
818 return static_cast<std::string_view
>(*this).contains(text);
827 return static_cast<std::string_view
>(*this).contains(ch);
836 inline Size find(std::string_view text,
Size pos = {})
const noexcept {
837 return FindAt(
static_cast<std::string_view
>(*
this), text, pos);
847 return FindAt(
static_cast<std::string_view
>(*
this), ch, pos);
858 return find(std::string_view(text,
static_cast<std::size_t
>(count)), pos);
868 return RFindAt(
static_cast<std::string_view
>(*
this), text, pos);
878 return RFindAt(
static_cast<std::string_view
>(*
this), ch, pos);
889 return rfind(std::string_view(text,
static_cast<std::size_t
>(count)), pos);
899 const std::string_view self = *
this;
900 if (pos >
Size{self.size()})
902 return FromIndex(self.find_first_of(text,
static_cast<std::size_t
>(pos)));
912 return find(ch, pos);
922 const std::string_view self = *
this;
923 const std::size_t start = pos ==
npos ? std::string_view::npos :
static_cast<std::size_t
>(pos);
924 return FromIndex(self.find_last_of(text, start));
934 return rfind(ch, pos);
944 const std::string_view self = *
this;
945 if (pos >
Size{self.size()})
947 return FromIndex(self.find_first_not_of(text,
static_cast<std::size_t
>(pos)));
957 const std::string_view self = *
this;
958 if (pos >
Size{self.size()})
960 return FromIndex(self.find_first_not_of(ch,
static_cast<std::size_t
>(pos)));
970 const std::string_view self = *
this;
971 const std::size_t start = pos ==
npos ? std::string_view::npos :
static_cast<std::size_t
>(pos);
972 return FromIndex(self.find_last_not_of(text, start));
982 const std::string_view self = *
this;
983 const std::size_t start = pos ==
npos ? std::string_view::npos :
static_cast<std::size_t
>(pos);
984 return FromIndex(self.find_last_not_of(ch, start));
994 const std::string_view text = *
this;
995 if (pos >
Size{text.size()})
997 const std::size_t n = count == npos ? std::string_view::npos :
static_cast<std::size_t
>(count);
998 return String(text.substr(
static_cast<std::size_t
>(pos), n));
1006 inline int compare(std::string_view text)
const noexcept {
1007 return static_cast<std::string_view
>(*this).compare(text);
1018 return static_cast<std::string_view
>(
substr(pos, count)).
compare(text);
1027 return (*
this)[
Size{0}];
1036 return (*
this)[
Size{0}];
1053 inline char back() const noexcept {
1113 for (
const String& part : parts)
1148 return ToLower(
static_cast<std::string_view
>(*
this));
1156 return ToUpper(
static_cast<std::string_view
>(*
this));
1180 return IsInteger(
static_cast<std::string_view
>(*
this));
1209 return static_cast<std::string_view
>(*this) ==
static_cast<std::string_view
>(other);
1218 return !(*
this == other);
1227 return str ?
static_cast<std::string_view
>(*this) == std::string_view(str) :
empty();
1236 return !(*
this == str);
1245 return static_cast<std::string_view
>(*this) <=>
static_cast<std::string_view
>(other);
1254 return str ?
static_cast<std::string_view
>(*this) <=> std::string_view(str)
1255 :
static_cast<std::string_view
>(*this) <=> std::string_view{};
1273 struct alignas(16) Storage {
1281 static_assert(
sizeof(Storage) == 48);
1282 static_assert(
alignof(Storage) == 16);
1288 bool IsLong() const noexcept;
1293 void ReleaseLong() noexcept;
1298 void SetEmpty() noexcept;
1305 void SetShort(const
char* text,
std::
size_t count) noexcept;
1312 static
Size FromIndex(
std::
size_t index) noexcept {
1313 return index == std::string_view::npos ? npos :
Size{index};
1324 template<
typename Needle>
1325 static Size FindAt(std::string_view self, Needle needle, Size pos)
noexcept {
1326 if (pos > Size{self.size()})
1328 return FromIndex(self.find(needle,
static_cast<std::size_t
>(pos)));
1339 template<
typename Needle>
1340 static Size RFindAt(std::string_view self, Needle needle, Size pos)
noexcept {
1341 const std::size_t start = pos == npos ? std::string_view::npos :
static_cast<std::size_t
>(pos);
1342 return FromIndex(self.rfind(needle, start));
1355 static String ReplaceTokens(std::string_view text, std::span<const std::string_view> tokens, std::span<const std::size_t> offsets, std::span<const std::size_t> lengths);
1356 Storage m_storage{};
1366 return stream << static_cast<std::string_view>(text);
1421 const std::string_view view = text;
1422 return HashBytes(std::as_bytes(std::span<const char>(view.data(), view.size())));
1430struct std::formatter<
StormByte::Safe::String, char>: std::formatter<std::string_view, char> {
1439 template<
typename FormatContext>
1441 const std::string_view view = text;
1442 return std::formatter<std::string_view, char>::format(view, context);
1446#if __has_include(<fmt/format.h>)
1451struct fmt::formatter<
StormByte::Safe::String, char>: fmt::formatter<std::string_view, char> {
1460 template<
typename FormatContext>
1462 const std::string_view view = text;
1463 return fmt::formatter<std::string_view, char>::format(view, context);
FIFO sequence stored on Base's heap.
Definition queue.hxx:77
Owned UTF-8 text.
Definition string.hxx:113
Size find_first_of(std::string_view text, Size pos={}) const noexcept
First byte that is in text, at or after pos.
Definition string.hxx:898
bool empty() const noexcept
Whether Size is zero.
Definition string.hxx:378
String & assign(size_type count, char character)
Replace the contents with count copies of a byte.
String & assign(std::string_view text)
Replace the contents with a text view.
Size find_first_of(char ch, Size pos={}) const noexcept
First ch at or after pos.
Definition string.hxx:911
const char & at(size_type index) const
Read-only byte at index.
String(const String &other)
Copy constructor.
bool starts_with(std::string_view text) const noexcept
Whether the text begins with text.
Definition string.hxx:781
bool operator==(const char *str) const noexcept
Content equality with a C string.
Definition string.hxx:1226
Size find_last_of(std::string_view text, Size pos=npos) const noexcept
Last byte that is in text, at or before pos.
Definition string.hxx:921
String & append(size_type count, char character)
Append count copies of a byte.
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const String &other) const noexcept
Content order.
Definition string.hxx:1244
char * data() noexcept
Contiguous pointer to the first byte, or to the trailing NUL when empty.
bool ends_with(std::string_view text) const noexcept
Whether the text ends with text.
Definition string.hxx:799
Size find_first_not_of(char ch, Size pos={}) const noexcept
First byte other than ch, at or after pos.
Definition string.hxx:956
static Queue< String > Explode(std::string_view str, char delimiter)
Tokens on delimiter, including empty ones.
const_reverse_iterator crend() const noexcept
Reverse end.
Definition string.hxx:324
void reserve(size_type new_capacity)
Reserve storage for at least new_capacity UTF-8 bytes.
static constexpr size_type npos
Not found.
Definition string.hxx:774
Size rfind(std::string_view text, Size pos=npos) const noexcept
Last occurrence of text at or before pos.
Definition string.hxx:867
char front() const noexcept
Read the first byte.
Definition string.hxx:1035
static String ToLower(std::string_view str)
ASCII-letter lower case; UTF-8 otherwise copied.
String & append(std::string_view text)
Append UTF-8 bytes.
reverse_iterator rbegin() noexcept
Reverse begin.
Definition string.hxx:284
size_type copy(char *dest, size_type count, size_type pos={}) const
Copy at most count bytes into dest, starting at pos.
char * iterator
Mutable contiguous iterator.
Definition string.hxx:122
String() noexcept
Empty text.
Size find_last_not_of(char ch, Size pos=npos) const noexcept
Last byte other than ch, at or before pos.
Definition string.hxx:981
std::ptrdiff_t difference_type
Iterator distance type.
Definition string.hxx:117
void pop_back()
Remove the final byte.
static bool IsInteger(std::string_view str) noexcept
Optional sign plus ASCII digits.
iterator erase(const_iterator pos)
Erase the byte at pos.
String RemoveWhitespace() const
Drops isspace bytes from this text.
Definition string.hxx:1171
Size rfind(char ch, Size pos=npos) const noexcept
Last occurrence of ch at or before pos.
Definition string.hxx:877
static String Implode(const Vector< String > &parts, std::string_view delimiter)
Join parts with delimiter.
const_iterator cbegin() const noexcept
First character.
Definition string.hxx:268
Size find(const char *text, Size pos, Size count) const noexcept
First occurrence of count bytes of text.
Definition string.hxx:857
void resize(size_type count, char character=char{})
Resize the byte sequence, filling new bytes with character.
String & insert(size_type position, size_type count, char character)
Insert count copies of character at position.
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const char *str) const noexcept
Content order against a C string.
Definition string.hxx:1253
Queue< String > Explode(char delimiter) const
Tokens of this text on delimiter, including empty ones.
Size max_size() const noexcept
Largest representable byte count.
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse observer.
Definition string.hxx:125
const char * c_str() const noexcept
Same pointer as data.
Definition string.hxx:344
String & replace(const_iterator first, const_iterator last, std::string_view text)
Replace [first, last) with text.
char & at(size_type index)
Byte at index.
void shrink_to_fit()
Drop unused long-path capacity.
operator std::string_view() const noexcept
Non-owning view of the text.
Definition string.hxx:732
const_iterator cend() const noexcept
One past the last character.
Definition string.hxx:276
static String SanitizeNewlines(std::string_view str)
Turns CR LF into LF.
static Vector< String > Split(const std::vector< String > &parts)
Copy caller-owned tokens into Base storage.
Definition string.hxx:1111
const_reverse_iterator rend() const noexcept
Reverse end.
Definition string.hxx:308
char & back() noexcept
Last byte.
Definition string.hxx:1044
bool operator!=(const String &other) const noexcept
Content inequality.
Definition string.hxx:1217
char & front() noexcept
First byte.
Definition string.hxx:1026
String(const WString &other)
UTF-8 from wide text.
char & reference
Mutable character reference.
Definition string.hxx:120
const_reverse_iterator crbegin() const noexcept
Reverse begin.
Definition string.hxx:316
String & append_range(R &&range)
Append a copy of range.
Definition string.hxx:420
iterator insert_range(const_iterator pos, R &&range)
Insert a copy of range before pos.
Definition string.hxx:574
String & replace(size_type position, size_type count, std::string_view text)
Replace a byte range with text.
iterator insert(const_iterator pos, InputIt first, InputIt last)
Insert [first, last) before pos.
Definition string.hxx:546
bool IsInteger() const noexcept
Whether this text is an integer token.
Definition string.hxx:1179
String & insert(size_type position, std::string_view text)
Insert bytes at a byte position.
void clear()
Replace the contents with an empty string, retaining long-path capacity.
static String RemoveWhitespace(std::string_view str)
Drops isspace bytes.
bool contains(char ch) const noexcept
Whether ch occurs.
Definition string.hxx:826
static String ReplaceToken(std::string_view text, Args &&... args)
Copy text with each field replaced, in order, by one argument.
Definition string.hxx:633
const char * Bytes() const noexcept
Non-owning view of the owned bytes.
Definition string.hxx:763
String ToUpper() const
ASCII-letter upper case of this text.
Definition string.hxx:1155
reverse_iterator rend() noexcept
Reverse end.
Definition string.hxx:300
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse observer.
Definition string.hxx:124
Size length() const noexcept
Same as Size.
Definition string.hxx:370
void push_back(char character)
Append one byte.
String ToLower() const
ASCII-letter lower case of this text.
Definition string.hxx:1147
iterator begin() noexcept
First character.
Definition string.hxx:236
int compare(size_type pos, size_type count, std::string_view text) const noexcept
Compare a slice with text.
Definition string.hxx:1017
Size find_last_not_of(std::string_view text, Size pos=npos) const noexcept
Last byte that is not in text, at or before pos.
Definition string.hxx:969
String substr(Size pos={}, Size count=npos) const noexcept
Copy of a slice.
Definition string.hxx:993
char operator[](const Size &index) const noexcept
Read-only character at index.
Definition string.hxx:681
Size rfind(const char *text, Size pos, Size count) const noexcept
Last occurrence of count bytes of text.
Definition string.hxx:888
char value_type
Byte type.
Definition string.hxx:115
String(String &&other) noexcept
Move constructor.
int compare(std::string_view text) const noexcept
Same order as std::string_view::compare.
Definition string.hxx:1006
String & operator+=(char character)
Append one byte.
static String Implode(const Queue< String > &parts, std::string_view delimiter)
Join parts with delimiter, in queue order.
Size size_type
Byte count and position type.
Definition string.hxx:116
char back() const noexcept
Read the last byte.
Definition string.hxx:1053
String & append(InputIt first, InputIt last)
Append the byte range [first, last).
Definition string.hxx:406
Size capacity() const noexcept
Allocated byte capacity, excluding the trailing NUL.
static Vector< String > Split(std::string_view str)
Whitespace-separated tokens.
bool operator!=(const char *str) const noexcept
Content inequality with a C string.
Definition string.hxx:1235
String & assign(InputIt first, InputIt last)
Replace the contents with [first, last).
Definition string.hxx:448
const char & const_reference
Read-only character reference.
Definition string.hxx:121
Size find(char ch, Size pos={}) const noexcept
First occurrence of ch at or after pos.
Definition string.hxx:846
bool operator==(const String &other) const noexcept
Content equality.
Definition string.hxx:1208
Size find_first_not_of(std::string_view text, Size pos={}) const noexcept
First byte that is not in text, at or after pos.
Definition string.hxx:943
const char * const_pointer
Read-only buffer pointer.
Definition string.hxx:119
String & erase(size_type position={}, size_type count=npos)
Erase bytes starting at position.
static String ToUpper(std::string_view str)
ASCII-letter upper case; UTF-8 otherwise copied.
~String() noexcept
Releases a long SDS buffer.
iterator insert(const_iterator pos, std::initializer_list< char > values)
Insert an initializer list before pos.
Size find_last_of(char ch, Size pos=npos) const noexcept
Last ch at or before pos.
Definition string.hxx:933
bool contains(std::string_view text) const noexcept
Whether text occurs.
Definition string.hxx:817
Vector< String > Split() const
Whitespace-separated tokens of this text.
char * pointer
Mutable buffer pointer.
Definition string.hxx:118
const_iterator begin() const noexcept
First character.
Definition string.hxx:244
const char * const_iterator
Contiguous observer.
Definition string.hxx:123
void swap(String &other) noexcept
Swaps storage with other.
bool ends_with(char ch) const noexcept
Whether the text ends with ch.
Definition string.hxx:808
String SanitizeNewlines() const
CR LF to LF on this text.
Definition string.hxx:1163
const_iterator end() const noexcept
One past the last character.
Definition string.hxx:260
Size size() const noexcept
Byte count, excluding the trailing NUL.
static constexpr std::size_t SSO_CAPACITY
Short-text capacity, excluding the trailing NUL.
Definition string.hxx:127
bool starts_with(char ch) const noexcept
Whether the text begins with ch.
Definition string.hxx:790
iterator end() noexcept
One past the last character.
Definition string.hxx:252
const_reverse_iterator rbegin() const noexcept
Reverse begin.
Definition string.hxx:292
String & assign_range(R &&range)
Replace the contents with a copy of range.
Definition string.hxx:461
String & operator+=(std::string_view text)
Append a view.
char & operator[](const Size &index) noexcept
Character at index.
Definition string.hxx:670
Size find(std::string_view text, Size pos={}) const noexcept
First occurrence of text at or after pos.
Definition string.hxx:836
iterator erase(const_iterator first, const_iterator last)
Erase [first, last).
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
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::String &text) const noexcept
Hash text.
Definition string.hxx:1420
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::String &text) const noexcept
Hashes text.
Definition string.hxx:1475
#define STORMBYTE_PUBLIC
Definition visibility.h:52