-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunicode.cpp
More file actions
246 lines (203 loc) · 6.37 KB
/
unicode.cpp
File metadata and controls
246 lines (203 loc) · 6.37 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
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "unicode.h"
#include <src/com/error.h>
#include <cstddef>
#include <cstdint>
#include <iomanip>
#include <ios>
#include <sstream>
#include <string>
// UTF-8
// U+0000 .. U+007F 0xxxxxxx
// U+0080 .. U+07FF 110xxxxx 10xxxxxx
// U+0800 .. U+FFFF 1110xxxx 10xxxxxx 10xxxxxx
// U+10000 .. U+10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
namespace ns::text::unicode
{
namespace
{
char32_t replacement(std::size_t* const i)
{
*i += 1;
return unicode::REPLACEMENT_CHARACTER;
}
char32_t utf8_2_to_utf32(const std::string& s, std::size_t* const i)
{
ASSERT(*i < s.size());
if (*i + 1 >= s.size())
{
return replacement(i);
}
const unsigned char s1 = s[*i + 1];
if ((s1 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s0 = s[*i];
*i += 2;
return (s0 & 0b1'1111) << 6 | (s1 & 0b11'1111);
}
char32_t utf8_3_to_utf32(const std::string& s, std::size_t* const i)
{
ASSERT(*i < s.size());
if (*i + 2 >= s.size())
{
return replacement(i);
}
const unsigned char s1 = s[*i + 1];
if ((s1 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s2 = s[*i + 2];
if ((s2 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s0 = s[*i];
*i += 3;
return (s0 & 0b1111) << 12 | (s1 & 0b11'1111) << 6 | (s2 & 0b11'1111);
}
char32_t utf8_4_to_utf32(const std::string& s, std::size_t* const i)
{
ASSERT(*i < s.size());
if (*i + 3 >= s.size())
{
return replacement(i);
}
const unsigned char s1 = s[*i + 1];
if ((s1 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s2 = s[*i + 2];
if ((s2 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s3 = s[*i + 3];
if ((s3 & 0b1100'0000) != 0b1000'0000)
{
return replacement(i);
}
const unsigned char s0 = s[*i];
const char32_t res = (s0 & 0b111) << 18 | (s1 & 0b11'1111) << 12 | (s2 & 0b11'1111) << 6 | (s3 & 0b11'1111);
if (res <= 0x10FFFF)
{
*i += 4;
return res;
}
return replacement(i);
}
}
template <typename T>
std::string utf32_to_number_string(const T code_point)
{
static_assert(std::is_same_v<T, char32_t>);
std::ostringstream oss;
oss << "U+";
oss << std::uppercase << std::hex << std::setfill('0');
oss << std::setw(4) << static_cast<std::uint_least32_t>(code_point);
return oss.str();
}
std::string utf8_to_number_string(const std::string& s)
{
if (s.empty())
{
error("Empty UTF-8 string");
}
std::ostringstream oss;
oss << std::uppercase << std::hex << std::setfill('0');
for (const char c : s)
{
if (!oss.str().empty())
{
oss << " ";
}
oss << "0x" << std::setw(2) << static_cast<unsigned>(static_cast<unsigned char>(c));
}
return oss.str();
}
template <typename T>
std::string utf32_to_utf8(const T code_point)
{
static_assert(std::is_same_v<T, char32_t>);
if (code_point <= 0x7F)
{
return std::string(1, code_point);
}
if (code_point <= 0x7FF)
{
const char c0 = 0b1100'0000 | (code_point >> 6);
const char c1 = 0b1000'0000 | (code_point & 0b11'1111);
return std::string({c0, c1});
}
if (code_point <= 0xFFFF)
{
const char c0 = 0b1110'0000 | (code_point >> 12);
const char c1 = 0b1000'0000 | ((code_point >> 6) & 0b11'1111);
const char c2 = 0b1000'0000 | (code_point & 0b11'1111);
return std::string({c0, c1, c2});
}
if (code_point <= 0x10FFFF)
{
const char c0 = 0b1111'0000 | (code_point >> 18);
const char c1 = 0b1000'0000 | ((code_point >> 12) & 0b11'1111);
const char c2 = 0b1000'0000 | ((code_point >> 6) & 0b11'1111);
const char c3 = 0b1000'0000 | (code_point & 0b11'1111);
return std::string({c0, c1, c2, c3});
}
static_assert(unicode::REPLACEMENT_CHARACTER <= 0x10FFFF);
return utf32_to_utf8(unicode::REPLACEMENT_CHARACTER);
}
char32_t utf8_to_utf32(const std::string& s, std::size_t* const i)
{
if (*i >= s.size())
{
error("UTF-8 string index out of range");
}
const unsigned char s0 = s[*i];
if (s0 <= 0x7F)
{
*i += 1;
return s0;
}
if ((s0 & 0b1110'0000) == 0b1100'0000)
{
return utf8_2_to_utf32(s, i);
}
if ((s0 & 0b1111'0000) == 0b1110'0000)
{
return utf8_3_to_utf32(s, i);
}
if ((s0 & 0b1111'1000) == 0b1111'0000)
{
return utf8_4_to_utf32(s, i);
}
return replacement(i);
}
char32_t utf8_to_utf32(const std::string& s)
{
std::size_t i = 0;
const char32_t c = unicode::utf8_to_utf32(s, &i);
if (i == s.size())
{
return c;
}
error("One UTF-8 character string is too long: " + utf8_to_number_string(s));
}
template std::string utf32_to_number_string(char32_t);
template std::string utf32_to_utf8(char32_t);
}