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
304
305
306
307
308
309
310
311
312
313
|
/*
* Copyright (c) 2022, mat
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Find.h>
#include <AK/QuickSort.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/Normalize.h>
#include <LibUnicode/UnicodeData.h>
namespace Unicode {
Optional<CodePointDecomposition const> __attribute__((weak)) code_point_decomposition(u32) { return {}; }
Optional<CodePointDecomposition const> __attribute__((weak)) code_point_decomposition_by_index(size_t) { return {}; }
NormalizationForm normalization_form_from_string(StringView form)
{
if (form == "NFD"sv)
return NormalizationForm::NFD;
if (form == "NFC"sv)
return NormalizationForm::NFC;
if (form == "NFKD"sv)
return NormalizationForm::NFKD;
if (form == "NFKC"sv)
return NormalizationForm::NFKC;
VERIFY_NOT_REACHED();
}
StringView normalization_form_to_string(NormalizationForm form)
{
switch (form) {
case NormalizationForm::NFD:
return "NFD"sv;
case NormalizationForm::NFC:
return "NFC"sv;
case NormalizationForm::NFKD:
return "NFKD"sv;
case NormalizationForm::NFKC:
return "NFKC"sv;
}
VERIFY_NOT_REACHED();
}
ALWAYS_INLINE static bool is_starter(u32 code_point)
{
return Unicode::canonical_combining_class(code_point) == 0;
}
// From https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G56669
static constexpr u32 HANGUL_SYLLABLE_BASE = 0xAC00;
static constexpr u32 HANGUL_LEADING_BASE = 0x1100;
static constexpr u32 HANGUL_VOWEL_BASE = 0x1161;
static constexpr u32 HANGUL_TRAILING_BASE = 0x11A7;
static constexpr u32 HANGUL_LEADING_COUNT = 19;
static constexpr u32 HANGUL_VOWEL_COUNT = 21;
static constexpr u32 HANGUL_TRAILING_COUNT = 28;
// NCount in the standard.
static constexpr u32 HANGUL_BLOCK_COUNT = HANGUL_VOWEL_COUNT * HANGUL_TRAILING_COUNT;
static constexpr u32 HANGUL_SYLLABLE_COUNT = HANGUL_LEADING_COUNT * HANGUL_BLOCK_COUNT;
ALWAYS_INLINE static bool is_hangul_code_point(u32 code_point)
{
return code_point >= HANGUL_SYLLABLE_BASE && code_point < HANGUL_SYLLABLE_BASE + HANGUL_SYLLABLE_COUNT;
}
ALWAYS_INLINE static bool is_hangul_leading(u32 code_point)
{
return code_point >= HANGUL_LEADING_BASE && code_point < HANGUL_LEADING_BASE + HANGUL_LEADING_COUNT;
}
ALWAYS_INLINE static bool is_hangul_vowel(u32 code_point)
{
return code_point >= HANGUL_VOWEL_BASE && code_point < HANGUL_VOWEL_BASE + HANGUL_VOWEL_COUNT;
}
ALWAYS_INLINE static bool is_hangul_trailing(u32 code_point)
{
return code_point >= HANGUL_TRAILING_BASE && code_point < HANGUL_TRAILING_BASE + HANGUL_TRAILING_COUNT;
}
// https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G56669
static void decompose_hangul_code_point(u32 code_point, Vector<u32>& code_points_output)
{
auto const index = code_point - HANGUL_SYLLABLE_BASE;
auto const leading_index = index / HANGUL_BLOCK_COUNT;
auto const vowel_index = (index % HANGUL_BLOCK_COUNT) / HANGUL_TRAILING_COUNT;
auto const trailing_index = index % HANGUL_TRAILING_COUNT;
auto const leading_part = HANGUL_LEADING_BASE + leading_index;
auto const vowel_part = HANGUL_VOWEL_BASE + vowel_index;
auto const trailing_part = HANGUL_TRAILING_BASE + trailing_index;
code_points_output.append(leading_part);
code_points_output.append(vowel_part);
if (trailing_index != 0)
code_points_output.append(trailing_part);
}
// L, V and LV, T Hangul Syllable Composition
// https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G59688
static u32 combine_hangul_code_points(u32 a, u32 b)
{
if (is_hangul_leading(a) && is_hangul_vowel(b)) {
auto const leading_index = a - HANGUL_LEADING_BASE;
auto const vowel_index = b - HANGUL_VOWEL_BASE;
auto const leading_vowel_index = leading_index * HANGUL_BLOCK_COUNT + vowel_index * HANGUL_TRAILING_COUNT;
return HANGUL_SYLLABLE_BASE + leading_vowel_index;
}
// LV characters are the first in each "T block", so use this check to avoid combining LVT with T.
if (is_hangul_code_point(a) && (a - HANGUL_SYLLABLE_BASE) % HANGUL_TRAILING_COUNT == 0 && is_hangul_trailing(b)) {
return a + b - HANGUL_TRAILING_BASE;
}
return 0;
}
static u32 combine_code_points(u32 a, u32 b)
{
Array<u32, 2> const points { a, b };
// FIXME: Do something better than linear search to find reverse mappings.
for (size_t index = 0;; ++index) {
auto mapping_maybe = Unicode::code_point_decomposition_by_index(index);
if (!mapping_maybe.has_value())
break;
auto& mapping = mapping_maybe.value();
if (mapping.tag == CompatibilityFormattingTag::Canonical && mapping.decomposition == points) {
if (code_point_has_property(mapping.code_point, Property::Full_Composition_Exclusion))
continue;
return mapping.code_point;
}
}
return 0;
}
enum class UseCompatibility {
Yes,
No
};
static void decompose_code_point(u32 code_point, Vector<u32>& code_points_output, UseCompatibility use_compatibility)
{
if (is_hangul_code_point(code_point)) {
decompose_hangul_code_point(code_point, code_points_output);
return;
}
auto const mapping = Unicode::code_point_decomposition(code_point);
if (mapping.has_value() && (mapping->tag == CompatibilityFormattingTag::Canonical || use_compatibility == UseCompatibility::Yes)) {
for (auto code_point : mapping->decomposition) {
decompose_code_point(code_point, code_points_output, use_compatibility);
}
} else {
code_points_output.append(code_point);
}
}
// This can be any sorting algorithm that maintains order (like std::stable_sort),
// however bubble sort is easier to implement, so go with it (for now).
template<typename T, typename LessThan>
void bubble_sort(Span<T> span, LessThan less_than)
{
for (size_t i = 0; i < span.size() - 1; ++i) {
for (size_t j = 0; j < span.size() - 1 - i; ++j) {
if (!less_than(span[j], span[j + 1]))
swap(span[j], span[j + 1]);
}
}
}
// The Canonical Ordering Algorithm, as specified in Version 15.0.0 of the Unicode Standard.
// See Section 3.11, D109; and UAX #15 https://unicode.org/reports/tr15
// https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G49591
static void canonical_ordering_algorithm(Span<u32> code_points)
{
for (size_t i = 0; i < code_points.size(); ++i) {
if (!is_starter(code_points[i])) {
auto starter = find_if(code_points.begin() + i, code_points.end(), is_starter);
auto const span_size = static_cast<size_t>(starter - (code_points.begin() + i));
// Nothing to reorder, so continue.
if (span_size <= 1)
continue;
Span<u32> const span { code_points.data() + i, span_size };
bubble_sort(span, [](u32 a, u32 b) {
// Use <= to keep ordering.
return Unicode::canonical_combining_class(a) <= Unicode::canonical_combining_class(b);
});
// Skip over span we just sorted.
i += span_size - 1;
}
}
}
// See Section 3.11, D115 of Version 15.0.0 of the Unicode Standard.
static bool is_blocked(Span<u32> code_points, size_t a, size_t c)
{
if (!is_starter(code_points[a]) || a == c - 1)
return false;
auto const c_combining_class = Unicode::canonical_combining_class(code_points[c]);
auto const b_combining_class = Unicode::canonical_combining_class(code_points[c - 1]);
return b_combining_class == 0 || b_combining_class >= c_combining_class;
}
// The Canonical Composition Algorithm, as specified in Version 15.0.0 of the Unicode Standard.
// See Section 3.11, D117; and UAX #15 https://unicode.org/reports/tr15
// https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf#G50628
static void canonical_composition_algorithm(Vector<u32>& code_points)
{
for (size_t i = 1; i < code_points.size(); ++i) {
auto const current_character = code_points[i];
// R1. Seek back (left) to find the last Starter L preceding C in the character sequence
for (ssize_t j = i - 1; j >= 0; --j) {
if (!is_starter(code_points[j]))
continue;
// R2. If there is such an L, and C is not blocked from L,
// and there exists a Primary Composite P which is canonically equivalent to <L, C>,
// then replace L by P in the sequence and delete C from the sequence.
if (is_blocked(code_points.span(), j, i))
continue;
auto composite = combine_hangul_code_points(code_points[j], current_character);
if (composite == 0)
composite = combine_code_points(code_points[j], current_character);
if (composite != 0) {
code_points[j] = composite;
code_points.remove(i);
--i;
break;
}
}
}
}
static Vector<u32> normalize_nfd(Utf8View string)
{
Vector<u32> result;
for (auto const code_point : string) {
decompose_code_point(code_point, result, UseCompatibility::No);
}
canonical_ordering_algorithm(result);
return result;
}
static Vector<u32> normalize_nfc(Utf8View string)
{
auto result = normalize_nfd(string);
canonical_composition_algorithm(result);
return result;
}
static Vector<u32> normalize_nfkd(Utf8View string)
{
Vector<u32> result;
for (auto const code_point : string) {
decompose_code_point(code_point, result, UseCompatibility::Yes);
}
canonical_ordering_algorithm(result);
return result;
}
static Vector<u32> normalize_nfkc(Utf8View string)
{
auto result = normalize_nfkd(string);
canonical_composition_algorithm(result);
return result;
}
static Vector<u32> normalize_implementation(Utf8View string, NormalizationForm form)
{
switch (form) {
case NormalizationForm::NFD:
return normalize_nfd(string);
case NormalizationForm::NFC:
return normalize_nfc(string);
case NormalizationForm::NFKD:
return normalize_nfkd(string);
case NormalizationForm::NFKC:
return normalize_nfkc(string);
}
VERIFY_NOT_REACHED();
}
String normalize(StringView string, NormalizationForm form)
{
Utf8View const view { string };
auto const code_points = normalize_implementation(view, form);
StringBuilder builder;
for (auto code_point : code_points) {
builder.append_code_point(code_point);
}
return builder.to_string();
}
}
|