Sane C++ Libraries
C++ Platform Abstraction Libraries
SmallString.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Array.h"
5#include "../Strings/String.h"
6#include "../Strings/StringConverter.h" // ensureZeroTermination
7
8namespace SC
9{
10template <int N>
11struct SmallString;
12} // namespace SC
13
16
19template <int N>
20struct SC::SmallString : public String
21{
22 Array<char, N> buffer;
23
24 SmallString(StringEncoding encoding = StringEncoding::Utf8) : String(encoding) { init(); }
25
27 {
28 init();
30 static_assert(SC_COMPILER_OFFSETOF(SmallString<1>, buffer) - SC_COMPILER_OFFSETOF(String, data) ==
31 alignof(SegmentHeader),
32 "Wrong alignment");
35 }
36
37 template <size_t Q>
38 SmallString(const char (&text)[Q])
39 {
40 init();
42 }
43
44 SmallString(Vector<char>&& data, StringEncoding encoding) : String(encoding)
45 {
46 init();
47 SC_ASSERT_RELEASE(StringConverter::ensureZeroTermination(data, encoding));
48 String::data = move(data);
49 }
50
51 SmallString(SmallString&& other) : String(other.encoding)
52 {
53 init();
54 String::data = move(other.data);
55 }
56
57 SmallString(const SmallString& other) : String(other.encoding)
58 {
59 init();
60 String::data = other.data;
61 }
62
63 SmallString& operator=(SmallString&& other) noexcept
64 {
65 String::operator=(forward<String>(other));
66 return *this;
67 }
68
69 SmallString& operator=(const SmallString& other)
70 {
71 String::operator=(other);
72 return *this;
73 }
74
75 SmallString& operator=(StringView other)
76 {
78 return *this;
79 }
80
81 SmallString(String&& other) : String(forward<String>(other)) {}
82 SmallString(const String& other) : String(other) {}
83 SmallString& operator=(String&& other)
84 {
85 String::operator=(forward<String>(other));
86 return *this;
87 }
88 SmallString& operator=(const String& other)
89 {
90 String::operator=(other);
91 return *this;
92 }
93 template <size_t Q>
94 SmallString& operator=(const char (&text)[Q])
95 {
97 return *this;
98 }
99
100 private:
101 void init()
102 {
103 SegmentHeader* header = SegmentHeader::getSegmentHeader(buffer.items);
104 header->isSmallVector = true;
105 String::data.items = buffer.items;
106 }
107};
109
110namespace SC
111{
112template <int N>
113using StringNative = SmallString<N * sizeof(native_char_t)>;
114
115// Allows using this type across Plugin boundaries
120SC_COMPILER_EXTERN template struct SC_COMPILER_EXPORT SmallString<1024 * sizeof(native_char_t)>;
121} // namespace SC
#define SC_COMPILER_WARNING_POP
Pops warning from inside a macro.
Definition: Compiler.h:107
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
#define SC_COMPILER_OFFSETOF(Class, Field)
Returns offset of Class::Field in bytes.
Definition: Compiler.h:111
#define SC_COMPILER_EXTERN
Define compiler-specific export macros for DLL visibility.
Definition: Compiler.h:74
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition: Assert.h:66
#define SC_COMPILER_WARNING_PUSH_OFFSETOF
Disables invalid-offsetof gcc and clang warning.
Definition: Compiler.h:131
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition: Compiler.h:269
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
StringEncoding
String Encoding (Ascii, Utf8, Utf16)
Definition: StringIterator.h:17
@ Ascii
Encoding is ASCII.
@ Utf8
Encoding is UTF8.
A contiguous sequence of elements kept inside its inline storage.
Definition: Array.h:43
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
A non-modifiable owning string with associated encoding.
Definition: String.h:30
bool assign(StringView sv)
Assigns a StringView to this String, replacing existing contents.
Definition: String.h:175
String(StringEncoding encoding=StringEncoding::Utf8)
Builds an empty String with a given Encoding.
Definition: String.h:33
StringView view() const SC_LANGUAGE_LIFETIME_BOUND
Obtain a null-terminated StringView from current String.
Definition: String.h:200
String & operator=(const char(&text)[N])
Assigns an ascii string literal to current String.
Definition: String.h:131
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
constexpr StringEncoding getEncoding() const
Get encoding of this StringView.
Definition: StringView.h:93
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51