-
-
Notifications
You must be signed in to change notification settings - Fork 914
Expand file tree
/
Copy pathicuexportdata.cpp
More file actions
1525 lines (1360 loc) · 62.8 KB
/
Copy pathicuexportdata.cpp
File metadata and controls
1525 lines (1360 loc) · 62.8 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include "unicode/localpointer.h"
#include "unicode/umachine.h"
#include "unicode/unistr.h"
#include "unicode/urename.h"
#include "unicode/uset.h"
#include <vector>
#include <algorithm>
#include "toolutil.h"
#include "uoptions.h"
#include "cmemory.h"
#include "charstr.h"
#include "cstring.h"
#include "unicode/uchar.h"
#include "unicode/errorcode.h"
#include "unicode/uniset.h"
#include "unicode/uscript.h"
#include "unicode/putil.h"
#include "unicode/umutablecptrie.h"
#include "unicode/ucharstriebuilder.h"
#include "ucase.h"
#include "unicode/normalizer2.h"
#include "uprops.h"
#include "normalizer2impl.h"
#include "writesrc.h"
U_NAMESPACE_USE
/*
* Global - verbosity
*/
UBool VERBOSE = false;
UBool QUIET = false;
UBool haveCopyright = true;
UCPTrieType trieType = UCPTRIE_TYPE_SMALL;
const char* destdir = "";
// Mask constants for modified values in the Script CodePointTrie, values are logically 12-bits.
int16_t DATAEXPORT_SCRIPT_X_WITH_COMMON = 0x0400;
int16_t DATAEXPORT_SCRIPT_X_WITH_INHERITED = 0x0800;
int16_t DATAEXPORT_SCRIPT_X_WITH_OTHER = 0x0c00;
void handleError(ErrorCode& status, int line, const char* context) {
if (status.isFailure()) {
std::cerr << "Error[" << line << "]: " << context << ": " << status.errorName() << std::endl;
exit(status.reset());
}
}
class PropertyValueNameGetter : public ValueNameGetter {
public:
PropertyValueNameGetter(UProperty prop) : property(prop) {}
~PropertyValueNameGetter() override;
const char *getName(uint32_t value) override {
return u_getPropertyValueName(property, value, U_SHORT_PROPERTY_NAME);
}
private:
UProperty property;
};
PropertyValueNameGetter::~PropertyValueNameGetter() {}
// Dump an aliases = [...] key for properties with aliases
void dumpPropertyAliases(UProperty uproperty, FILE* f) {
int i = U_LONG_PROPERTY_NAME + 1;
while(true) {
// The API works by having extra names after U_LONG_PROPERTY_NAME, sequentially,
// and returning null after that
const char* alias = u_getPropertyName(uproperty, static_cast<UPropertyNameChoice>(i));
if (!alias) {
break;
}
if (i == U_LONG_PROPERTY_NAME + 1) {
fprintf(f, "aliases = [\"%s\"", alias);
} else {
fprintf(f, ", \"%s\"", alias);
}
i++;
}
if (i != U_LONG_PROPERTY_NAME + 1) {
fprintf(f, "]\n");
}
}
void dumpBinaryProperty(UProperty uproperty, FILE* f) {
IcuToolErrorCode status("icuexportdata: dumpBinaryProperty");
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
const USet* uset = u_getBinaryPropertySet(uproperty, status);
handleError(status, __LINE__, fullPropName);
fputs("[[binary_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
dumpPropertyAliases(uproperty, f);
usrc_writeUnicodeSet(f, uset, UPRV_TARGET_SYNTAX_TOML);
}
// If the value exists, dump an indented entry of the format
// `" {discr = <discriminant>, long = <longname>, short = <shortname>, aliases = [<aliases>]},"`
void dumpValueEntry(UProperty uproperty, int v, bool is_mask, FILE* f) {
const char* fullValueName = u_getPropertyValueName(uproperty, v, U_LONG_PROPERTY_NAME);
const char* shortValueName = u_getPropertyValueName(uproperty, v, U_SHORT_PROPERTY_NAME);
if (!fullValueName) {
return;
}
if (is_mask) {
fprintf(f, " {discr = 0x%X", v);
} else {
fprintf(f, " {discr = %i", v);
}
fprintf(f, ", long = \"%s\"", fullValueName);
if (shortValueName) {
fprintf(f, ", short = \"%s\"", shortValueName);
}
int i = U_LONG_PROPERTY_NAME + 1;
while(true) {
// The API works by having extra names after U_LONG_PROPERTY_NAME, sequentially,
// and returning null after that
const char* alias = u_getPropertyValueName(uproperty, v, static_cast<UPropertyNameChoice>(i));
if (!alias) {
break;
}
if (i == U_LONG_PROPERTY_NAME + 1) {
fprintf(f, ", aliases = [\"%s\"", alias);
} else {
fprintf(f, ", \"%s\"", alias);
}
i++;
}
if (i != U_LONG_PROPERTY_NAME + 1) {
fprintf(f, "]");
}
fprintf(f, "},\n");
}
void dumpEnumeratedProperty(UProperty uproperty, FILE* f) {
IcuToolErrorCode status("icuexportdata: dumpEnumeratedProperty");
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
const UCPMap* umap = u_getIntPropertyMap(uproperty, status);
handleError(status, __LINE__, fullPropName);
fputs("[[enum_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
dumpPropertyAliases(uproperty, f);
int32_t minValue = u_getIntPropertyMinValue(uproperty);
U_ASSERT(minValue >= 0);
int32_t maxValue = u_getIntPropertyMaxValue(uproperty);
U_ASSERT(maxValue >= 0);
fprintf(f, "values = [\n");
for (int v = minValue; v <= maxValue; v++) {
dumpValueEntry(uproperty, v, false, f);
}
fprintf(f, "]\n");
PropertyValueNameGetter valueNameGetter(uproperty);
usrc_writeUCPMap(f, umap, &valueNameGetter, UPRV_TARGET_SYNTAX_TOML);
fputs("\n", f);
UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_32;
if (maxValue <= 0xff) {
width = UCPTRIE_VALUE_BITS_8;
} else if (maxValue <= 0xffff) {
width = UCPTRIE_VALUE_BITS_16;
}
LocalUMutableCPTriePointer builder(umutablecptrie_fromUCPMap(umap, status));
LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
builder.getAlias(),
trieType,
width,
status));
handleError(status, __LINE__, fullPropName);
fputs("[enum_property.code_point_trie]\n", f);
usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
}
/*
* Export Bidi_Mirroring_Glyph values (code points) in a similar way to how enumerated
* properties are dumped to file.
* Note: the data will store 0 for code points without a value defined for
* Bidi_Mirroring_Glyph.
*/
void dumpBidiMirroringGlyph(FILE* f) {
UProperty uproperty = UCHAR_BIDI_MIRRORING_GLYPH;
IcuToolErrorCode status("icuexportdata: dumpBidiMirroringGlyph");
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
handleError(status, __LINE__, fullPropName);
// Store 21-bit code point as is
UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_32;
// note: unlike dumpEnumeratedProperty, which can get inversion map data using
// u_getIntPropertyMap(uproperty), the only reliable way to get Bidi_Mirroring_Glyph
// is to use u_charMirror(cp) over the code point space.
LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
for(UChar32 c = UCHAR_MIN_VALUE; c <= UCHAR_MAX_VALUE; c++) {
UChar32 mirroringGlyph = u_charMirror(c);
// The trie builder code throws an error when it cannot compress the data sufficiently.
// Therefore, when the value is undefined for a code point, keep a 0 in the trie
// instead of the ICU API behavior of returning the code point value. Using 0
// results in a relatively significant space savings by not including redundant data.
if (c != mirroringGlyph) {
umutablecptrie_set(builder.getAlias(), c, mirroringGlyph, status);
}
}
LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
builder.getAlias(),
trieType,
width,
status));
handleError(status, __LINE__, fullPropName);
// currently a trie and inversion map are the same (as relied upon in characterproperties.cpp)
const UCPMap* umap = reinterpret_cast<UCPMap *>(utrie.getAlias());
fputs("[[enum_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
if (shortPropName) {
fprintf(f, "short_name = \"%s\"\n", shortPropName);
}
fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
dumpPropertyAliases(uproperty, f);
usrc_writeUCPMap(f, umap, nullptr, UPRV_TARGET_SYNTAX_TOML);
fputs("\n", f);
fputs("[enum_property.code_point_trie]\n", f);
usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
}
/*
* Export Numeric_Value values in a similar way to how enumerated
* properties are dumped to file.
*/
void dumpNumericValue(FILE* f) {
IcuToolErrorCode status("icuexportdata: dumpNumericValue");
UProperty uproperty = UCHAR_NUMERIC_VALUE;
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_32;
LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
for(UChar32 c = UCHAR_MIN_VALUE; c <= UCHAR_MAX_VALUE; c++) {
int32_t ntv = static_cast<int32_t>(GET_NUMERIC_TYPE_VALUE(u_getMainProperties(c)));
if (ntv != UPROPS_NTV_NONE) {
umutablecptrie_set(builder.getAlias(), c, ntv, status);
}
}
LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
builder.getAlias(),
trieType,
width,
status));
handleError(status, __LINE__, fullPropName);
fputs("[[enum_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
fprintf(f, "upropert_discr = 0x%X\n", uproperty);
dumpPropertyAliases(uproperty, f);
const UCPMap* umap = reinterpret_cast<UCPMap *>(utrie.getAlias());
usrc_writeUCPMap(f, umap, nullptr, UPRV_TARGET_SYNTAX_TOML);
fputs("\n", f);
fputs("[enum_property.code_point_trie]\n", f);
usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
}
// After printing property value `v`, print `mask` if and only if `mask` comes immediately
// after the property in the listing
void maybeDumpMaskValue(UProperty uproperty, uint32_t v, uint32_t mask, FILE* f) {
if (U_MASK(v) < mask && U_MASK(v + 1) > mask)
dumpValueEntry(uproperty, mask, true, f);
}
void dumpGeneralCategoryMask(FILE* f) {
IcuToolErrorCode status("icuexportdata: dumpGeneralCategoryMask");
UProperty uproperty = UCHAR_GENERAL_CATEGORY_MASK;
fputs("[[mask_property]]\n", f);
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
dumpPropertyAliases(uproperty, f);
fprintf(f, "mask_for = \"General_Category\"\n");
int32_t minValue = u_getIntPropertyMinValue(UCHAR_GENERAL_CATEGORY);
U_ASSERT(minValue >= 0);
int32_t maxValue = u_getIntPropertyMaxValue(UCHAR_GENERAL_CATEGORY);
U_ASSERT(maxValue >= 0);
fprintf(f, "values = [\n");
for (int32_t v = minValue; v <= maxValue; v++) {
dumpValueEntry(uproperty, U_MASK(v), true, f);
// We want to dump these masks "in order", which means they
// should come immediately after every property they contain
maybeDumpMaskValue(uproperty, v, U_GC_L_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_LC_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_M_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_N_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_Z_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_C_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_P_MASK, f);
maybeDumpMaskValue(uproperty, v, U_GC_S_MASK, f);
}
fprintf(f, "]\n");
}
namespace {
void U_CALLCONV
set_add(USet *set, UChar32 c) {
UnicodeSet::fromUSet(set)->add(c);
}
void U_CALLCONV
set_addRange(USet *set, UChar32 start, UChar32 end) {
UnicodeSet::fromUSet(set)->add(start, end);
}
}
UnicodeSet getScriptExtensionsCodePoints(IcuToolErrorCode &errorCode) {
UnicodeSet scxCPs;
USetAdder sa = {
scxCPs.toUSet(),
set_add,
set_addRange,
nullptr, // don't need addString,
nullptr, // don't need remove()
nullptr // don't need removeRange()
};
uprv_addScriptExtensionsCodePoints(&sa, errorCode);
return scxCPs;
}
void dumpScriptExtensions(FILE* f) {
IcuToolErrorCode status("icuexportdata: dumpScriptExtensions");
fputs("[[script_extensions]]\n", f);
const char* scxFullPropName = u_getPropertyName(UCHAR_SCRIPT_EXTENSIONS, U_LONG_PROPERTY_NAME);
const char* scxShortPropName = u_getPropertyName(UCHAR_SCRIPT_EXTENSIONS, U_SHORT_PROPERTY_NAME);
fprintf(f, "long_name = \"%s\"\n", scxFullPropName);
if (scxShortPropName) fprintf(f, "short_name = \"%s\"\n", scxShortPropName);
fprintf(f, "uproperty_discr = 0x%X\n", UCHAR_SCRIPT_EXTENSIONS);
dumpPropertyAliases(UCHAR_SCRIPT_EXTENSIONS, f);
// We want to use 16 bits for our exported trie of sc/scx data because we
// need 12 bits to match the 12 bits of data stored for sc/scx in the trie
// in the uprops.icu data file.
UCPTrieValueWidth scWidth = UCPTRIE_VALUE_BITS_16;
// Create a mutable UCPTrie builder populated with Script property values data.
const UCPMap* scInvMap = u_getIntPropertyMap(UCHAR_SCRIPT, status);
handleError(status, __LINE__, scxFullPropName);
LocalUMutableCPTriePointer builder(umutablecptrie_fromUCPMap(scInvMap, status));
handleError(status, __LINE__, scxFullPropName);
// The values for the output scx companion array.
// Invariant is that all subvectors are distinct.
std::vector< std::vector<uint16_t> > outputDedupVec;
// The sc/scx companion array is an array of arrays (of script codes)
fputs("script_code_array = [\n", f);
UnicodeSet scxCodePoints = getScriptExtensionsCodePoints(status);
for(const UChar32 cp : scxCodePoints.codePoints()) {
// Get the Script value
uint32_t scVal = umutablecptrie_get(builder.getAlias(), cp);
// Get the Script_Extensions value (array of Script codes)
const int32_t SCX_ARRAY_CAPACITY = 32;
UScriptCode scxValArray[SCX_ARRAY_CAPACITY];
int32_t numScripts = uscript_getScriptExtensions(cp, scxValArray, SCX_ARRAY_CAPACITY, status);
handleError(status, __LINE__, scxFullPropName);
// Convert the scx array into a vector
std::vector<uint16_t> scxValVec;
for(int i = 0; i < numScripts; i++) {
scxValVec.push_back(scxValArray[i]);
}
// Ensure that it is sorted
std::sort(scxValVec.begin(), scxValVec.end());
// Copy the Script value into the first position of the scx array only
// if we have the "other" case (Script value is not Common nor Inherited).
// This offers faster access when users want only the Script value.
if (scVal != USCRIPT_COMMON && scVal != USCRIPT_INHERITED) {
scxValVec.insert(scxValVec.begin(), scVal);
}
// See if there is already an scx value array matching the newly built one.
// If there is, then use its index.
// If not, then append the new value array.
bool isScxValUnique = true;
size_t outputIndex = 0;
for (outputIndex = 0; outputIndex < outputDedupVec.size(); outputIndex++) {
if (outputDedupVec[outputIndex] == scxValVec) {
isScxValUnique = false;
break;
}
}
if (isScxValUnique) {
outputDedupVec.push_back(scxValVec);
usrc_writeArray(f, " [", scxValVec.data(), 16, scxValVec.size(), " ", "],\n");
}
// We must update the value in the UCPTrie for the code point to contain:
// 9..0 the Script code in the lower 10 bits when 11..10 is 0, else it is
// the index into the companion array
// 11..10 the same higher-order 2 bits in the trie in uprops.icu indicating whether
// 3: other
// 2: Script=Inherited
// 1: Script=Common
// 0: Script=value in 9..0 (N/A because we are in this loop to create the companion array for non-0 cases)
uint16_t mask = 0;
if (scVal == USCRIPT_COMMON) {
mask = DATAEXPORT_SCRIPT_X_WITH_COMMON;
} else if (scVal == USCRIPT_INHERITED) {
mask = DATAEXPORT_SCRIPT_X_WITH_INHERITED;
} else {
mask = DATAEXPORT_SCRIPT_X_WITH_OTHER;
}
// The new trie value is the index into the new array with the high order bits set
uint32_t newScVal = outputIndex | mask;
// Update the code point in the mutable trie builder with the trie value
umutablecptrie_set(builder.getAlias(), cp, newScVal, status);
handleError(status, __LINE__, scxFullPropName);
}
fputs("]\n\n", f); // Print the TOML close delimiter for the outer array.
// Convert from mutable trie builder to immutable trie.
LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
builder.getAlias(),
trieType,
scWidth,
status));
handleError(status, __LINE__, scxFullPropName);
fputs("[script_extensions.code_point_trie]\n", f);
usrc_writeUCPTrie(f, scxShortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
}
FILE* prepareOutputFile(const char* basename) {
IcuToolErrorCode status("icuexportdata");
CharString outFileName;
if (destdir != nullptr && *destdir != 0) {
outFileName.append(destdir, status).ensureEndsWithFileSeparator(status);
}
outFileName.append(basename, status);
outFileName.append(".toml", status);
handleError(status, __LINE__, basename);
FILE* f = fopen(outFileName.data(), "w");
if (f == nullptr) {
std::cerr << "Unable to open file: " << outFileName.data() << std::endl;
exit(U_FILE_ACCESS_ERROR);
}
if (!QUIET) {
std::cout << "Writing to: " << outFileName.data() << std::endl;
}
if (haveCopyright) {
usrc_writeCopyrightHeader(f, "#", 2021);
}
usrc_writeFileNameGeneratedBy(f, "#", basename, "icuexportdata.cpp");
return f;
}
#if !UCONFIG_NO_NORMALIZATION
class PendingDescriptor {
public:
UChar32 scalar;
uint32_t descriptorOrFlags;
// If false, we use the above fields only. If true, descriptor only
// contains the two highest-bit flags and the rest is computed later
// from the fields below.
UBool complex;
UBool supplementary;
UBool onlyNonStartersInTrail;
uint32_t len;
uint32_t offset;
PendingDescriptor(UChar32 scalar, uint32_t descriptor);
PendingDescriptor(UChar32 scalar, uint32_t flags, UBool supplementary, UBool onlyNonStartersInTrail, uint32_t len, uint32_t offset);
};
PendingDescriptor::PendingDescriptor(UChar32 scalar, uint32_t descriptor)
: scalar(scalar), descriptorOrFlags(descriptor), complex(false), supplementary(false), onlyNonStartersInTrail(false), len(0), offset(0) {}
PendingDescriptor::PendingDescriptor(UChar32 scalar, uint32_t flags, UBool supplementary, UBool onlyNonStartersInTrail, uint32_t len, uint32_t offset)
: scalar(scalar), descriptorOrFlags(flags), complex(true), supplementary(supplementary), onlyNonStartersInTrail(onlyNonStartersInTrail), len(len), offset(offset) {}
void writeCanonicalCompositions(USet* backwardCombiningStarters) {
IcuToolErrorCode status("icuexportdata: computeCanonicalCompositions");
const char* basename = "compositions";
FILE* f = prepareOutputFile(basename);
LocalPointer<UCharsTrieBuilder> backwardBuilder(new UCharsTrieBuilder(status), status);
const int32_t DECOMPOSITION_BUFFER_SIZE = 20;
UChar32 utf32[DECOMPOSITION_BUFFER_SIZE];
const Normalizer2* nfc = Normalizer2::getNFCInstance(status);
for (UChar32 c = 0; c <= 0x10FFFF; ++c) {
if (c >= 0xD800 && c < 0xE000) {
// Surrogate
continue;
}
UnicodeString decomposition;
if (!nfc->getRawDecomposition(c, decomposition)) {
continue;
}
int32_t len = decomposition.toUTF32(utf32, DECOMPOSITION_BUFFER_SIZE, status);
if (len != 2) {
continue;
}
UChar32 starter = utf32[0];
UChar32 second = utf32[1];
UChar32 composite = nfc->composePair(starter, second);
if (composite < 0) {
continue;
}
if (c != composite) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
if (!u_getCombiningClass(second)) {
uset_add(backwardCombiningStarters, second);
}
if (composite >= 0xAC00 && composite <= 0xD7A3) {
// Hangul syllable
continue;
}
UnicodeString backward;
backward.append(second);
backward.append(starter);
backwardBuilder->add(backward, static_cast<int32_t>(composite), status);
}
UnicodeString canonicalCompositionTrie;
backwardBuilder->buildUnicodeString(USTRINGTRIE_BUILD_SMALL, canonicalCompositionTrie, status);
usrc_writeArray(f, "compositions = [\n ", canonicalCompositionTrie.getBuffer(), 16, canonicalCompositionTrie.length(), " ", "\n]\n");
fclose(f);
handleError(status, __LINE__, basename);
}
void writeDecompositionTables(const char* basename, const uint16_t* ptr16, size_t len16, const uint32_t* ptr32, size_t len32) {
FILE* f = prepareOutputFile(basename);
usrc_writeArray(f, "scalars16 = [\n ", ptr16, 16, len16, " ", "\n]\n");
usrc_writeArray(f, "scalars32 = [\n ", ptr32, 32, len32, " ", "\n]\n");
fclose(f);
}
void pendingInsertionsToTrie(const char* basename, UMutableCPTrie* trie, const std::vector<PendingDescriptor>& pendingTrieInsertions, uint32_t baseSize16, uint32_t baseSize32, uint32_t supplementSize16) {
IcuToolErrorCode status("icuexportdata: pendingInsertionsToTrie");
// Iterate backwards to insert lower code points in the trie first in case it matters
// for trie block allocation.
for (int32_t i = pendingTrieInsertions.size() - 1; i >= 0; --i) {
const PendingDescriptor& pending = pendingTrieInsertions[i];
if (pending.complex) {
uint32_t additional = 0;
uint32_t offset = pending.offset;
uint32_t len = pending.len;
if (!pending.supplementary) {
len -= 2;
if (offset >= baseSize16) {
// This is a offset to supplementary 16-bit data. We have
// 16-bit base data and 32-bit base data before. However,
// the 16-bit base data length is already part of offset.
additional = baseSize32;
}
} else {
len -= 1;
if (offset >= baseSize32) {
// This is an offset to supplementary 32-bit data. We have 16-bit
// base data, 32-bit base data, and 16-bit supplementary data before.
// However, the 32-bit base data length is already part
// of offset.
additional = baseSize16 + supplementSize16;
} else {
// This is an offset to 32-bit base data. We have 16-bit
// base data before.
additional = baseSize16;
}
}
// +1 to make offset always non-zero
offset += 1;
if (offset + additional > 0xFFF) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
if (len > 7) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
umutablecptrie_set(trie, pending.scalar, pending.descriptorOrFlags | (uint32_t(pending.onlyNonStartersInTrail) << 4) | len | (offset + additional) << 16, status);
} else {
umutablecptrie_set(trie, pending.scalar, pending.descriptorOrFlags, status);
}
}
}
/// Marker that the decomposition does not round trip via NFC.
const uint32_t NON_ROUND_TRIP_MASK = (1 << 30);
/// Marker that the first character of the decomposition can combine
/// backwards.
const uint32_t BACKWARD_COMBINING_MASK = (1 << 31);
void writeDecompositionData(const char* basename, uint32_t baseSize16, uint32_t baseSize32, uint32_t supplementSize16, USet* uset, USet* reference, const std::vector<PendingDescriptor>& pendingTrieInsertions, const std::vector<PendingDescriptor>& nfdPendingTrieInsertions, char16_t passthroughCap) {
IcuToolErrorCode status("icuexportdata: writeDecompositionData");
FILE* f = prepareOutputFile(basename);
// Zero is a magic number that means the character decomposes to itself.
LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
if (uprv_strcmp(basename, "uts46d") != 0) {
// Make surrogates decompose to U+FFFD. Don't do this for UTS 46, since this
// optimization is only used by the UTF-16 slice mode, and UTS 46 is not
// supported in slice modes (which do not support ignorables).
// Mark these as potentially backward-combining, to make lead surrogates
// for non-BMP characters that are backward-combining count as
// backward-combining just in case, though the backward-combiningness
// is not actually being looked at today.
umutablecptrie_setRange(builder.getAlias(), 0xD800, 0xDFFF, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK | 0xFFFD, status);
}
// Add a marker value for Hangul syllables
umutablecptrie_setRange(builder.getAlias(), 0xAC00, 0xD7A3, 1, status);
// First put the NFD data in the trie, to be partially overwritten in the NFKD and UTS 46 cases.
// This is easier that changing the logic that computes the pending insertions.
pendingInsertionsToTrie(basename, builder.getAlias(), nfdPendingTrieInsertions, baseSize16, baseSize32, supplementSize16);
pendingInsertionsToTrie(basename, builder.getAlias(), pendingTrieInsertions, baseSize16, baseSize32, supplementSize16);
LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
builder.getAlias(),
trieType,
UCPTRIE_VALUE_BITS_32,
status));
handleError(status, __LINE__, basename);
// The ICU4X side has changed enough this whole block of expectation checking might be more appropriate to remove.
if (reference) {
if (uset_contains(reference, 0xFF9E) || uset_contains(reference, 0xFF9F) || !uset_contains(reference, 0x0345)) {
// NFD expectations don't hold. The set must not contain the half-width
// kana voicing marks and must contain iota subscript.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
USet* halfWidthVoicing = uset_openEmpty();
uset_add(halfWidthVoicing, 0xFF9E);
uset_add(halfWidthVoicing, 0xFF9F);
USet* iotaSubscript = uset_openEmpty();
uset_add(iotaSubscript, 0x0345);
USet* halfWidthCheck = uset_cloneAsThawed(uset);
uset_removeAll(halfWidthCheck, reference);
if (!uset_equals(halfWidthCheck, halfWidthVoicing) && !uset_isEmpty(halfWidthCheck)) {
// The result was neither empty nor contained exactly
// the two half-width voicing marks. The ICU4X
// normalizer doesn't know how to deal with this case.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
uset_close(halfWidthCheck);
USet* iotaCheck = uset_cloneAsThawed(reference);
uset_removeAll(iotaCheck, uset);
if (!(uset_equals(iotaCheck, iotaSubscript)) && !uset_isEmpty(iotaCheck)) {
// The result was neither empty nor contained exactly
// the iota subscript. The ICU4X normalizer doesn't
// know how to deal with this case.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
uset_close(iotaSubscript);
uset_close(halfWidthVoicing);
}
fprintf(f, "cap = 0x%X\n", passthroughCap);
fprintf(f, "[trie]\n");
usrc_writeUCPTrie(f, "trie", utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
fclose(f);
handleError(status, __LINE__, basename);
}
// Find the slice `needle` within `storage` and return its index, failing which,
// append all elements of `needle` to `storage` and return the index of it at the end.
template<typename T>
size_t findOrAppend(std::vector<T>& storage, const UChar32* needle, size_t needleLen) {
// Last index where we might find the start of the complete needle.
// bounds check is `i + needleLen <= storage.size()` since the inner
// loop will range from `i` to `i + needleLen - 1` (the `-1` is why we use `<=`)
for (size_t i = 0; i + needleLen <= storage.size(); i++) {
for (size_t j = 0;; j++) {
if (j == needleLen) {
return i; // found a match
}
if (storage[i + j] != static_cast<uint32_t>(needle[j])) {
break;
}
}
}
// We didn't find anything. Append, keeping the append index in mind.
size_t index = storage.size();
for(size_t i = 0; i < needleLen; i++) {
storage.push_back(static_cast<T>(needle[i]));
}
return index;
}
// Computes data for canonical decompositions
// See components/normalizer/trie-value-format.md in the ICU4X repo
// for documentation of the trie value format.
void computeDecompositions(const char* basename,
const USet* backwardCombiningStarters,
std::vector<uint16_t>& storage16,
std::vector<uint32_t>& storage32,
USet* decompositionStartsWithNonStarter,
USet* decompositionStartsWithBackwardCombiningStarter,
std::vector<PendingDescriptor>& pendingTrieInsertions,
UChar32& decompositionPassthroughBound,
UChar32& compositionPassthroughBound) {
IcuToolErrorCode status("icuexportdata: computeDecompositions");
const Normalizer2* mainNormalizer;
const Normalizer2* nfdNormalizer = Normalizer2::getNFDInstance(status);
const Normalizer2* nfcNormalizer = Normalizer2::getNFCInstance(status);
FILE* f = nullptr;
std::vector<uint32_t> nonRecursive32;
LocalUMutableCPTriePointer nonRecursiveBuilder(umutablecptrie_open(0, 0, status));
UBool uts46 = false;
if (uprv_strcmp(basename, "nfkd") == 0) {
mainNormalizer = Normalizer2::getNFKDInstance(status);
} else if (uprv_strcmp(basename, "uts46d") == 0) {
uts46 = true;
mainNormalizer = Normalizer2::getInstance(nullptr, "uts46", UNORM2_COMPOSE, status);
} else {
mainNormalizer = nfdNormalizer;
f = prepareOutputFile("decompositionex");
}
// Max length as of Unicode 14 is 4 for NFD. For NFKD the max
// is 18 (U+FDFA; special-cased), and the next longest is 8 (U+FDFB).
const int32_t LONGEST_ENCODABLE_LENGTH_16 = 9;
const int32_t LONGEST_ENCODABLE_LENGTH_32 = 8;
const int32_t DECOMPOSITION_BUFFER_SIZE = 20;
UChar32 utf32[DECOMPOSITION_BUFFER_SIZE];
const int32_t RAW_DECOMPOSITION_BUFFER_SIZE = 2;
UChar32 rawUtf32[RAW_DECOMPOSITION_BUFFER_SIZE];
// Iterate over all scalar values excluding Hangul syllables.
//
// We go backwards in order to better find overlapping decompositions.
//
// As of Unicode 14:
// Iterate forward without overlap search:
// nfd: 16 size: 896, 32 size: 173
// nfkd: 16 size: 3854, 32 size: 179
//
// Iterate forward with overlap search:
// nfd: 16 size: 888, 32 size: 173
// nfkd: 16 size: 3266, 32 size: 179
//
// Iterate backward with overlap search:
// nfd: 16 size: 776, 32 size: 173
// nfkd: 16 size: 2941, 32 size: 179
//
// UChar32 is signed!
for (UChar32 c = 0x10FFFF; c >= 0; --c) {
if (c >= 0xAC00 && c <= 0xD7A3) {
// Hangul syllable
continue;
}
if (c >= 0xD800 && c < 0xE000) {
// Surrogate
continue;
}
if (c == 0xFFFD) {
// REPLACEMENT CHARACTER
// This character is a starter that decomposes to self,
// so without a special case here it would end up as
// passthrough-eligible in all normalizations forms.
// However, in the potentially-ill-formed UTF-8 case
// UTF-8 errors return U+FFFD from the iterator, and
// errors need to be treated as ineligible for
// passthrough on the slice fast path. By giving
// U+FFFD a trie value whose flags make it ineligible
// for passthrough avoids a specific U+FFFD branch on
// the passthrough fast path.
pendingTrieInsertions.push_back({c, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK});
continue;
}
UnicodeString src;
UnicodeString dst;
src.append(c);
if (mainNormalizer != nfdNormalizer) {
UnicodeString inter;
mainNormalizer->normalize(src, inter, status);
nfdNormalizer->normalize(inter, dst, status);
} else {
nfdNormalizer->normalize(src, dst, status);
}
UnicodeString nfc;
nfcNormalizer->normalize(dst, nfc, status);
UBool roundTripsViaCanonicalComposition = (src == nfc);
int32_t len = dst.toUTF32(utf32, DECOMPOSITION_BUFFER_SIZE, status);
if (!len || (len == 1 && utf32[0] == 0xFFFD && c != 0xFFFD)) {
if (!uts46) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
}
if (len > DECOMPOSITION_BUFFER_SIZE) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
uint8_t firstCombiningClass = u_getCombiningClass(utf32[0]);
bool specialNonStarterDecomposition = false;
bool startsWithBackwardCombiningStarter = false;
if (firstCombiningClass) {
decompositionPassthroughBound = c;
compositionPassthroughBound = c;
uset_add(decompositionStartsWithNonStarter, c);
if (src != dst) {
if (c == 0x0340 || c == 0x0341 || c == 0x0343 || c == 0x0344 || c == 0x0F73 || c == 0x0F75 || c == 0x0F81 || (c == 0xFF9E && utf32[0] == 0x3099) || (c == 0xFF9F && utf32[0] == 0x309A)) {
specialNonStarterDecomposition = true;
} else {
// A character whose decomposition starts with a non-starter and isn't the same as the character itself and isn't already hard-coded into ICU4X.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
}
} else if (uset_contains(backwardCombiningStarters, utf32[0])) {
compositionPassthroughBound = c;
startsWithBackwardCombiningStarter = true;
uset_add(decompositionStartsWithBackwardCombiningStarter, c);
}
if (mainNormalizer != nfdNormalizer) {
UnicodeString nfd;
nfdNormalizer->normalize(src, nfd, status);
if (dst == nfd) {
continue;
}
decompositionPassthroughBound = c;
compositionPassthroughBound = c;
}
if (firstCombiningClass) {
len = 1;
if (specialNonStarterDecomposition) {
// Special marker
pendingTrieInsertions.push_back({c, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK | 0xD900 | u_getCombiningClass(c)});
} else {
// Use the surrogate range to store the canonical combining class
// XXX: Should non-started that decompose to self be marked as non-round-trippable in
// case such semantics turn out to be more useful for `NON_ROUND_TRIP_MASK`?
pendingTrieInsertions.push_back({c, BACKWARD_COMBINING_MASK | 0xD800 | static_cast<uint32_t>(firstCombiningClass)});
}
continue;
} else {
if (src == dst) {
if (startsWithBackwardCombiningStarter) {
pendingTrieInsertions.push_back({c, BACKWARD_COMBINING_MASK});
}
continue;
}
decompositionPassthroughBound = c;
// ICU4X hard-codes ANGSTROM SIGN
if (c != 0x212B && mainNormalizer == nfdNormalizer) {
UnicodeString raw;
if (!nfdNormalizer->getRawDecomposition(c, raw)) {
// We're always supposed to have a non-recursive decomposition
// if we had a recursive one.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
// In addition to actual difference, put the whole range that contains characters
// with oxia into the non-recursive trie in order to catch cases where characters
// with oxia have singleton decompositions to corresponding characters with tonos.
// This way, the run-time decision to fall through can be done on the range
// without checking for individual characters inside the range.
if (raw != dst || (c >= 0x1F71 && c <= 0x1FFB)) {
int32_t rawLen = raw.toUTF32(rawUtf32, RAW_DECOMPOSITION_BUFFER_SIZE, status);
if (!rawLen) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
if (rawLen == 1) {
if (c >= 0xFFFF) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, static_cast<uint32_t>(rawUtf32[0]), status);
} else if (rawUtf32[0] <= 0xFFFF && rawUtf32[1] <= 0xFFFF) {
if (!rawUtf32[0] || !rawUtf32[1]) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
// Swapped for consistency with the primary trie
uint32_t bmpPair = static_cast<uint32_t>(rawUtf32[1]) << 16 | static_cast<uint32_t>(rawUtf32[0]);
umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, bmpPair, status);
} else {
// Let's add 1 to index to make it always non-zero to distinguish
// it from the default zero.
uint32_t index = nonRecursive32.size() + 1;
nonRecursive32.push_back(static_cast<uint32_t>(rawUtf32[0]));
nonRecursive32.push_back(static_cast<uint32_t>(rawUtf32[1]));
if (index > 0xFFFF) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, index << 16, status);
}
}
}
}
if (!roundTripsViaCanonicalComposition) {
compositionPassthroughBound = c;
}
if (!len) {
if (!uts46) {
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
pendingTrieInsertions.push_back({c, uint32_t(0xFFFFFFFF)});
} else if (len == 1 && ((utf32[0] >= 0x1161 && utf32[0] <= 0x1175) || (utf32[0] >= 0x11A8 && utf32[0] <= 0x11C2))) {
// Singleton decompositions to conjoining jamo.
if (mainNormalizer == nfdNormalizer) {
// Not supposed to happen in NFD
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | NON_ROUND_TRIP_MASK | (startsWithBackwardCombiningStarter ? BACKWARD_COMBINING_MASK : 0)});
} else if (!startsWithBackwardCombiningStarter && len == 1 && utf32[0] <= 0xFFFF) {
pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | NON_ROUND_TRIP_MASK | (startsWithBackwardCombiningStarter ? BACKWARD_COMBINING_MASK : 0)});
} else if (c != 0x212B && // ANGSTROM SIGN is special to make the Harfbuzz case branch less in the more common case.
!startsWithBackwardCombiningStarter &&
len == 2 &&
utf32[0] <= 0x7FFF &&
utf32[1] <= 0x7FFF &&
utf32[0] > 0x1F &&
utf32[1] > 0x1F &&
!u_getCombiningClass(utf32[0]) &&
u_getCombiningClass(utf32[1])) {
for (int32_t i = 0; i < len; ++i) {
if (((utf32[i] == 0x0345) && (uprv_strcmp(basename, "uts46d") == 0)) || utf32[i] == 0xFF9E || utf32[i] == 0xFF9F) {
// Assert that iota subscript and half-width voicing marks never occur in these
// expansions in the normalization forms where they are special.
status.set(U_INTERNAL_PROGRAM_ERROR);
handleError(status, __LINE__, basename);
}
}
pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | (static_cast<uint32_t>(utf32[1]) << 15) | (roundTripsViaCanonicalComposition ? 0 : NON_ROUND_TRIP_MASK)});
} else {
UBool supplementary = false;
UBool nonInitialStarter = false;
for (int32_t i = 0; i < len; ++i) {
if (((utf32[i] == 0x0345) && (uprv_strcmp(basename, "uts46d") == 0)) || utf32[i] == 0xFF9E || utf32[i] == 0xFF9F) {
// Assert that iota subscript and half-width voicing marks never occur in these
// expansions in the normalization forms where they are special.
status.set(U_INTERNAL_PROGRAM_ERROR);