StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
cstring.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.
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
43
44#include <compare>
45#include <cstddef>
46#include <functional>
47#include <ostream>
48#include <string>
49#include <string_view>
50
55namespace StormByte {
90 public:
99 CString() noexcept;
100
105 explicit CString(const char* str) noexcept;
106
111 CString(const CString& other) noexcept;
112
117 CString(CString&& other) noexcept;
118
122 ~CString() noexcept;
123
129 CString& operator=(const CString& other) noexcept;
130
136 CString& operator=(CString&& other) noexcept;
137
149 void Reset(const char* str = nullptr) noexcept;
150
155 void swap(CString& other) noexcept;
156
168 std::size_t Length() const noexcept;
169
176 char operator[](std::size_t index) const noexcept;
177
183 inline explicit operator bool() const noexcept {
184 return static_cast<const char*>(*this) != nullptr;
185 }
186
199 explicit operator const char*() const noexcept;
200
206 inline explicit operator std::string_view() const noexcept {
207 const char* text = static_cast<const char*>(*this);
208 return text ? std::string_view(text) : std::string_view();
209 }
210
215 inline operator std::string() const {
216 const char* text = static_cast<const char*>(*this);
217 return text ? std::string(text) : std::string();
218 }
219
225 inline std::ostream& operator<<(std::ostream& stream) const {
226 const char* text = static_cast<const char*>(*this);
227 if (text)
228 stream << text;
229 return stream;
230 }
231
244 bool operator==(const CString& other) const noexcept;
245
251 bool operator!=(const CString& other) const noexcept {
252 return !(*this == other);
253 }
254
260 bool operator==(const char* str) const noexcept;
261
267 bool operator!=(const char* str) const noexcept {
268 return !(*this == str);
269 }
270
276 std::strong_ordering operator<=>(const CString& other) const noexcept;
277
283 std::strong_ordering operator<=>(const char* str) const noexcept;
284
287 private:
288 const char* m_data;
289
295 static const char* Duplicate(const char* str) noexcept;
296 };
297
304 inline std::ostream& operator<<(std::ostream& stream, const CString& text) {
305 return text.operator<<(stream);
306 }
307
314 inline bool operator==(const char* str, const CString& text) noexcept {
315 return text == str;
316 }
317
324 inline bool operator!=(const char* str, const CString& text) noexcept {
325 return text != str;
326 }
327
333 inline void swap(CString& left, CString& right) noexcept {
334 left.swap(right);
335 }
336}
337
341template<>
342struct std::hash<StormByte::CString> {
348 std::size_t operator()(const StormByte::CString& text) const noexcept {
349 const char* raw = static_cast<const char*>(text);
350 if (!raw)
351 return 0;
352 return std::hash<std::string_view>{}(raw);
353 }
354};
Owned NUL-terminated buffer, safe to use across a DLL boundary.
Definition cstring.hxx:89
std::strong_ordering operator<=>(const char *str) const noexcept
Content order against a C string.
bool operator==(const char *str) const noexcept
Content equality with a C string.
std::strong_ordering operator<=>(const CString &other) const noexcept
Content order.
bool operator!=(const char *str) const noexcept
Content inequality with a C string.
Definition cstring.hxx:267
std::ostream & operator<<(std::ostream &stream) const
Writes the text to stream.
Definition cstring.hxx:225
bool operator==(const CString &other) const noexcept
Content equality.
void swap(CString &other) noexcept
Swaps buffers with other.
bool operator!=(const CString &other) const noexcept
Content inequality.
Definition cstring.hxx:251
CString() noexcept
Empty (null) buffer.
Root namespace of the StormByte suite.
bool operator!=(const char *str, const CString &text) noexcept
Content inequality.
Definition cstring.hxx:324
std::ostream & operator<<(std::ostream &stream, const CString &text)
Writes text to stream.
Definition cstring.hxx:304
void swap(CString &left, CString &right) noexcept
Swaps two buffers.
Definition cstring.hxx:333
bool operator==(const char *str, const CString &text) noexcept
Content equality.
Definition cstring.hxx:314
STL namespace.
std::size_t operator()(const StormByte::CString &text) const noexcept
Hashes text.
Definition cstring.hxx:348
#define STORMBYTE_PUBLIC
Definition visibility.h:52