-
Notifications
You must be signed in to change notification settings - Fork 11
/
Utf8ToUc2.cpp
50 lines (46 loc) · 1.03 KB
/
Utf8ToUc2.cpp
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
#include "Common.h"
#include <stdexcept>
#include <iostream>
int UtfToUc2(
const U_TF8* utfarray,
size_t utf8len,
U_NIC* uc2dest,
size_t uc2space) {
try {
if (utfarray == nullptr || uc2dest == nullptr)
throw std::runtime_error("Check your file source");
}
catch (std::runtime_error& e) {
std::cout << e.what() << std::endl;
abort();
}
const U_TF8* utf8pos = utfarray;
const U_TF8* utf8end = utfarray + utf8len;
U_NIC* uc2pos = uc2dest;
U_NIC* uc2end = uc2dest + uc2space;
while (utf8pos < utf8end && uc2pos < uc2end)
{
if (*utf8pos < 0x80)
{
*uc2pos++ = *utf8pos++;
}
else if ((*utf8pos & 0xF0) == 0xE0)
{
if (uc2space < 3)
break;
*uc2pos |= (*utf8pos++ & 0x0F) << 12;
*uc2pos |= (*utf8pos++ & 0x3F) << 6;
*uc2pos++ |= (*utf8pos++ & 0x3F);
}
else if ((*utf8pos & 0xF0) == 0xC0)
{
if (uc2space < 2)
break;
*uc2pos |= (*utf8pos++ & 0x1F) << 6;
*uc2pos++ |= (*utf8pos++ & 0x3F);
}
else
utf8pos++;
}
return (uc2pos - uc2dest);
}