-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtf16.cpp
More file actions
279 lines (228 loc) · 5.64 KB
/
Utf16.cpp
File metadata and controls
279 lines (228 loc) · 5.64 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
#include <hxcpp.h>
#include <array>
using namespace cpp::marshal;
namespace
{
bool isSurrogate(char32_t codepoint)
{
return codepoint >= 0xd800 && codepoint < 0xe000;
}
bool isLowSurrogate(char32_t codepoint)
{
return codepoint >= 0xdc00 && codepoint < 0xe000;
}
bool isHighSurrogate(char32_t codepoint)
{
return codepoint >= 0xd800 && codepoint < 0xdc00;
}
bool isAsciiBuffer(const View<uint8_t>& buffer)
{
auto i = int64_t{ 0 };
while (i < buffer.length)
{
auto p = cpp::encoding::Utf16::codepoint(buffer.slice(i));
if (p > 127)
{
return false;
}
i += cpp::encoding::Utf16::getByteCount(p);
}
return true;
}
String toAsciiString(const View<uint8_t>& buffer)
{
auto bytes = buffer.length / sizeof(char16_t);
auto chars = View<char>(hx::InternalNew(bytes + 1, false), bytes * sizeof(char));
auto i = int64_t{ 0 };
auto k = int64_t{ 0 };
while (i < buffer.length)
{
auto p = cpp::encoding::Utf16::codepoint(buffer.slice(i));
chars[k++] = static_cast<char>(p);
i += cpp::encoding::Utf16::getByteCount(p);
}
return String(chars.ptr.ptr, chars.length);
}
}
bool cpp::encoding::Utf16::isEncoded(const String& string)
{
if (null() == string)
{
hx::NullReference("String", false);
}
return string.isUTF16Encoded();
}
int cpp::encoding::Utf16::getByteCount(const null&)
{
hx::NullReference("String", false);
return 0;
}
int cpp::encoding::Utf16::getByteCount(const char32_t& codepoint)
{
return codepoint <= 0xFFFF ? 2 : 4;
}
int64_t cpp::encoding::Utf16::getByteCount(const String& string)
{
if (null() == string)
{
hx::NullReference("String", false);
}
if (string.isUTF16Encoded())
{
return string.length * sizeof(char16_t);
}
else
{
auto bytes = int64_t{ 0 };
for (auto i = 0; i < string.length; i++)
{
bytes += getByteCount(static_cast<char32_t>(string.raw_ptr()[i]));
}
return bytes;
}
}
int cpp::encoding::Utf16::getCharCount(const null&)
{
hx::NullReference("String", false);
return 0;
}
int cpp::encoding::Utf16::getCharCount(const char32_t& codepoint)
{
return getByteCount(codepoint) / sizeof(char16_t);
}
int64_t cpp::encoding::Utf16::getCharCount(const String& string)
{
return getByteCount(string) / sizeof(char16_t);
}
int cpp::encoding::Utf16::encode(const null&, const cpp::marshal::View<uint8_t>& buffer)
{
hx::NullReference("String", false);
return 0;
}
int64_t cpp::encoding::Utf16::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 defined(HX_SMART_STRINGS)
if (string.isUTF16Encoded())
{
auto src = cpp::marshal::View<uint8_t>(reinterpret_cast<uint8_t*>(const_cast<char16_t*>(string.raw_wptr())), string.length * sizeof(char16_t));
if (src.tryCopyTo(buffer))
{
return src.length;
}
else
{
return hx::Throw(HX_CSTRING("Buffer too small"));
}
}
else
#endif
{
auto bytes = int64_t{ 0 };
for (auto i = 0; i < string.length; i++)
{
bytes += getByteCount(static_cast<char32_t>(string.raw_ptr()[i]));
}
if (bytes > buffer.length)
{
return hx::Throw(HX_CSTRING("Buffer too small"));
}
auto i = int64_t{ 0 };
for (auto k = 0; k < string.length; k++)
{
i += encode(static_cast<char32_t>(string.raw_ptr()[k]), buffer.slice(i));
}
return bytes;
}
}
int cpp::encoding::Utf16::encode(const char32_t& codepoint, const cpp::marshal::View<uint8_t>& buffer)
{
if (codepoint < 0xD800)
{
Marshal::writeUInt16(buffer, static_cast<char16_t>(codepoint));
return 2;
}
else if (codepoint < 0xE000)
{
// D800 - DFFF is invalid
return hx::Throw(HX_CSTRING("Invalid UTF16"));
}
else if (codepoint < 0x10000)
{
Marshal::writeUInt16(buffer, static_cast<char16_t>(codepoint));
return 2;
}
else if (codepoint < 0x110000)
{
auto staging = std::array<uint8_t, 4>();
auto fst = View<uint8_t>(staging.data(), 2);
auto snd = View<uint8_t>(staging.data() + 2, 2);
auto all = View<uint8_t>(staging.data(), staging.size());
Marshal::writeUInt16(fst, 0xD800 + (((codepoint - 0x10000) >> 10) & 0x3FF));
Marshal::writeUInt16(snd, 0xDC00 + ((codepoint - 0x10000) & 0x3FF));
all.copyTo(buffer);
return 4;
}
return 0;
}
String cpp::encoding::Utf16::decode(const cpp::marshal::View<uint8_t>& buffer)
{
if (buffer.isEmpty())
{
return String::emptyString;
}
if (isAsciiBuffer(buffer))
{
return toAsciiString(buffer);
}
#if defined(HX_SMART_STRINGS)
auto i = int64_t{ 0 };
while (i < buffer.length)
{
auto p = codepoint(buffer.slice(i));
i += getByteCount(p);
}
auto chars = i / sizeof(char16_t);
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 += encode(p, output.slice(k));
}
return String(backing.ptr.ptr, chars);
#else
return hx::Throw(HX_CSTRING("Not Implemented : UTF16 decode when HX_SMART_STRINGS is not defined"));
#endif
}
char32_t cpp::encoding::Utf16::codepoint(const cpp::marshal::View<uint8_t>& buffer)
{
auto first = static_cast<char16_t>(Marshal::readUInt16(buffer));
if (0xD800 <= first && first < 0xDc00)
{
auto second = static_cast<char16_t>(Marshal::readUInt16(buffer.slice(2)));
if (0xDC00 <= second && second < 0xE000)
{
return static_cast<char32_t>((((first - 0xD800) << 10) | (second - 0xDC00)) + 0x10000);
}
return int{ hx::Throw(HX_CSTRING("Invalid UTF16")) };
}
else
{
return static_cast<char32_t>(first);
}
}