-
Notifications
You must be signed in to change notification settings - Fork 171
/
unicode_ops.c
447 lines (403 loc) · 18.1 KB
/
unicode_ops.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* Compares two strings, based on Unicode Collation Algorithm
* 0 The strings are identical, includes codepoints
* -1/1 We used the primary collation values to decide the result
* -2/2 We used the secondary, meaning the primary weights were equal
* -3/3 We used the tetriary, meaning the primary and also the secondary
weights (if we used that option, which is the default) were equal.
* -4/4 We used codepoints to decide because primary, secondary and/or tetriary
were equal (depending on if secondary and tetriary were requested).
If the codepoints are all the same, we will decide based on length.
* -10/10 The collation algorithm was not able to be applied and so they
were compared based on codepoints. If codepoints were equal they were
compared by length. */
MVMint32 MVM_unicode_collation_primary (MVMThreadContext *tc, MVMint32 codepoint) {
return MVM_unicode_codepoint_get_property_int(tc, codepoint, MVM_UNICODE_PROPERTY_MVM_COLLATION_PRIMARY);
}
MVMint32 MVM_unicode_collation_secondary (MVMThreadContext *tc, MVMint32 codepoint) {
return MVM_unicode_codepoint_get_property_int(tc, codepoint, MVM_UNICODE_PROPERTY_MVM_COLLATION_SECONDARY);
}
MVMint32 MVM_unicode_collation_tertiary (MVMThreadContext *tc, MVMint32 codepoint) {
return MVM_unicode_codepoint_get_property_int(tc, codepoint, MVM_UNICODE_PROPERTY_MVM_COLLATION_TERTIARY);
}
MVMint64 MVM_unicode_string_compare
(MVMThreadContext *tc, MVMString *a, MVMString *b,
MVMint32 collation_mode, MVMint32 lang_mode, MVMint32 country_mode) {
MVMStringIndex alen, blen, i, scanlen;
/* Iteration variables */
MVMGrapheme32 ai;
MVMGrapheme32 bi;
/* Collation order numbers */
MVMint32 ai_coll_val;
MVMint32 bi_coll_val;
MVM_string_check_arg(tc, a, "compare");
MVM_string_check_arg(tc, b, "compare");
/* Simple cases when one or both are zero length. */
alen = MVM_string_graphs(tc, a);
blen = MVM_string_graphs(tc, b);
if (alen == 0)
return blen == 0 ? 0 : -1;
if (blen == 0)
return 1;
/* Otherwise, need to scan them. */
scanlen = alen > blen ? blen : alen;
for (i = 0; i < scanlen; i++) {
ai = MVM_string_get_grapheme_at_nocheck(tc, a, i);
bi = MVM_string_get_grapheme_at_nocheck(tc, b, i);
/* If they are the same grapheme */
if (ai != bi) {
/* only try and get a property if the value it is isn't a synthetic
* grapheme. we should change from get_grapheme to something to get the
* NFC form */
if ( ai >= 0 || bi >= 0 ) {
/* Get the primary collation value for the grapheme */
ai_coll_val = MVM_unicode_collation_primary(tc, ai);
bi_coll_val = MVM_unicode_collation_primary(tc, bi);
/* If we don't find a collation value,
we should compare by codepoint */
/* Eventually we should try and catch codepoints that don't
have a collation value. We would then need to decompose it and
apply weighting based on their decomposed values. */
if (ai_coll_val == 0 || bi_coll_val == 0) {
/* return -10 or 10 to indicate we didn't use the collation
algorithm */
return ai < bi ? -10 :
ai > bi ? 10 :
0 ;
}
/* If both have primary collation values ( they are not 0 ) */
if ( (ai_coll_val != 0 && bi_coll_val != 0) && (ai_coll_val != bi_coll_val) ) {
return ai_coll_val < bi_coll_val ? -1 : 1;
}
/* If both have the same primary collation values */
ai_coll_val += MVM_unicode_collation_secondary(tc, ai);
bi_coll_val += MVM_unicode_collation_secondary(tc, bi);
if ( (ai_coll_val != 0 && bi_coll_val != 0) && (ai_coll_val != bi_coll_val) ) {
return ai_coll_val < bi_coll_val ? -2 : 2;
}
/* If both have the same tertiary collation values */
ai_coll_val = MVM_unicode_collation_tertiary(tc, ai);
bi_coll_val = MVM_unicode_collation_tertiary(tc, bi);
if ( (ai_coll_val != 0 && bi_coll_val != 0) && (ai_coll_val != bi_coll_val) ) {
return ai_coll_val < bi_coll_val ? -3 : 3;
}
/* All the collation values were equal. Check codepoints */
return ai < bi ? -4 :
ai > bi ? 4 :
0 ;
}
/* For now, if it's a synthetic codepoint just compare by codepoint. */
else {
return ai < bi ? -10 :
ai > bi ? 10 :
1 ;
}
}
}
/* All shared chars equal, so go on length. */
return alen < blen ? -1 :
alen > blen ? 1 :
0 ;
}
/* Looks up a codepoint by name. Lazily constructs a hash. */
MVMGrapheme32 MVM_unicode_lookup_by_name(MVMThreadContext *tc, MVMString *name) {
MVMuint64 size;
char *cname = MVM_string_ascii_encode(tc, name, &size, 0);
size_t cname_len = strlen((const char *) cname );
MVMUnicodeNameRegistry *result;
if (!codepoints_by_name) {
generate_codepoints_by_name(tc);
}
HASH_FIND(hash_handle, codepoints_by_name, cname, cname_len, result);
MVM_free(cname);
return result ? result->codepoint : -1;
}
MVMString * MVM_unicode_get_name(MVMThreadContext *tc, MVMint64 codepoint) {
const char *name;
/* Catch out-of-bounds code points. */
if (codepoint < 0) {
name = "<illegal>";
}
else if (codepoint > 0x10ffff) {
name = "<unassigned>";
}
/* Look up name. */
else {
MVMuint32 codepoint_row = MVM_codepoint_to_row_index(tc, codepoint);
if (codepoint_row != -1) {
name = codepoint_names[codepoint_row];
if (!name) {
while (codepoint_row && !codepoint_names[codepoint_row])
codepoint_row--;
name = codepoint_names[codepoint_row];
if (!name || name[0] != '<')
name = "<reserved>";
}
}
else {
name = "<illegal>";
}
}
return MVM_string_ascii_decode(tc, tc->instance->VMString, name, strlen(name));
}
MVMString * MVM_unicode_codepoint_get_property_str(MVMThreadContext *tc, MVMGrapheme32 codepoint, MVMint64 property_code) {
const char * const str = MVM_unicode_get_property_str(tc, codepoint, property_code);
if (!str)
return tc->instance->str_consts.empty;
return MVM_string_ascii_decode(tc, tc->instance->VMString, str, strlen(str));
}
const char * MVM_unicode_codepoint_get_property_cstr(MVMThreadContext *tc, MVMGrapheme32 codepoint, MVMint64 property_code) {
return MVM_unicode_get_property_str(tc, codepoint, property_code);
}
MVMint64 MVM_unicode_codepoint_get_property_int(MVMThreadContext *tc, MVMGrapheme32 codepoint, MVMint64 property_code) {
if (property_code == 0)
return 0;
return (MVMint64)MVM_unicode_get_property_int(tc, codepoint, property_code);
}
MVMint64 MVM_unicode_codepoint_get_property_bool(MVMThreadContext *tc, MVMGrapheme32 codepoint, MVMint64 property_code) {
if (property_code == 0)
return 0;
return (MVMint64)MVM_unicode_get_property_int(tc, codepoint, property_code) != 0;
}
MVMint64 MVM_unicode_codepoint_has_property_value(MVMThreadContext *tc, MVMGrapheme32 codepoint, MVMint64 property_code, MVMint64 property_value_code) {
if (property_code == 0)
return 0;
return (MVMint64)MVM_unicode_get_property_int(tc,
codepoint, property_code) == property_value_code ? 1 : 0;
}
/* Looks if there is a case change for the provided codepoint. Since a case
* change may produce multiple codepoints occasionally, then we return 0 if
* the case change is a no-op, and otherwise the number of codepoints. The
* codepoints argument will be set to a pointer to a buffer where those code
* points can be read from. The caller must not mutate the buffer, nor free
* it. */
MVMuint32 MVM_unicode_get_case_change(MVMThreadContext *tc, MVMCodepoint codepoint, MVMint32 case_,
const MVMCodepoint **result) {
if (case_ == MVM_unicode_case_change_type_fold) {
MVMint32 folding_index = MVM_unicode_get_property_int(tc,
codepoint, MVM_UNICODE_PROPERTY_CASE_FOLDING);
if (folding_index) {
MVMint32 is_simple = MVM_unicode_get_property_int(tc,
codepoint, MVM_UNICODE_PROPERTY_CASE_FOLDING_SIMPLE);
if (is_simple) {
*result = &(CaseFolding_simple_table[folding_index]);
return 1;
}
else {
MVMint32 i = 3;
while (i > 0 && CaseFolding_grows_table[folding_index][i - 1] == 0)
i--;
*result = &(CaseFolding_grows_table[folding_index][0]);
return i;
}
}
}
else {
MVMint32 special_casing_index = MVM_unicode_get_property_int(tc,
codepoint, MVM_UNICODE_PROPERTY_SPECIAL_CASING);
if (special_casing_index) {
MVMint32 i = 3;
while (i > 0 && SpecialCasing_table[special_casing_index][case_][i - 1] == 0)
i--;
*result = SpecialCasing_table[special_casing_index][case_];
return i;
}
else {
MVMint32 changes_index = MVM_unicode_get_property_int(tc,
codepoint, MVM_UNICODE_PROPERTY_CASE_CHANGE_INDEX);
if (changes_index) {
const MVMCodepoint *found = &(case_changes[changes_index][case_]);
if (*found != 0) {
*result = found;
return 1;
}
}
}
}
return 0;
}
/* XXX make all the statics members of the global MVM instance instead? */
static MVMUnicodeNameRegistry *property_codes_by_names_aliases;
static MVMUnicodeNameRegistry *property_codes_by_seq_names;
static void generate_property_codes_by_names_aliases(MVMThreadContext *tc) {
MVMuint32 num_names = num_unicode_property_keypairs;
while (num_names--) {
MVMUnicodeNameRegistry *entry = MVM_malloc(sizeof(MVMUnicodeNameRegistry));
entry->name = (char *)unicode_property_keypairs[num_names].name;
entry->codepoint = unicode_property_keypairs[num_names].value;
HASH_ADD_KEYPTR(hash_handle, property_codes_by_names_aliases,
entry->name, strlen(entry->name), entry);
}
}
static void generate_property_codes_by_seq_names(MVMThreadContext *tc) {
MVMuint32 num_names = num_unicode_seq_keypairs;
while (num_names--) {
MVMUnicodeGraphemeNameRegistry *entry = MVM_malloc(sizeof(MVMUnicodeGraphemeNameRegistry));
entry->name = (char *)uni_seq_pairs[num_names].name;
entry->structindex = uni_seq_pairs[num_names].value;
HASH_ADD_KEYPTR(hash_handle, property_codes_by_seq_names,
entry->name, strlen(entry->name), entry);
}
}
MVMint32 MVM_unicode_name_to_property_code(MVMThreadContext *tc, MVMString *name) {
MVMuint64 size;
char *cname = MVM_string_ascii_encode(tc, name, &size, 0);
MVMUnicodeNameRegistry *result;
if (!property_codes_by_names_aliases) {
generate_property_codes_by_names_aliases(tc);
}
HASH_FIND(hash_handle, property_codes_by_names_aliases, cname, strlen((const char *)cname), result);
MVM_free(cname); /* not really codepoint, really just an index */
return result ? result->codepoint : 0;
}
static void generate_unicode_property_values_hashes(MVMThreadContext *tc) {
MVMUnicodeNameRegistry **hash_array = MVM_calloc(MVM_NUM_PROPERTY_CODES, sizeof(MVMUnicodeNameRegistry *));
MVMuint32 index = 0;
MVMUnicodeNameRegistry *entry = NULL, *binaries = NULL;
for ( ; index < num_unicode_property_value_keypairs; index++) {
MVMint32 property_code = unicode_property_value_keypairs[index].value >> 24;
entry = MVM_malloc(sizeof(MVMUnicodeNameRegistry));
entry->name = (char *)unicode_property_value_keypairs[index].name;
entry->codepoint = unicode_property_value_keypairs[index].value & 0xFFFFFF;
HASH_ADD_KEYPTR(hash_handle, hash_array[property_code],
entry->name, strlen(entry->name), entry);
}
for (index = 0; index < MVM_NUM_PROPERTY_CODES; index++) {
if (!hash_array[index]) {
if (!binaries) {
MVMUnicodeNamedValue yes[8] = { {"T",1}, {"Y",1},
{"Yes",1}, {"yes",1}, {"True",1}, {"true",1}, {"t",1}, {"y",1} };
MVMUnicodeNamedValue no [8] = { {"F",0}, {"N",0},
{"No",0}, {"no",0}, {"False",0}, {"false",0}, {"f",0}, {"n",0} };
MVMuint8 i;
for (i = 0; i < 8; i++) {
entry = MVM_malloc(sizeof(MVMUnicodeNameRegistry));
entry->name = (char *)yes[i].name;
entry->codepoint = yes[i].value;
HASH_ADD_KEYPTR(hash_handle, binaries, yes[i].name, strlen(yes[i].name), entry);
}
for (i = 0; i < 8; i++) {
entry = MVM_malloc(sizeof(MVMUnicodeNameRegistry));
entry->name = (char *)no[i].name;
entry->codepoint = no[i].value;
HASH_ADD_KEYPTR(hash_handle, binaries, no[i].name, strlen(no[i].name), entry);
}
}
hash_array[index] = binaries;
}
}
unicode_property_values_hashes = hash_array;
}
MVMint32 MVM_unicode_name_to_property_value_code(MVMThreadContext *tc, MVMint64 property_code, MVMString *name) {
if (property_code <= 0 || property_code >= MVM_NUM_PROPERTY_CODES) {
return 0;
}
else {
MVMuint64 size;
char *cname = MVM_string_ascii_encode(tc, name, &size, 0);
MVMUnicodeNameRegistry *result;
HASH_FIND(hash_handle, unicode_property_values_hashes[property_code], cname, strlen((const char *)cname), result);
MVM_free(cname); /* not really codepoint, really just an index */
return result ? result->codepoint : 0;
}
}
MVMint32 MVM_unicode_cname_to_property_value_code(MVMThreadContext *tc, MVMint64 property_code, const char *cname, size_t cname_length) {
if (property_code <= 0 || property_code >= MVM_NUM_PROPERTY_CODES) {
return 0;
}
else {
MVMuint64 size;
MVMUnicodeNameRegistry *result;
HASH_FIND(hash_handle, unicode_property_values_hashes[property_code], cname, cname_length, result);
return result ? result->codepoint : 0;
}
}
/* Look up the primary composite for a pair of codepoints, if it exists.
* Returns 0 if not. */
MVMCodepoint MVM_unicode_find_primary_composite(MVMThreadContext *tc, MVMCodepoint l, MVMCodepoint c) {
MVMint32 lower = l & 0xFF;
MVMint32 upper = (l >> 8) & 0xFF;
MVMint32 plane = (l >> 16) & 0xF;
const MVMint32 *pcs = comp_p[plane][upper][lower];
if (pcs) {
MVMint32 entries = pcs[0];
MVMint32 i;
for (i = 1; i < entries; i += 2)
if (pcs[i] == c)
return pcs[i + 1];
}
return 0;
}
static uv_mutex_t property_hash_count_mutex;
static int property_hash_count = 0;
static uv_once_t property_hash_count_guard = UV_ONCE_INIT;
static void setup_property_mutex(void)
{
uv_mutex_init(&property_hash_count_mutex);
}
void MVM_unicode_init(MVMThreadContext *tc)
{
uv_once(&property_hash_count_guard, setup_property_mutex);
uv_mutex_lock(&property_hash_count_mutex);
if (property_hash_count == 0) {
generate_unicode_property_values_hashes(tc);
}
property_hash_count++;
uv_mutex_unlock(&property_hash_count_mutex);
}
void MVM_unicode_release(MVMThreadContext *tc)
{
uv_mutex_lock(&property_hash_count_mutex);
property_hash_count--;
if (property_hash_count == 0) {
int i;
for (i = 0; i < MVM_NUM_PROPERTY_CODES; i++) {
MVMUnicodeNameRegistry *entry;
MVMUnicodeNameRegistry *tmp;
unsigned bucket_tmp;
int j;
if (!unicode_property_values_hashes[i]) {
continue;
}
for(j = i + 1; j < MVM_NUM_PROPERTY_CODES; j++) {
if (unicode_property_values_hashes[i] == unicode_property_values_hashes[j]) {
unicode_property_values_hashes[j] = NULL;
}
}
HASH_ITER(hash_handle, unicode_property_values_hashes[i], entry, tmp, bucket_tmp) {
HASH_DELETE(hash_handle, unicode_property_values_hashes[i], entry);
MVM_free(entry);
}
assert(!unicode_property_values_hashes[i]);
}
MVM_free(unicode_property_values_hashes);
unicode_property_values_hashes = NULL;
}
uv_mutex_unlock(&property_hash_count_mutex);
}
/* Looks up a grapheme by name. Lazily constructs a hash. */
MVMString * MVM_unicode_string_from_name(MVMThreadContext *tc, MVMString *name) {
MVMuint64 size;
char *cname = MVM_string_ascii_encode(tc, name, &size, 0);
MVMUnicodeGraphemeNameRegistry *result;
MVMGrapheme32 result_graph;
const MVMint32 * uni_seq;
int array_size;
result_graph = MVM_unicode_lookup_by_name(tc, name);
/* If it's just a codepoint, return that */
if (result_graph >= 0) {
MVM_free(cname);
return MVM_string_chr(tc, result_graph);
}
if (!property_codes_by_seq_names) {
generate_property_codes_by_seq_names(tc);
}
HASH_FIND(hash_handle, property_codes_by_seq_names, cname, strlen((const char *)cname), result);
MVM_free(cname);
/* If we can't find a result return an empty string */
if (!result)
return tc->instance->str_consts.empty;
uni_seq = uni_seq_enum[result->structindex];
/* The first element is the number of codepoints in the sequence */
array_size = uni_seq[0];
return MVM_unicode_codepoints_c_array_to_nfg_string(tc, (MVMCodepoint *) uni_seq + 1, array_size);
}