-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.h
204 lines (157 loc) · 4.41 KB
/
utf8.h
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
#include <iostream>
constexpr char Utf8ByteCompose(const int64_t unicode, size_t header_size) {
unsigned char header = 0xfe << (8 - header_size);
unsigned char payload = unicode & (0xff >> header_size);
return header | payload;
}
constexpr char Utf8Encoding(const uint64_t codepoint, const size_t byte) {
size_t code_units =
codepoint < 0x80 ? 1 :
codepoint < 0x800 ? 2 :
codepoint < 0x10000 ? 3 : 4;
if (byte > code_units)
return 0;
const uint64_t codepoint_cut = codepoint >> ((code_units - byte) * 6);
size_t header_size = 2;
if (byte == 1) {
header_size = code_units;
if (code_units > 1)
++header_size;
}
return Utf8ByteCompose(codepoint_cut, header_size);
}
#include <cstring>
struct char8_t {
const char data[5];
constexpr char8_t(const uint64_t unicode) :
data {
Utf8Encoding(unicode, 1),
Utf8Encoding(unicode, 2),
Utf8Encoding(unicode, 3),
Utf8Encoding(unicode, 4),
'\0'
}
{}
bool operator==(const uint64_t unicode) const {
const char data[5] {
Utf8Encoding(unicode, 1),
Utf8Encoding(unicode, 2),
Utf8Encoding(unicode, 3),
Utf8Encoding(unicode, 4),
'\0'
};
return std::strcmp(this->data, data) == 0;
}
bool operator==(char8_t& ch) const {
return std::strcmp(data, ch.data) == 0;
}
bool operator<(char8_t& ch) const {
return std::strcmp(data, ch.data) < 0;
}
bool operator>(char8_t& ch) const {
return std::strcmp(data, ch.data) > 0;
}
constexpr char8_t(const char* str) : data{str[0], str[1], str[2], str[3], '\0'} {}
};
std::ostream& operator <<(std::ostream& out, const char8_t& ch) {
out << ch.data;
return out;
}
#include <string_view>
template <typename T>
class base_utf8_string_iterator {
public:
base_utf8_string_iterator (T& data, const size_t pos) : pos(pos), data(data) {}
char8_t operator*();
base_utf8_string_iterator& operator--();
base_utf8_string_iterator operator--(int) {
auto past = *this;
--(*this);
return past;
}
base_utf8_string_iterator& operator++();
base_utf8_string_iterator operator++(int) {
auto past = *this;
++(*this);
return past;
}
base_utf8_string_iterator operator+(size_t i) const {
auto it = *this;
if (i > 0)
while(i--) ++it;
else
while(i++) --it;
return it;
}
base_utf8_string_iterator operator-(size_t i) const {
return (*this) + -i;
}
bool operator==(const base_utf8_string_iterator<T>& it) const {
return this->pos == it.pos;
}
bool operator!=(const base_utf8_string_iterator<T>& it) const {
return this->pos != it.pos;
}
private:
size_t pos;
T& data;
};
typedef base_utf8_string_iterator<const std::string_view> const_ustring_iterator;
class utf8_string_view : public std::string_view {
public:
utf8_string_view(const char* str) : std::string_view(str) {
code_points = CalculateLength(*this);
}
size_t length() const {
return this->code_points;
}
static size_t CalculateLength(const std::string_view& str) {
size_t code_points = 0;
for (const char& character : str)
if ((character & 0b11000000) != 0b10000000)
++code_points;
return code_points;
}
const_ustring_iterator begin() const {
return {*this, 0};
}
const_ustring_iterator end() const {
return {*this, std::string_view::length()};
}
char8_t operator[](const size_t pos) const {
return *(begin() + pos);
}
char8_t at(const size_t pos) const {
if (pos >= code_points)
throw std::out_of_range("");
return (*this)[pos];
}
private:
size_t code_points;
};
template <typename T>
char8_t base_utf8_string_iterator<T>::operator*() {
if ((data[pos] & 0b10000000) == 0b00000000)
return data[pos];
const char character[] = {data[pos], data[pos + 1], data[pos + 2], data[pos + 3], '\0'};
return character;
}
template <typename T>
base_utf8_string_iterator<T>& base_utf8_string_iterator<T>::operator++() {
// offset by variable character lenght
if ((data[pos] & 0b11100000) == 0b11000000)
pos += 1;
if ((data[pos] & 0b11110000) == 0b11100000)
pos += 2;
if ((data[pos] & 0b11111000) == 0b11110000)
pos += 3;
++pos;
return *this;
}
template <typename T>
base_utf8_string_iterator<T>& base_utf8_string_iterator<T>::operator--() {
--pos;
while((data[pos] & 0b11000000) == 0b10000000)
--pos;
return *this;
}