-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtf8.cpp
More file actions
303 lines (249 loc) · 7.1 KB
/
Utf8.cpp
File metadata and controls
303 lines (249 loc) · 7.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <hxcpp.h>
#include <array>
using namespace cpp::marshal;
namespace
{
bool isAsciiBuffer(const View<uint8_t>& buffer)
{
auto i = int64_t{ 0 };
while (i < buffer.length)
{
auto p = cpp::encoding::Utf8::codepoint(buffer.slice(i));
if (p > 127)
{
return false;
}
i += cpp::encoding::Utf8::getByteCount(p);
}
return true;
}
}
int cpp::encoding::Utf8::getByteCount(const null&)
{
hx::NullReference("String", false);
return 0;
}
int cpp::encoding::Utf8::getByteCount(const char32_t& codepoint)
{
if (codepoint <= 0x7F)
{
return 1;
}
else if (codepoint <= 0x7FF)
{
return 2;
}
else if (codepoint <= 0xFFFF)
{
return 3;
}
else
{
return 4;
}
}
int64_t cpp::encoding::Utf8::getByteCount(const String& string)
{
if (null() == string)
{
hx::NullReference("String", false);
}
if (string.isAsciiEncoded())
{
return string.length;
}
#if defined(HX_SMART_STRINGS)
auto source = View<char16_t>(string.raw_wptr(), string.length).reinterpret<uint8_t>();
auto length = source.length;
auto bytes = int64_t{ 0 };
auto i = int64_t{ 0 };
while (i < source.length)
{
auto slice = source.slice(i);
auto p = Utf16::codepoint(slice);
i += Utf16::getByteCount(p);
bytes += getByteCount(p);
}
return bytes;
#else
return hx::Throw(HX_CSTRING("Unexpected encoding error"));
#endif
}
int cpp::encoding::Utf8::getCharCount(const null&)
{
hx::NullReference("String", false);
return 0;
}
int cpp::encoding::Utf8::getCharCount(const char32_t& codepoint)
{
return getByteCount(codepoint) / sizeof(char);
}
int64_t cpp::encoding::Utf8::getCharCount(const String& string)
{
return getByteCount(string) / sizeof(char);
}
int cpp::encoding::Utf8::encode(const null&, const cpp::marshal::View<uint8_t>& buffer)
{
hx::NullReference("String", false);
return 0;
}
int64_t cpp::encoding::Utf8::encode(const String& string, const cpp::marshal::View<uint8_t>& buffer)
{
if (null() == string)
{
hx::NullReference("String", false);
}
if (0 == string.length)
{
return 0;
}
if (buffer.isEmpty())
{
return hx::Throw(HX_CSTRING("Buffer too small"));
}
if (string.isAsciiEncoded())
{
auto src = cpp::marshal::View<uint8_t>(reinterpret_cast<uint8_t*>(const_cast<char*>(string.raw_ptr())), string.length);
if (src.tryCopyTo(buffer))
{
return src.length;
}
else
{
return hx::Throw(HX_CSTRING("Buffer too small"));
}
}
#if defined(HX_SMART_STRINGS)
if (getByteCount(string) > buffer.length)
{
hx::Throw(HX_CSTRING("Buffer too small"));
}
auto initialPtr = buffer.ptr.ptr;
auto source = View<char16_t>(string.raw_wptr(), string.length).reinterpret<uint8_t>();
auto i = int64_t{ 0 };
auto k = int64_t{ 0 };
while (i < source.length)
{
auto p = Utf16::codepoint(source.slice(i));
i += Utf16::getByteCount(p);
k += encode(p, buffer.slice(k));
}
return k;
#else
return hx::Throw(HX_CSTRING("Unexpected encoding error"));
#endif
}
int cpp::encoding::Utf8::encode(const char32_t& codepoint, const cpp::marshal::View<uint8_t>& buffer)
{
if (codepoint <= 0x7F)
{
buffer[0] = static_cast<uint8_t>(codepoint);
return 1;
}
else if (codepoint <= 0x7FF)
{
auto data = std::array<uint8_t, 2>
{ {
static_cast<uint8_t>(0xC0 | (codepoint >> 6)),
static_cast<uint8_t>(0x80 | (codepoint & 63))
} };
auto src = View<uint8_t>(data.data(), data.size());
src.copyTo(buffer);
return data.size();
}
else if (codepoint <= 0xFFFF)
{
auto data = std::array<uint8_t, 3>
{ {
static_cast<uint8_t>(0xE0 | (codepoint >> 12)),
static_cast<uint8_t>(0x80 | ((codepoint >> 6) & 63)),
static_cast<uint8_t>(0x80 | (codepoint & 63))
} };
auto src = View<uint8_t>(data.data(), data.size());
src.copyTo(buffer);
return data.size();
}
else
{
auto data = std::array<uint8_t, 4>
{ {
static_cast<uint8_t>(0xF0 | (codepoint >> 18)),
static_cast<uint8_t>(0x80 | ((codepoint >> 12) & 63)),
static_cast<uint8_t>(0x80 | ((codepoint >> 6) & 63)),
static_cast<uint8_t>(0x80 | (codepoint & 63))
} };
auto src = View<uint8_t>(data.data(), data.size());
src.copyTo(buffer);
return data.size();
}
}
String cpp::encoding::Utf8::decode(const cpp::marshal::View<uint8_t>& buffer)
{
if (buffer.isEmpty())
{
return String::emptyString;
}
if (isAsciiBuffer(buffer))
{
return Ascii::decode(buffer);
}
#if defined(HX_SMART_STRINGS)
auto chars = int64_t{ 0 };
auto i = int64_t{ 0 };
while (i < buffer.length)
{
auto p = codepoint(buffer.slice(i));
i += getByteCount(p);
chars += Utf16::getCharCount(p);
}
auto backing = View<char16_t>(::String::allocChar16Ptr(chars), chars);
auto output = backing.reinterpret<uint8_t>();
auto k = int64_t{ 0 };
i = 0;
while (i < buffer.length)
{
auto p = codepoint(buffer.slice(i));
i += getByteCount(p);
k += Utf16::encode(p, output.slice(k));
}
return String(backing.ptr.ptr, chars);
#else
auto backing = View<char>(hx::InternalNew(buffer.length, false), buffer.length);
std::memcpy(backing.ptr.ptr, buffer.ptr.ptr, buffer.length);
return String(backing.ptr.ptr, static_cast<int>(buffer.length));
#endif
}
char32_t cpp::encoding::Utf8::codepoint(const cpp::marshal::View<uint8_t>& buffer)
{
auto b0 = static_cast<char32_t>(buffer[0]);
if ((b0 & 0x80) == 0)
{
return b0;
}
else if ((b0 & 0xE0) == 0xC0)
{
return (static_cast<char32_t>(b0 & 0x1F) << 6) | static_cast<char32_t>(buffer.slice(1)[0] & 0x3F);
}
else if ((b0 & 0xF0) == 0xE0)
{
auto staging = std::array<uint8_t, 2>();
auto dst = View<uint8_t>(staging.data(), staging.size());
buffer.slice(1, staging.size()).copyTo(dst);
return (static_cast<char32_t>(b0 & 0x0F) << 12) | (static_cast<char32_t>(staging[0] & 0x3F) << 6) | static_cast<char32_t>(staging[1] & 0x3F);
}
else if ((b0 & 0xF8) == 0xF0)
{
auto staging = std::array<uint8_t, 3>();
auto dst = View<uint8_t>(staging.data(), staging.size());
buffer.slice(1, staging.size()).copyTo(dst);
return
(static_cast<char32_t>(b0 & 0x07) << 18) |
(static_cast<char32_t>(staging[0] & 0x3F) << 12) |
(static_cast<char32_t>(staging[1] & 0x3F) << 6) |
static_cast<char32_t>(staging[2] & 0x3F);
}
else
{
return int{ hx::Throw(HX_CSTRING("Failed to read codepoint")) };
}
}