StormByte-String 1.0.0
C++26 string module of the StormByte suite
 
Loading...
Searching...
No Matches
wstring.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
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 String;
71
92 public:
93 using value_type = wchar_t;
94 using const_iterator = const wchar_t*;
95 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
96
105 WString() noexcept;
106
111 explicit WString(const wchar_t* str) noexcept;
112
117 explicit WString(std::wstring_view str) noexcept;
118
123 explicit WString(WCString text) noexcept;
124
129 explicit WString(const String& other) noexcept;
130
135 WString(const WString& other) noexcept;
136
141 WString(WString&& other) noexcept;
142
146 ~WString() noexcept = default;
147
153 WString& operator=(const WString& other) noexcept;
154
160 WString& operator=(WString&& other) noexcept;
161
173 inline const_iterator begin() const noexcept {
174 return data();
175 }
176
181 inline const_iterator end() const noexcept {
182 const wchar_t* text = data();
183 return text ? text + size() : nullptr;
184 }
185
190 inline const_iterator cbegin() const noexcept {
191 return begin();
192 }
193
198 inline const_iterator cend() const noexcept {
199 return end();
200 }
201
206 inline const_reverse_iterator rbegin() const noexcept {
207 return const_reverse_iterator(end());
208 }
209
214 inline const_reverse_iterator rend() const noexcept {
215 return const_reverse_iterator(begin());
216 }
217
222 inline const_reverse_iterator crbegin() const noexcept {
223 return rbegin();
224 }
225
230 inline const_reverse_iterator crend() const noexcept {
231 return rend();
232 }
233
238 inline const wchar_t* data() const noexcept {
239 return static_cast<const wchar_t*>(m_text);
240 }
241
246 inline std::size_t size() const noexcept {
247 return m_text.Length();
248 }
249
254 inline std::size_t length() const noexcept {
255 return size();
256 }
257
262 inline bool empty() const noexcept {
263 return size() == 0;
264 }
265
272 inline wchar_t operator[](std::size_t index) const noexcept {
273 return m_text[index];
274 }
275
280 inline explicit operator bool() const noexcept {
281 return static_cast<bool>(m_text);
282 }
283
296 inline operator std::wstring_view() const noexcept {
297 return static_cast<std::wstring_view>(m_text);
298 }
299
304 inline explicit operator std::wstring() const {
305 return static_cast<std::wstring>(m_text);
306 }
307
313 inline explicit operator const wchar_t*() const noexcept {
314 return static_cast<const wchar_t*>(m_text);
315 }
316
321 explicit operator String() const noexcept;
322
327 inline const WCString& Bytes() const noexcept {
328 return m_text;
329 }
330
343 static WString ToLower(std::wstring_view str) noexcept;
344
350 static WString ToUpper(std::wstring_view str) noexcept;
351
357 static WString SanitizeNewlines(std::wstring_view str) noexcept;
358
364 static WString RemoveWhitespace(std::wstring_view str) noexcept;
365
371 static bool IsInteger(std::wstring_view str) noexcept;
372
378 static void Split(std::wstring_view str, std::vector<WString>& out) noexcept;
379
386 static void Explode(std::wstring_view str, wchar_t delimiter, std::queue<WString>& out) noexcept;
387
392 inline WString ToLower() const noexcept {
393 return ToLower(static_cast<std::wstring_view>(*this));
394 }
395
400 inline WString ToUpper() const noexcept {
401 return ToUpper(static_cast<std::wstring_view>(*this));
402 }
403
408 inline WString SanitizeNewlines() const noexcept {
409 return SanitizeNewlines(static_cast<std::wstring_view>(*this));
410 }
411
416 inline WString RemoveWhitespace() const noexcept {
417 return RemoveWhitespace(static_cast<std::wstring_view>(*this));
418 }
419
424 inline bool IsInteger() const noexcept {
425 return IsInteger(static_cast<std::wstring_view>(*this));
426 }
427
432 inline std::vector<WString> Split() const noexcept {
433 std::vector<WString> out;
434 Split(static_cast<std::wstring_view>(*this), out);
435 return out;
436 }
437
443 inline std::queue<WString> Explode(wchar_t delimiter) const noexcept {
444 std::queue<WString> out;
445 Explode(static_cast<std::wstring_view>(*this), delimiter, out);
446 return out;
447 }
448
461 inline bool operator==(const WString& other) const noexcept {
462 return m_text == other.m_text;
463 }
464
470 inline bool operator!=(const WString& other) const noexcept {
471 return !(*this == other);
472 }
473
479 inline bool operator==(const wchar_t* str) const noexcept {
480 return m_text == str;
481 }
482
488 inline bool operator!=(const wchar_t* str) const noexcept {
489 return !(*this == str);
490 }
491
497 inline std::strong_ordering operator<=>(const WString& other) const noexcept {
498 return m_text <=> other.m_text;
499 }
500
506 inline std::strong_ordering operator<=>(const wchar_t* str) const noexcept {
507 return m_text <=> str;
508 }
509
516 void swap(WString& other) noexcept;
517
518 private:
519 WCString m_text;
520 };
521
528 inline std::wostream& operator<<(std::wostream& stream, const WString& text) {
529 return stream << text.Bytes();
530 }
531
538 inline bool operator==(const wchar_t* str, const WString& text) noexcept {
539 return text == str;
540 }
541
548 inline bool operator!=(const wchar_t* str, const WString& text) noexcept {
549 return text != str;
550 }
551
557 inline void swap(WString& left, WString& right) noexcept {
558 left.swap(right);
559 }
560 }
561}
562
566template<>
567struct std::hash<StormByte::String::WString> {
573 std::size_t operator()(const StormByte::String::WString& text) const noexcept {
574 return std::hash<std::wstring_view>{}(static_cast<std::wstring_view>(text));
575 }
576};
Wide counterpart of String.
Definition wstring.hxx:91
std::vector< WString > Split() const noexcept
Whitespace-separated tokens.
Definition wstring.hxx:432
std::size_t size() const noexcept
Code-unit count; 0 when null or empty.
Definition wstring.hxx:246
const_reverse_iterator rbegin() const noexcept
Reverse begin.
Definition wstring.hxx:206
const_iterator cend() const noexcept
One past the last character, or null.
Definition wstring.hxx:198
const wchar_t * data() const noexcept
Contiguous pointer; null when the buffer is null.
Definition wstring.hxx:238
WString ToUpper() const noexcept
ASCII-letter upper case of this text.
Definition wstring.hxx:400
std::strong_ordering operator<=>(const WString &other) const noexcept
Content order.
Definition wstring.hxx:497
WString ToLower() const noexcept
ASCII-letter lower case of this text.
Definition wstring.hxx:392
static bool IsInteger(std::wstring_view str) noexcept
Optional sign plus ASCII digits.
bool operator!=(const WString &other) const noexcept
Content inequality.
Definition wstring.hxx:470
bool operator==(const wchar_t *str) const noexcept
Content equality with a wide C string.
Definition wstring.hxx:479
std::size_t length() const noexcept
Same as size.
Definition wstring.hxx:254
bool operator!=(const wchar_t *str) const noexcept
Content inequality with a wide C string.
Definition wstring.hxx:488
const_reverse_iterator rend() const noexcept
Reverse end.
Definition wstring.hxx:214
static void Explode(std::wstring_view str, wchar_t delimiter, std::queue< WString > &out) noexcept
Tokens on delimiter.
std::strong_ordering operator<=>(const wchar_t *str) const noexcept
Content order against a wide C string.
Definition wstring.hxx:506
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse observer.
Definition wstring.hxx:95
WString SanitizeNewlines() const noexcept
CR LF to LF on this text.
Definition wstring.hxx:408
bool empty() const noexcept
Whether size is zero.
Definition wstring.hxx:262
bool operator==(const WString &other) const noexcept
Content equality.
Definition wstring.hxx:461
static WString ToLower(std::wstring_view str) noexcept
ASCII-letter lower case; other code points copied.
void swap(WString &other) noexcept
Swaps buffers with other.
const wchar_t * const_iterator
Contiguous observer.
Definition wstring.hxx:94
const_reverse_iterator crbegin() const noexcept
Reverse begin.
Definition wstring.hxx:222
static WString SanitizeNewlines(std::wstring_view str) noexcept
Turns CR LF into LF.
static WString RemoveWhitespace(std::wstring_view str) noexcept
Drops iswspace code units.
const WCString & Bytes() const noexcept
Owned code units.
Definition wstring.hxx:327
wchar_t operator[](std::size_t index) const noexcept
Code unit at index.
Definition wstring.hxx:272
WString RemoveWhitespace() const noexcept
Drops iswspace code units from this text.
Definition wstring.hxx:416
const_reverse_iterator crend() const noexcept
Reverse end.
Definition wstring.hxx:230
static WString ToUpper(std::wstring_view str) noexcept
ASCII-letter upper case; other code points copied.
const_iterator cbegin() const noexcept
First character, or null.
Definition wstring.hxx:190
bool IsInteger() const noexcept
Whether this text is an integer token.
Definition wstring.hxx:424
static void Split(std::wstring_view str, std::vector< WString > &out) noexcept
Whitespace-separated tokens.
wchar_t value_type
Code unit type.
Definition wstring.hxx:93
WString() noexcept
Null text.
const_iterator end() const noexcept
One past the last character, or null.
Definition wstring.hxx:181
std::queue< WString > Explode(wchar_t delimiter) const noexcept
Tokens on delimiter.
Definition wstring.hxx:443
void swap(WCString &other) noexcept
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::WString &text) const noexcept
Hashes text.
Definition wstring.hxx:573
#define STORMBYTE_STRING_PUBLIC
Definition visibility.h:53