StormByte-String 1.0.0
C++26 string module 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 file is part of StormByte-String.
5 *
6 * StormByte-String 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-String source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte Base tree),
23 * which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-String is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-String. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/cstring.hxx>
45
46#include <compare>
47#include <cstddef>
48#include <functional>
49#include <iterator>
50#include <ostream>
51#include <queue>
52#include <string>
53#include <string_view>
54#include <vector>
55
60namespace StormByte {
65 namespace String {
70 class WString;
71
93 public:
94 using value_type = char;
95 using const_iterator = const char*;
96 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
97
106 String() noexcept;
107
112 explicit String(const char* str) noexcept;
113
118 explicit String(std::string_view str) noexcept;
119
124 explicit String(CString text) noexcept;
125
130 explicit String(const WString& other) noexcept;
131
136 String(const String& other) noexcept;
137
142 String(String&& other) noexcept;
143
147 ~String() noexcept = default;
148
154 String& operator=(const String& other) noexcept;
155
161 String& operator=(String&& other) noexcept;
162
174 inline const_iterator begin() const noexcept {
175 return data();
176 }
177
182 inline const_iterator end() const noexcept {
183 const char* text = data();
184 return text ? text + size() : nullptr;
185 }
186
191 inline const_iterator cbegin() const noexcept {
192 return begin();
193 }
194
199 inline const_iterator cend() const noexcept {
200 return end();
201 }
202
207 inline const_reverse_iterator rbegin() const noexcept {
208 return const_reverse_iterator(end());
209 }
210
215 inline const_reverse_iterator rend() const noexcept {
216 return const_reverse_iterator(begin());
217 }
218
223 inline const_reverse_iterator crbegin() const noexcept {
224 return rbegin();
225 }
226
231 inline const_reverse_iterator crend() const noexcept {
232 return rend();
233 }
234
239 inline const char* data() const noexcept {
240 return static_cast<const char*>(m_text);
241 }
242
247 inline std::size_t size() const noexcept {
248 return m_text.Length();
249 }
250
255 inline std::size_t length() const noexcept {
256 return size();
257 }
258
263 inline bool empty() const noexcept {
264 return size() == 0;
265 }
266
273 inline char operator[](std::size_t index) const noexcept {
274 return m_text[index];
275 }
276
281 inline explicit operator bool() const noexcept {
282 return static_cast<bool>(m_text);
283 }
284
297 inline operator std::string_view() const noexcept {
298 return static_cast<std::string_view>(m_text);
299 }
300
305 inline explicit operator std::string() const {
306 return static_cast<std::string>(m_text);
307 }
308
314 inline explicit operator const char*() const noexcept {
315 return static_cast<const char*>(m_text);
316 }
317
322 explicit operator WString() const noexcept;
323
328 inline const CString& Bytes() const noexcept {
329 return m_text;
330 }
331
344 static String ToLower(std::string_view str) noexcept;
345
351 static String ToUpper(std::string_view str) noexcept;
352
358 static String SanitizeNewlines(std::string_view str) noexcept;
359
365 static String RemoveWhitespace(std::string_view str) noexcept;
366
372 static bool IsInteger(std::string_view str) noexcept;
373
379 static void Split(std::string_view str, std::vector<String>& out) noexcept;
380
387 static void Explode(std::string_view str, char delimiter, std::queue<String>& out) noexcept;
388
393 inline String ToLower() const noexcept {
394 return ToLower(static_cast<std::string_view>(*this));
395 }
396
401 inline String ToUpper() const noexcept {
402 return ToUpper(static_cast<std::string_view>(*this));
403 }
404
409 inline String SanitizeNewlines() const noexcept {
410 return SanitizeNewlines(static_cast<std::string_view>(*this));
411 }
412
417 inline String RemoveWhitespace() const noexcept {
418 return RemoveWhitespace(static_cast<std::string_view>(*this));
419 }
420
425 inline bool IsInteger() const noexcept {
426 return IsInteger(static_cast<std::string_view>(*this));
427 }
428
433 inline std::vector<String> Split() const noexcept {
434 std::vector<String> out;
435 Split(static_cast<std::string_view>(*this), out);
436 return out;
437 }
438
444 inline std::queue<String> Explode(char delimiter) const noexcept {
445 std::queue<String> out;
446 Explode(static_cast<std::string_view>(*this), delimiter, out);
447 return out;
448 }
449
462 inline bool operator==(const String& other) const noexcept {
463 return m_text == other.m_text;
464 }
465
471 inline bool operator!=(const String& other) const noexcept {
472 return !(*this == other);
473 }
474
480 inline bool operator==(const char* str) const noexcept {
481 return m_text == str;
482 }
483
489 inline bool operator!=(const char* str) const noexcept {
490 return !(*this == str);
491 }
492
498 inline std::strong_ordering operator<=>(const String& other) const noexcept {
499 return m_text <=> other.m_text;
500 }
501
507 inline std::strong_ordering operator<=>(const char* str) const noexcept {
508 return m_text <=> str;
509 }
510
517 void swap(String& other) noexcept;
518
519 private:
520 CString m_text;
521 };
522
529 inline std::ostream& operator<<(std::ostream& stream, const String& text) {
530 return stream << text.Bytes();
531 }
532
539 inline bool operator==(const char* str, const String& text) noexcept {
540 return text == str;
541 }
542
549 inline bool operator!=(const char* str, const String& text) noexcept {
550 return text != str;
551 }
552
558 inline void swap(String& left, String& right) noexcept {
559 left.swap(right);
560 }
561 }
562}
563
567template<>
568struct std::hash<StormByte::String::String> {
574 std::size_t operator()(const StormByte::String::String& text) const noexcept {
575 return std::hash<std::string_view>{}(static_cast<std::string_view>(text));
576 }
577};
Owned UTF-8 text composed of StormByte::CString.
Definition string.hxx:92
std::strong_ordering operator<=>(const String &other) const noexcept
Content order.
Definition string.hxx:498
char value_type
Byte type.
Definition string.hxx:94
std::size_t length() const noexcept
Same as size.
Definition string.hxx:255
std::vector< String > Split() const noexcept
Whitespace-separated tokens.
Definition string.hxx:433
const_iterator cbegin() const noexcept
First character, or null.
Definition string.hxx:191
String ToUpper() const noexcept
ASCII-letter upper case of this text.
Definition string.hxx:401
bool operator!=(const String &other) const noexcept
Content inequality.
Definition string.hxx:471
static String RemoveWhitespace(std::string_view str) noexcept
Drops isspace bytes.
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse observer.
Definition string.hxx:96
std::queue< String > Explode(char delimiter) const noexcept
Tokens on delimiter.
Definition string.hxx:444
bool empty() const noexcept
Whether size is zero.
Definition string.hxx:263
static String ToLower(std::string_view str) noexcept
ASCII-letter lower case; UTF-8 otherwise copied.
void swap(String &other) noexcept
Swaps buffers with other.
static String ToUpper(std::string_view str) noexcept
ASCII-letter upper case; UTF-8 otherwise copied.
bool operator==(const String &other) const noexcept
Content equality.
Definition string.hxx:462
const_reverse_iterator rbegin() const noexcept
Reverse begin.
Definition string.hxx:207
String ToLower() const noexcept
ASCII-letter lower case of this text.
Definition string.hxx:393
const_iterator end() const noexcept
One past the last character, or null.
Definition string.hxx:182
static void Split(std::string_view str, std::vector< String > &out) noexcept
Whitespace-separated tokens.
static bool IsInteger(std::string_view str) noexcept
Optional sign plus ASCII digits.
bool operator==(const char *str) const noexcept
Content equality with a C string.
Definition string.hxx:480
static void Explode(std::string_view str, char delimiter, std::queue< String > &out) noexcept
Tokens on delimiter.
String() noexcept
Null text.
String RemoveWhitespace() const noexcept
Drops isspace bytes from this text.
Definition string.hxx:417
std::size_t size() const noexcept
Byte count; 0 when null or empty.
Definition string.hxx:247
String SanitizeNewlines() const noexcept
CR LF to LF on this text.
Definition string.hxx:409
const_iterator cend() const noexcept
One past the last character, or null.
Definition string.hxx:199
std::strong_ordering operator<=>(const char *str) const noexcept
Content order against a C string.
Definition string.hxx:507
const char * const_iterator
Contiguous observer.
Definition string.hxx:95
static String SanitizeNewlines(std::string_view str) noexcept
Turns CR LF into LF.
const char * data() const noexcept
Contiguous pointer; null when the buffer is null.
Definition string.hxx:239
char operator[](std::size_t index) const noexcept
Byte at index.
Definition string.hxx:273
const_reverse_iterator rend() const noexcept
Reverse end.
Definition string.hxx:215
bool operator!=(const char *str) const noexcept
Content inequality with a C string.
Definition string.hxx:489
bool IsInteger() const noexcept
Whether this text is an integer token.
Definition string.hxx:425
const_reverse_iterator crend() const noexcept
Reverse end.
Definition string.hxx:231
const_reverse_iterator crbegin() const noexcept
Reverse begin.
Definition string.hxx:223
Wide counterpart of String.
Definition wstring.hxx:91
void swap(String &left, String &right) noexcept
Swaps two texts.
Definition string.hxx:558
std::ostream & operator<<(std::ostream &stream, const String &text)
Writes text to stream.
Definition string.hxx:529
bool operator==(const char *str, const String &text) noexcept
Content equality.
Definition string.hxx:539
bool operator!=(const char *str, const String &text) noexcept
Content inequality.
Definition string.hxx:549
Root namespace of the StormByte suite.
String module: owned UTF-8 and wide text on top of StormByte::CString / StormByte::WCString.
STL namespace.
std::size_t operator()(const StormByte::String::String &text) const noexcept
Hashes text.
Definition string.hxx:574
#define STORMBYTE_STRING_PUBLIC
Definition visibility.h:53