blob: 1939c7dddac681298973e19837d90cc371d8b538 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* libfud
* Copyright 2024 Dominick Allen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUD_STRING_HPP
#define FUD_STRING_HPP
#include "fud_status.hpp"
#include "fud_string_view.hpp"
#include "fud_utf8.hpp"
#include <climits>
#include <cstddef>
static_assert(CHAR_BIT == 8);
namespace fud {
constexpr size_t SSO_BUF_LENGTH = 15;
constexpr size_t SSO_BUF_SIZE = SSO_BUF_LENGTH + 1;
class String {
public:
String() = default;
explicit String(const utf8* cString);
explicit String(const char* cString);
String(const String& rhs);
String(String&& rhs) noexcept;
~String();
String& operator=(const String& rhs);
String& operator=(String&& rhs) noexcept;
[[nodiscard]] constexpr size_t length() const
{
return m_length;
}
[[nodiscard]] constexpr bool empty() const
{
return m_length == 0;
}
[[nodiscard]] constexpr size_t size() const
{
return m_length + 1;
}
[[nodiscard]] constexpr size_t capacity() const
{
return m_capacity - 1;
}
/** \brier The underlying data, guaranteed to have c string representation. */
[[nodiscard]] constexpr const utf8* data() const
{
return isLarge() ? m_data : m_buffer.data();
}
/** \brier The underlying data as an explicit c string. */
[[nodiscard]] inline const char* c_str() const
{
return isLarge() ? reinterpret_cast<const char*>(m_data) : reinterpret_cast<const char*>(m_buffer.data());
}
/** \brier The underlying data, guaranteed to have c string representation. */
[[nodiscard]] constexpr utf8* data()
{
return isLarge() ? m_data : m_buffer.data();
}
/** \brier The underlying data as an explicit c string. */
[[nodiscard]] inline char* c_str()
{
return isLarge() ? reinterpret_cast<char*>(m_data) : reinterpret_cast<char*>(m_buffer.data());
}
[[nodiscard]] bool nullTerminated() const;
[[nodiscard]] bool valid() const;
[[nodiscard]] bool utf8Valid() const;
FudStatus nullTerminate();
FudStatus reserve(size_t newCapacity);
[[nodiscard]] std::optional<utf8> back();
[[nodiscard]] constexpr size_t remainingLength() const
{
if (m_length >= m_capacity) {
return 0;
}
return m_capacity - 1U - m_length;
}
[[nodiscard]] inline StringView asView() const {
return StringView(*this);
}
FudStatus pushBack(char letter);
FudStatus pushBack(utf8 letter);
FudStatus pushBack(const FudUtf8& letter);
std::optional<utf8> pop();
FudStatus append(const char* source);
FudStatus append(const String& source);
FudStatus append(StringView source);
[[nodiscard]] String catenate(const String& rhs) const;
[[nodiscard]] String catenate(const char* rhs) const;
[[nodiscard]] bool compare(const String& rhs) const;
const utf8* begin() const;
const utf8* end() const;
private:
void cleanup();
FudStatus resize(size_t newCapacity);
using BufType = Array<utf8, SSO_BUF_SIZE>;
union {
BufType m_buffer{BufType::constFill(0)};
utf8* m_data;
};
size_t m_length{0};
size_t m_capacity{SSO_BUF_SIZE};
[[nodiscard]] constexpr bool isLarge() const
{
return m_capacity > SSO_BUF_SIZE;
}
};
} // namespace fud
#endif
|