Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
String.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/StringSpan.h"
5#include "../Memory/Buffer.h"
6
7namespace SC
8{
9template <int N>
10struct SmallString;
11namespace Reflection
12{
13template <typename T>
14struct Reflect;
15}
16
19
26struct SC_COMPILER_EXPORT String
27{
30 String(StringEncoding encoding = StringEncoding::Utf8) : encoding(encoding) {}
31
35 String(StringSpan sv) { SC_ASSERT_RELEASE(assign(sv)); }
36
39 String(Buffer&& otherData, StringEncoding encoding);
40
45 template <size_t N>
46 String(const char (&text)[N])
47 {
48 SC_ASSERT_RELEASE(assign(StringSpan({text, N - 1}, true, StringEncoding::Ascii)));
49 }
50
54 [[nodiscard]] bool owns(StringSpan view) const;
55
60 [[nodiscard]] bool assign(StringSpan sv);
61
64 [[nodiscard]] StringEncoding getEncoding() const { return encoding; }
65
68 [[nodiscard]] size_t sizeInBytesIncludingTerminator() const { return data.size(); }
69
72 [[nodiscard]] const char* bytesIncludingTerminator() const { return data.data(); }
73
76 [[nodiscard]] bool isEmpty() const { return data.isEmpty(); }
77
80 [[nodiscard]] StringSpan view() const SC_LANGUAGE_LIFETIME_BOUND;
81
85 [[nodiscard]] bool operator==(const String& other) const { return view() == (other.view()); }
86
90 [[nodiscard]] bool operator!=(const String& other) const { return not operator==(other); }
91
95 [[nodiscard]] bool operator==(const StringSpan other) const { return view() == (other); }
96
100 [[nodiscard]] bool operator!=(const StringSpan other) const { return not operator==(other); }
101
106 template <size_t N>
107 [[nodiscard]] bool operator==(const char (&other)[N]) const
108 {
109 return view() == other;
110 }
115 template <size_t N>
116 [[nodiscard]] bool operator!=(const char (&other)[N]) const
117 {
118 return view() != other;
119 }
120
124 [[nodiscard]] bool operator<(const StringSpan other) const { return view() < other; }
125
131 template <size_t N>
132 String& operator=(const char (&text)[N])
133 {
134 SC_ASSERT_RELEASE(assign(StringSpan({text, N - 1}, true, StringEncoding::Ascii)));
135 return *this;
136 }
137
141
142 protected:
143 struct Internal;
144
145 friend struct StringTest;
146 template <typename T>
147 friend struct Reflection::Reflect;
148
149 template <typename T>
150 friend struct GrowableBuffer;
151 struct SC_COMPILER_EXPORT GrowableImplementation
152 {
153 String& string;
154
155 IGrowableBuffer::DirectAccess& da;
156
157 GrowableImplementation(String& string, IGrowableBuffer::DirectAccess& da) noexcept;
158 ~GrowableImplementation() noexcept;
159 void finalize() noexcept;
160 bool tryGrowTo(size_t newSize) noexcept;
161 };
162
163 StringEncoding encoding;
164 Buffer data;
165
166 String(StringEncoding encoding, uint32_t inlineCapacity);
167 String(Buffer&& otherData, StringEncoding encoding, uint32_t inlineCapacity);
168};
169
172template <int N>
173struct SC_COMPILER_EXPORT SmallString : public String
174{
175 // Unfortunately we have to repeat all these overloads to set inline capacity and hasInlineData flag
176 SmallString(StringEncoding encoding = StringEncoding::Utf8) : String(encoding, N) {}
177 SmallString(const SmallString& other) : SmallString(other.getEncoding()) { String::operator=(other); }
178 SmallString(SmallString&& other) : SmallString(other.getEncoding()) { String::operator=(move(other)); }
179 String& operator=(const SmallString& other) { return String::operator=(other); }
180 String& operator=(SmallString&& other) { return String::operator=(move(other)); }
181
182 SmallString(const String& other) : SmallString(other.getEncoding()) { String::operator=(other); }
183 SmallString(String&& other) : SmallString(other.getEncoding()) { String::operator=(move(other)); }
184 SmallString(StringSpan other) : SmallString(other.getEncoding()) { String::operator=(move(other)); }
185 SmallString(Buffer&& otherData, StringEncoding encoding) : String(move(otherData), encoding, N) {}
186 template <size_t Q>
187 SmallString(const char (&text)[Q]) : SmallString(StringSpan({text, Q - 1}, true, StringEncoding::Ascii))
188 {}
189
190 private:
191 uint64_t inlineCapacity = N;
192 char buffer[N];
193};
195
196template <int N>
197using SmallStringNative = SmallString<N * sizeof(native_char_t)>;
198
199// Enables File library from reading data from file descriptor into a String
200template <>
201struct SC_COMPILER_EXPORT GrowableBuffer<String> : public IGrowableBuffer
202{
204 GrowableBuffer(String& string)
205 : IGrowableBuffer(&GrowableBuffer::tryGrowTo), gi(string, IGrowableBuffer::directAccess)
206 {}
207 static bool tryGrowTo(IGrowableBuffer& gb, size_t newSize) noexcept
208 {
209 return static_cast<GrowableBuffer&>(gb).gi.tryGrowTo(newSize);
210 }
211 static auto getEncodingFor(const String& str) noexcept { return str.getEncoding(); }
212 void finalize() noexcept { gi.finalize(); }
213};
214
215template <int N>
216struct SC_COMPILER_EXPORT GrowableBuffer<SmallString<N>> : public IGrowableBuffer
217{
219 GrowableBuffer(String& string)
220 : IGrowableBuffer(&GrowableBuffer::tryGrowTo), gi(string, IGrowableBuffer::directAccess)
221 {}
222 static bool tryGrowTo(IGrowableBuffer& gb, size_t newSize) noexcept
223 {
224 return static_cast<GrowableBuffer&>(gb).gi.tryGrowTo(newSize);
225 }
226 static auto getEncodingFor(const SmallString<N>& str) noexcept { return str.getEncoding(); }
227 void finalize() noexcept { gi.finalize(); }
228};
229} // namespace SC
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:46
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:264
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
char native_char_t
The native char for the platform (wchar_t (4 bytes) on Windows, char (1 byte) everywhere else )
Definition PrimitiveTypes.h:34
An heap allocated byte buffer that can optionally use an inline buffer.
Definition Buffer.h:29
String with compile time configurable inline storage (small string optimization)
Definition String.h:174
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
Definition String.h:152
A non-modifiable owning string with associated encoding.
Definition String.h:27
StringEncoding getEncoding() const
Get StringSpan encoding.
Definition String.h:64
bool operator!=(const String &other) const
Check if current String is different from other String.
Definition String.h:90
bool operator<(const StringSpan other) const
Check if current String is smaller to another StringView (using StringView::compare)
Definition String.h:124
bool operator!=(const char(&other)[N]) const
Check if current String is different from the ascii string literal.
Definition String.h:116
StringSpan view() const SC_LANGUAGE_LIFETIME_BOUND
Obtain a null-terminated StringSpan from current String.
String(const char(&text)[N])
Builds String with a null terminated char string literal.
Definition String.h:46
bool owns(StringSpan view) const
Checks if the memory pointed by the StringSpan is owned by this String.
size_t sizeInBytesIncludingTerminator() const
Get length of the string in bytes (including null terminator bytes)
Definition String.h:68
String(StringEncoding encoding=StringEncoding::Utf8)
Builds an empty String with a given Encoding.
Definition String.h:30
bool operator!=(const StringSpan other) const
Check if current String is different from other StringSpan.
Definition String.h:100
String & operator=(const char(&text)[N])
Assigns an ascii string literal to current String.
Definition String.h:132
bool assign(StringSpan sv)
Assigns a StringSpan to this String, replacing existing contents.
bool operator==(const StringSpan other) const
Check if current String is same as other StringSpan.
Definition String.h:95
String & operator=(StringSpan view)
Assigns (copy) contents of given StringSpan in current String.
bool isEmpty() const
Check if String is empty.
Definition String.h:76
const char * bytesIncludingTerminator() const
Access current string content as read-only null-terminated const char*
Definition String.h:72
String(Buffer &&otherData, StringEncoding encoding)
Builds a String from a buffer ensuring zero termination.
String(StringSpan sv)
Builds String from a StringSpan.
Definition String.h:35
bool operator==(const char(&other)[N]) const
Check if current String is equal to the ascii string literal.
Definition String.h:107