StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
string.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
45#include <StormByte/size.hxx>
49
50#if __has_include(<fmt/format.h>)
51#include <fmt/format.h>
52#endif
53
54#include <cassert>
55#include <cctype>
56#include <compare>
57#include <cstddef>
58#include <cstdint>
59#include <format>
60#include <functional>
61#include <initializer_list>
62#include <iterator>
63#include <ostream>
64#include <ranges>
65#include <span>
66#include <string>
67#include <string_view>
68#include <type_traits>
69#include <utility>
70#include <vector>
71
76namespace StormByte {
81 namespace Safe {
86 class WString;
87
114 public:
115 using value_type = char;
116 using size_type = Size;
117 using difference_type = std::ptrdiff_t;
118 using pointer = char*;
119 using const_pointer = const char*;
120 using reference = char&;
121 using const_reference = const char&;
122 using iterator = char*;
123 using const_iterator = const char*;
124 using reverse_iterator = std::reverse_iterator<iterator>;
125 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
126
127 static constexpr std::size_t SSO_CAPACITY = 32;
128
133
137 String() noexcept;
138
143 String(const char* str);
144
150
157 String(size_type count, char character);
158
167 String(const String& other, size_type pos, size_type count = npos);
168
176 template<std::input_iterator InputIt>
177 String(InputIt first, InputIt last): String() {
178 append(first, last);
179 }
180
185 explicit String(const WString& other);
186
191 String(const String& other);
192
197 String(String&& other) noexcept;
198
202 ~String() noexcept;
203
209 String& operator=(const String& other);
210
216 String& operator=(String&& other) noexcept;
217
223 String& operator=(std::string_view text);
224
226
231
236 inline iterator begin() noexcept {
237 return data();
238 }
239
244 inline const_iterator begin() const noexcept {
245 return data();
246 }
247
252 inline iterator end() noexcept {
253 return data() + static_cast<std::size_t>(size());
254 }
255
260 inline const_iterator end() const noexcept {
261 return data() + static_cast<std::size_t>(size());
262 }
263
268 inline const_iterator cbegin() const noexcept {
269 return begin();
270 }
271
276 inline const_iterator cend() const noexcept {
277 return end();
278 }
279
284 inline reverse_iterator rbegin() noexcept {
285 return reverse_iterator(end());
286 }
287
292 inline const_reverse_iterator rbegin() const noexcept {
293 return const_reverse_iterator(end());
294 }
295
300 inline reverse_iterator rend() noexcept {
301 return reverse_iterator(begin());
302 }
303
308 inline const_reverse_iterator rend() const noexcept {
310 }
311
316 inline const_reverse_iterator crbegin() const noexcept {
317 return rbegin();
318 }
319
324 inline const_reverse_iterator crend() const noexcept {
325 return rend();
326 }
327
332 char* data() noexcept;
333
338 const char* data() const noexcept;
339
344 inline const char* c_str() const noexcept {
345 return data();
346 }
347
352 Size size() const noexcept;
353
358 Size capacity() const noexcept;
359
364 Size max_size() const noexcept;
365
370 inline Size length() const noexcept {
371 return size();
372 }
373
378 inline bool empty() const noexcept {
379 return size() == 0;
380 }
381
387 String& append(std::string_view text);
388
395 String& append(size_type count, char character);
396
405 template<std::input_iterator InputIt>
406 String& append(InputIt first, InputIt last) {
407 for (; first != last; ++first)
408 push_back(static_cast<char>(*first));
409 return *this;
410 }
411
419 template<std::ranges::input_range R>
420 String& append_range(R&& range) {
421 return append(std::ranges::begin(range), std::ranges::end(range));
422 }
423
429 String& assign(std::string_view text);
430
437 String& assign(size_type count, char character);
438
447 template<std::input_iterator InputIt>
448 String& assign(InputIt first, InputIt last) {
449 clear();
450 return append(first, last);
451 }
452
460 template<std::ranges::input_range R>
461 String& assign_range(R&& range) {
462 return assign(std::ranges::begin(range), std::ranges::end(range));
463 }
464
470 String& operator+=(std::string_view text);
471
477 String& operator+=(char character);
478
483 void push_back(char character);
484
488 void pop_back();
489
493 void clear();
494
502 void reserve(size_type new_capacity);
503
508
514 void resize(size_type count, char character = char{});
515
522 String& insert(size_type position, std::string_view text);
523
533 String& insert(size_type position, size_type count, char character);
534
545 template<std::input_iterator InputIt>
546 iterator insert(const_iterator pos, InputIt first, InputIt last) {
547 const Size index{static_cast<std::size_t>(pos - cbegin())};
548 String copied;
549 copied.append(first, last);
550 insert(index, static_cast<std::string_view>(copied));
551 return begin() + static_cast<std::size_t>(index);
552 }
553
562 iterator insert(const_iterator pos, std::initializer_list<char> values);
563
573 template<std::ranges::input_range R>
575 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
576 }
577
584 String& erase(size_type position = {}, size_type count = npos);
585
593
602
610 String& replace(size_type position, size_type count, std::string_view text);
611
621 String& replace(const_iterator first, const_iterator last, std::string_view text);
631 template<class... Args>
632 requires (sizeof...(Args) >= 1)
633 static String ReplaceToken(std::string_view text, Args&&... args) {
634 constexpr std::size_t count = sizeof...(Args);
635 ReplaceTokenPlace<char> places[count];
636 const std::size_t found = FindReplaceTokens(text, places, count);
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) {
643 if (index >= found)
644 return;
645 using Bare = std::remove_cvref_t<decltype(value)>;
646 if (!FormatReplaceToken<char>(scratch[index], std::forward<decltype(value)>(value), places[index].spec)) {
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));
652 } else if constexpr (!ReplaceTokenBuiltin<char, Bare>)
653 static_assert(sizeof(Bare) == 0, "ReplaceToken argument must be text, a number, a pointer, or convertible to std::string");
654 }
655 tokens[index] = scratch[index];
656 offsets[index] = places[index].offset;
657 lengths[index] = places[index].length;
658 ++index;
659 };
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));
662 }
663
670 inline char& operator[](const Size& index) noexcept {
671 assert(index <= size());
672 return data()[static_cast<std::size_t>(index)];
673 }
674
681 inline char operator[](const Size& index) const noexcept {
682 assert(index <= size());
683 return data()[static_cast<std::size_t>(index)];
684 }
685
692 char& at(size_type index);
693
700 const char& at(size_type index) const;
701
710 size_type copy(char* dest, size_type count, size_type pos = {}) const;
711
716 inline explicit operator bool() const noexcept {
717 return !empty();
718 }
719
721
726
732 inline operator std::string_view() const noexcept {
733 return std::string_view(data(), static_cast<std::size_t>(size()));
734 }
735
740 STORMBYTE_FORCE_INLINE explicit operator std::string() const {
741 return std::string(static_cast<std::string_view>(*this));
742 }
743
749 inline explicit operator const char*() const noexcept {
750 return c_str();
751 }
752
757 explicit operator WString() const;
758
763 inline const char* Bytes() const noexcept {
764 return data();
765 }
766
768
773
774 static constexpr size_type npos{~0ull};
775
781 inline bool starts_with(std::string_view text) const noexcept {
782 return static_cast<std::string_view>(*this).starts_with(text);
783 }
784
790 inline bool starts_with(char ch) const noexcept {
791 return static_cast<std::string_view>(*this).starts_with(ch);
792 }
793
799 inline bool ends_with(std::string_view text) const noexcept {
800 return static_cast<std::string_view>(*this).ends_with(text);
801 }
802
808 inline bool ends_with(char ch) const noexcept {
809 return static_cast<std::string_view>(*this).ends_with(ch);
810 }
811
817 inline bool contains(std::string_view text) const noexcept {
818 return static_cast<std::string_view>(*this).contains(text);
819 }
820
826 inline bool contains(char ch) const noexcept {
827 return static_cast<std::string_view>(*this).contains(ch);
828 }
829
836 inline Size find(std::string_view text, Size pos = {}) const noexcept {
837 return FindAt(static_cast<std::string_view>(*this), text, pos);
838 }
839
846 inline Size find(char ch, Size pos = {}) const noexcept {
847 return FindAt(static_cast<std::string_view>(*this), ch, pos);
848 }
849
857 inline Size find(const char* text, Size pos, Size count) const noexcept {
858 return find(std::string_view(text, static_cast<std::size_t>(count)), pos);
859 }
860
867 inline Size rfind(std::string_view text, Size pos = npos) const noexcept {
868 return RFindAt(static_cast<std::string_view>(*this), text, pos);
869 }
870
877 inline Size rfind(char ch, Size pos = npos) const noexcept {
878 return RFindAt(static_cast<std::string_view>(*this), ch, pos);
879 }
880
888 inline Size rfind(const char* text, Size pos, Size count) const noexcept {
889 return rfind(std::string_view(text, static_cast<std::size_t>(count)), pos);
890 }
891
898 inline Size find_first_of(std::string_view text, Size pos = {}) const noexcept {
899 const std::string_view self = *this;
900 if (pos > Size{self.size()})
901 return npos;
902 return FromIndex(self.find_first_of(text, static_cast<std::size_t>(pos)));
903 }
904
911 inline Size find_first_of(char ch, Size pos = {}) const noexcept {
912 return find(ch, pos);
913 }
914
921 inline Size find_last_of(std::string_view text, Size pos = npos) const noexcept {
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));
925 }
926
933 inline Size find_last_of(char ch, Size pos = npos) const noexcept {
934 return rfind(ch, pos);
935 }
936
943 inline Size find_first_not_of(std::string_view text, Size pos = {}) const noexcept {
944 const std::string_view self = *this;
945 if (pos > Size{self.size()})
946 return npos;
947 return FromIndex(self.find_first_not_of(text, static_cast<std::size_t>(pos)));
948 }
949
956 inline Size find_first_not_of(char ch, Size pos = {}) const noexcept {
957 const std::string_view self = *this;
958 if (pos > Size{self.size()})
959 return npos;
960 return FromIndex(self.find_first_not_of(ch, static_cast<std::size_t>(pos)));
961 }
962
969 inline Size find_last_not_of(std::string_view text, Size pos = npos) const noexcept {
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));
973 }
974
981 inline Size find_last_not_of(char ch, Size pos = npos) const noexcept {
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));
985 }
986
993 inline String substr(Size pos = {}, Size count = npos) const noexcept {
994 const std::string_view text = *this;
995 if (pos > Size{text.size()})
996 return String();
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));
999 }
1000
1006 inline int compare(std::string_view text) const noexcept {
1007 return static_cast<std::string_view>(*this).compare(text);
1008 }
1009
1017 inline int compare(size_type pos, size_type count, std::string_view text) const noexcept {
1018 return static_cast<std::string_view>(substr(pos, count)).compare(text);
1019 }
1020
1026 inline char& front() noexcept {
1027 return (*this)[Size{0}];
1028 }
1029
1035 inline char front() const noexcept {
1036 return (*this)[Size{0}];
1037 }
1038
1044 inline char& back() noexcept {
1045 return (*this)[size() - Size{1}];
1046 }
1047
1053 inline char back() const noexcept {
1054 return (*this)[size() - Size{1}];
1055 }
1056
1058
1063
1069 static String ToLower(std::string_view str);
1070
1076 static String ToUpper(std::string_view str);
1077
1083 static String SanitizeNewlines(std::string_view str);
1084
1090 static String RemoveWhitespace(std::string_view str);
1091
1097 static bool IsInteger(std::string_view str) noexcept;
1098
1104 static Vector<String> Split(std::string_view str);
1105
1111 static inline Vector<String> Split(const std::vector<String>& parts) {
1112 Vector<String> result;
1113 for (const String& part : parts)
1114 result.push_back(part);
1115 return result;
1116 }
1117
1124 static Queue<String> Explode(std::string_view str, char delimiter);
1125
1126
1133 static String Implode(const Vector<String>& parts, std::string_view delimiter);
1134
1141 static String Implode(const Queue<String>& parts, std::string_view delimiter);
1142
1147 inline String ToLower() const {
1148 return ToLower(static_cast<std::string_view>(*this));
1149 }
1150
1155 inline String ToUpper() const {
1156 return ToUpper(static_cast<std::string_view>(*this));
1157 }
1158
1163 inline String SanitizeNewlines() const {
1164 return SanitizeNewlines(static_cast<std::string_view>(*this));
1165 }
1166
1171 inline String RemoveWhitespace() const {
1172 return RemoveWhitespace(static_cast<std::string_view>(*this));
1173 }
1174
1179 inline bool IsInteger() const noexcept {
1180 return IsInteger(static_cast<std::string_view>(*this));
1181 }
1182
1188
1194 Queue<String> Explode(char delimiter) const;
1195
1197
1202
1208 inline bool operator==(const String& other) const noexcept {
1209 return static_cast<std::string_view>(*this) == static_cast<std::string_view>(other);
1210 }
1211
1217 inline bool operator!=(const String& other) const noexcept {
1218 return !(*this == other);
1219 }
1220
1226 inline bool operator==(const char* str) const noexcept {
1227 return str ? static_cast<std::string_view>(*this) == std::string_view(str) : empty();
1228 }
1229
1235 inline bool operator!=(const char* str) const noexcept {
1236 return !(*this == str);
1237 }
1238
1244 STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const String& other) const noexcept {
1245 return static_cast<std::string_view>(*this) <=> static_cast<std::string_view>(other);
1246 }
1247
1253 STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const char* str) const noexcept {
1254 return str ? static_cast<std::string_view>(*this) <=> std::string_view(str)
1255 : static_cast<std::string_view>(*this) <=> std::string_view{};
1256 }
1257
1259
1264 void swap(String& other) noexcept;
1265
1266 private:
1273 struct alignas(16) Storage {
1274 union {
1275 char data[SSO_CAPACITY + 1];
1276 char* long_;
1277 };
1278 std::uint8_t m_tag;
1279 };
1280
1281 static_assert(sizeof(Storage) == 48);
1282 static_assert(alignof(Storage) == 16);
1283
1288 bool IsLong() const noexcept;
1289
1293 void ReleaseLong() noexcept;
1294
1298 void SetEmpty() noexcept;
1299
1305 void SetShort(const char* text, std::size_t count) noexcept;
1306
1312 static Size FromIndex(std::size_t index) noexcept {
1313 return index == std::string_view::npos ? npos : Size{index};
1314 }
1315
1324 template<typename Needle>
1325 static Size FindAt(std::string_view self, Needle needle, Size pos) noexcept {
1326 if (pos > Size{self.size()})
1327 return npos;
1328 return FromIndex(self.find(needle, static_cast<std::size_t>(pos)));
1329 }
1330
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));
1343 }
1344
1345
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{};
1357 };
1358
1365 STORMBYTE_FORCE_INLINE std::ostream& operator<<(std::ostream& stream, const String& text) {
1366 return stream << static_cast<std::string_view>(text);
1367 }
1368
1375 inline bool operator==(const char* str, const String& text) noexcept {
1376 return text == str;
1377 }
1378
1385 inline bool operator!=(const char* str, const String& text) noexcept {
1386 return text != str;
1387 }
1388
1396 inline String operator+(const String& left, const String& right) {
1397 String result(left);
1398 result += right;
1399 return result;
1400 }
1401
1407 inline void swap(String& left, String& right) noexcept {
1408 left.swap(right);
1409 }
1410 }
1411}
1412
1413template<>
1420 STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::String& text) const noexcept {
1421 const std::string_view view = text;
1422 return HashBytes(std::as_bytes(std::span<const char>(view.data(), view.size())));
1423 }
1424};
1425
1429template<>
1430struct std::formatter<StormByte::Safe::String, char>: std::formatter<std::string_view, char> {
1439 template<typename FormatContext>
1440 STORMBYTE_FORCE_INLINE auto format(const StormByte::Safe::String& text, FormatContext& context) const {
1441 const std::string_view view = text;
1442 return std::formatter<std::string_view, char>::format(view, context);
1443 }
1444};
1445
1446#if __has_include(<fmt/format.h>)
1450template<>
1451struct fmt::formatter<StormByte::Safe::String, char>: fmt::formatter<std::string_view, char> {
1460 template<typename FormatContext>
1461 STORMBYTE_FORCE_INLINE auto format(const StormByte::Safe::String& text, FormatContext& context) const {
1462 const std::string_view view = text;
1463 return fmt::formatter<std::string_view, char>::format(view, context);
1464 }
1465};
1466#endif
1467
1468template<>
1469struct std::hash<StormByte::Safe::String> {
1475 std::size_t operator()(const StormByte::Safe::String& text) const noexcept {
1477 }
1478};
1479
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.
STL namespace.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
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
STORMBYTE_FORCE_INLINE auto format(const StormByte::Safe::String &text, FormatContext &context) const
Formats the borrowed view without copying the owned text.
Definition string.hxx:1440
std::size_t operator()(const StormByte::Safe::String &text) const noexcept
Hashes text.
Definition string.hxx:1475
#define STORMBYTE_PUBLIC
Definition visibility.h:52