-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathfixed_string.hpp
216 lines (201 loc) · 6.93 KB
/
fixed_string.hpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef CTLL__FIXED_STRING__GPP
#define CTLL__FIXED_STRING__GPP
#include <utility>
#include <cstddef>
#include <string_view>
#include <cstdint>
#include "utilities.hpp"
namespace ctll {
struct length_value_t {
uint32_t value;
uint8_t length;
};
constexpr length_value_t length_and_value_of_utf8_code_point(uint8_t first_unit) noexcept {
if ((first_unit & 0b1000'0000) == 0b0000'0000) return {static_cast<uint32_t>(first_unit), 1};
else if ((first_unit & 0b1110'0000) == 0b1100'0000) return {static_cast<uint32_t>(first_unit & 0b0001'1111), 2};
else if ((first_unit & 0b1111'0000) == 0b1110'0000) return {static_cast<uint32_t>(first_unit & 0b0000'1111), 3};
else if ((first_unit & 0b1111'1000) == 0b1111'0000) return {static_cast<uint32_t>(first_unit & 0b0000'0111), 4};
else if ((first_unit & 0b1111'1100) == 0b1111'1000) return {static_cast<uint32_t>(first_unit & 0b0000'0011), 5};
else if ((first_unit & 0b1111'1100) == 0b1111'1100) return {static_cast<uint32_t>(first_unit & 0b0000'0001), 6};
else return {0, 0};
}
constexpr char32_t value_of_trailing_utf8_code_point(uint8_t unit, bool & correct) noexcept {
if ((unit & 0b1100'0000) == 0b1000'0000) return unit & 0b0011'1111;
else {
correct = false;
return 0;
}
}
constexpr length_value_t length_and_value_of_utf16_code_point(uint16_t first_unit) noexcept {
if ((first_unit & 0b1111110000000000) == 0b1101'1000'0000'0000) return {static_cast<uint32_t>(first_unit & 0b0000001111111111), 2};
else return {first_unit, 1};
}
template <size_t N> struct fixed_string {
char32_t content[N] = {};
size_t real_size{0};
bool correct_flag{true};
template <typename T> constexpr fixed_string(const T (&input)[N+1]) noexcept {
if constexpr (std::is_same_v<T, char>) {
#ifdef CTRE_STRING_IS_UTF8
size_t out{0};
for (size_t i{0}; i < N; ++i) {
if ((i == (N-1)) && (input[i] == 0)) break;
length_value_t info = length_and_value_of_utf8_code_point(input[i]);
switch (info.length) {
case 6:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 5:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 4:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 3:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 2:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 1:
content[out++] = static_cast<char32_t>(info.value);
real_size++;
break;
default:
correct_flag = false;
return;
}
}
#else
for (size_t i{0}; i < N; ++i) {
content[i] = static_cast<uint8_t>(input[i]);
if ((i == (N-1)) && (input[i] == 0)) break;
real_size++;
}
#endif
#if __cpp_char8_t
} else if constexpr (std::is_same_v<T, char8_t>) {
size_t out{0};
for (size_t i{0}; i < N; ++i) {
if ((i == (N-1)) && (input[i] == 0)) break;
length_value_t info = length_and_value_of_utf8_code_point(input[i]);
switch (info.length) {
case 6:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 5:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 4:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 3:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 2:
if (++i < N) info.value = (info.value << 6) | value_of_trailing_utf8_code_point(input[i], correct_flag);
[[fallthrough]];
case 1:
content[out++] = static_cast<char32_t>(info.value);
real_size++;
break;
default:
correct_flag = false;
return;
}
}
#endif
} else if constexpr (std::is_same_v<T, char16_t>) {
size_t out{0};
for (size_t i{0}; i < N; ++i) {
length_value_t info = length_and_value_of_utf16_code_point(input[i]);
if (info.length == 2) {
if (++i < N) {
if ((input[i] & 0b1111'1100'0000'0000) == 0b1101'1100'0000'0000) {
content[out++] = ((info.value << 10) | (input[i] & 0b0000'0011'1111'1111)) + 0x10000;
} else {
correct_flag = false;
break;
}
}
} else {
if ((i == (N-1)) && (input[i] == 0)) break;
content[out++] = info.value;
}
}
real_size = out;
#ifndef CTLL_NO_WCHAR_T
} else if constexpr (std::is_same_v<T, wchar_t> || std::is_same_v<T, char32_t>) {
for (size_t i{0}; i < N; ++i) {
content[i] = static_cast<char32_t>(input[i]);
if ((i == (N - 1)) && (input[i] == 0)) break;
real_size++;
}
#endif
}
}
constexpr fixed_string(const fixed_string & other) noexcept {
for (size_t i{0}; i < N; ++i) {
content[i] = other.content[i];
}
real_size = other.real_size;
correct_flag = other.correct_flag;
}
constexpr bool correct() const noexcept {
return correct_flag;
}
constexpr size_t size() const noexcept {
return real_size;
}
constexpr const char32_t * begin() const noexcept {
return content;
}
constexpr const char32_t * end() const noexcept {
return content + size();
}
constexpr char32_t operator[](size_t i) const noexcept {
return content[i];
}
template <size_t M> constexpr bool is_same_as(const fixed_string<M> & rhs) const noexcept {
if (real_size != rhs.size()) return false;
for (size_t i{0}; i != real_size; ++i) {
if (content[i] != rhs[i]) return false;
}
return true;
}
constexpr operator std::basic_string_view<char32_t>() const noexcept {
return std::basic_string_view<char32_t>{content, size()};
}
};
template <> class fixed_string<0> {
static constexpr char32_t empty[1] = {0};
public:
template <typename T> constexpr fixed_string(const T *) noexcept {
}
constexpr fixed_string(std::initializer_list<char32_t>) noexcept {
}
constexpr fixed_string(const fixed_string &) noexcept {
}
constexpr bool correct() const noexcept {
return true;
}
constexpr size_t size() const noexcept {
return 0;
}
constexpr const char32_t * begin() const noexcept {
return empty;
}
constexpr const char32_t * end() const noexcept {
return empty + size();
}
constexpr char32_t operator[](size_t) const noexcept {
return 0;
}
constexpr operator std::basic_string_view<char32_t>() const noexcept {
return std::basic_string_view<char32_t>{empty, 0};
}
};
template <typename CharT, size_t N> fixed_string(const CharT (&)[N]) -> fixed_string<N-1>;
template <size_t N> fixed_string(fixed_string<N>) -> fixed_string<N>;
}
#endif