Debian Codegraph @

intl/icu/source/tools/icuexportdata/icuexportdata.cpp

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include <cstddef>
5 #include <cstdint>
6 #include <cstdio>
7 #include <iostream>
8 #include "unicode/localpointer.h"
9 #include "unicode/umachine.h"
10 #include "unicode/unistr.h"
11 #include "unicode/urename.h"
12 #include "unicode/uset.h"
13 #include <vector>
14 #include <algorithm>
15 #include "toolutil.h"
16 #include "uoptions.h"
17 #include "cmemory.h"
18 #include "charstr.h"
19 #include "cstring.h"
20 #include "unicode/uchar.h"
21 #include "unicode/errorcode.h"
22 #include "unicode/uniset.h"
23 #include "unicode/uscript.h"
24 #include "unicode/putil.h"
25 #include "unicode/umutablecptrie.h"
26 #include "unicode/ucharstriebuilder.h"
27 #include "ucase.h"
28 #include "unicode/normalizer2.h"
29 #include "normalizer2impl.h"
30 #include "writesrc.h"
31
32 U_NAMESPACE_USE
33
34 /*
35 * Global - verbosity
36 */
37 UBool VERBOSE = false;
38 UBool QUIET = false;
39
40 UBool haveCopyright = true;
41 UCPTrieType trieType = UCPTRIE_TYPE_SMALL;
42 const char* destdir = "";
43
44 // Mask constants for modified values in the Script CodePointTrie, values are logically 12-bits.
45 int16_t DATAEXPORT_SCRIPT_X_WITH_COMMON = 0x0400;
46 int16_t DATAEXPORT_SCRIPT_X_WITH_INHERITED = 0x0800;
47 int16_t DATAEXPORT_SCRIPT_X_WITH_OTHER = 0x0c00;
48
49 // TODO(ICU-21821): Replace this with a call to a library function
50 // This is an array of all code points with explicit scx values, and can be generated the quick and dirty
51 // way with this script:
52 //
53 // # <ScriptExtensions.txt python script.py
54 //
55 // import sys
56 // for line in sys.stdin:
57 // line = line.strip()
58 // if len(line) == 0 or line.startswith("#"):
59 // continue
60 // entry = line.split(" ")[0]
61 // # Either it is a range
62 // if ".." in entry:
63 // split = entry.split("..")
64 // start = int(split[0], 16)
65 // end = int(split[1], 16)
66 // # +
67 // for ch in range(start, end + 1):
68 // print("0x%04x, " % ch, end="")
69 // # or a single code point
70 // else:
71 // print("0x%s, " % entry.lower(), end="")
72
73 int32_t scxCodePoints[] = {
74 0x00b7, 0x02bc, 0x02c7, 0x02c9, 0x02ca, 0x02cb, 0x02cd, 0x02d7, 0x02d9, 0x0300, 0x0301, 0x0302,
75 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030a, 0x030b, 0x030c, 0x030d, 0x030e,
76 0x0310, 0x0311, 0x0313, 0x0320, 0x0323, 0x0324, 0x0325, 0x032d, 0x032e, 0x0330, 0x0331, 0x0342,
77 0x0345, 0x0358, 0x035e, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367, 0x0368, 0x0369, 0x036a, 0x036b,
78 0x036c, 0x036d, 0x036e, 0x036f, 0x0374, 0x0375, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 0x0589,
79 0x060c, 0x061b, 0x061c, 0x061f, 0x0640, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f, 0x0650, 0x0651,
80 0x0652, 0x0653, 0x0654, 0x0655, 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667,
81 0x0668, 0x0669, 0x0670, 0x06d4, 0x0951, 0x0952, 0x0964, 0x0965, 0x0966, 0x0967, 0x0968, 0x0969,
82 0x096a, 0x096b, 0x096c, 0x096d, 0x096e, 0x096f, 0x09e6, 0x09e7, 0x09e8, 0x09e9, 0x09ea, 0x09eb,
83 0x09ec, 0x09ed, 0x09ee, 0x09ef, 0x0a66, 0x0a67, 0x0a68, 0x0a69, 0x0a6a, 0x0a6b, 0x0a6c, 0x0a6d,
84 0x0a6e, 0x0a6f, 0x0ae6, 0x0ae7, 0x0ae8, 0x0ae9, 0x0aea, 0x0aeb, 0x0aec, 0x0aed, 0x0aee, 0x0aef,
85 0x0be6, 0x0be7, 0x0be8, 0x0be9, 0x0bea, 0x0beb, 0x0bec, 0x0bed, 0x0bee, 0x0bef, 0x0bf0, 0x0bf1,
86 0x0bf2, 0x0bf3, 0x0ce6, 0x0ce7, 0x0ce8, 0x0ce9, 0x0cea, 0x0ceb, 0x0cec, 0x0ced, 0x0cee, 0x0cef,
87 0x1040, 0x1041, 0x1042, 0x1043, 0x1044, 0x1045, 0x1046, 0x1047, 0x1048, 0x1049, 0x10fb, 0x16eb,
88 0x16ec, 0x16ed, 0x1735, 0x1736, 0x1802, 0x1803, 0x1805, 0x1cd0, 0x1cd1, 0x1cd2, 0x1cd3, 0x1cd4,
89 0x1cd5, 0x1cd6, 0x1cd7, 0x1cd8, 0x1cd9, 0x1cda, 0x1cdb, 0x1cdc, 0x1cdd, 0x1cde, 0x1cdf, 0x1ce0,
90 0x1ce1, 0x1ce2, 0x1ce3, 0x1ce4, 0x1ce5, 0x1ce6, 0x1ce7, 0x1ce8, 0x1ce9, 0x1cea, 0x1ceb, 0x1cec,
91 0x1ced, 0x1cee, 0x1cef, 0x1cf0, 0x1cf1, 0x1cf2, 0x1cf3, 0x1cf4, 0x1cf5, 0x1cf6, 0x1cf7, 0x1cf8,
92 0x1cf9, 0x1cfa, 0x1dc0, 0x1dc1, 0x1df8, 0x1dfa, 0x202f, 0x204f, 0x205a, 0x205d, 0x20f0, 0x2e17,
93 0x2e30, 0x2e31, 0x2e3c, 0x2e41, 0x2e43, 0x2ff0, 0x2ff1, 0x2ff2, 0x2ff3, 0x2ff4, 0x2ff5, 0x2ff6,
94 0x2ff7, 0x2ff8, 0x2ff9, 0x2ffa, 0x2ffb, 0x2ffc, 0x2ffd, 0x2ffe, 0x2fff, 0x3001, 0x3002, 0x3003,
95 0x3006, 0x3008, 0x3009, 0x300a, 0x300b, 0x300c, 0x300d, 0x300e, 0x300f, 0x3010, 0x3011, 0x3013,
96 0x3014, 0x3015, 0x3016, 0x3017, 0x3018, 0x3019, 0x301a, 0x301b, 0x301c, 0x301d, 0x301e, 0x301f,
97 0x302a, 0x302b, 0x302c, 0x302d, 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3037, 0x303c,
98 0x303d, 0x303e, 0x303f, 0x3099, 0x309a, 0x309b, 0x309c, 0x30a0, 0x30fb, 0x30fc, 0x3190, 0x3191,
99 0x3192, 0x3193, 0x3194, 0x3195, 0x3196, 0x3197, 0x3198, 0x3199, 0x319a, 0x319b, 0x319c, 0x319d,
100 0x319e, 0x319f, 0x31c0, 0x31c1, 0x31c2, 0x31c3, 0x31c4, 0x31c5, 0x31c6, 0x31c7, 0x31c8, 0x31c9,
101 0x31ca, 0x31cb, 0x31cc, 0x31cd, 0x31ce, 0x31cf, 0x31d0, 0x31d1, 0x31d2, 0x31d3, 0x31d4, 0x31d5,
102 0x31d6, 0x31d7, 0x31d8, 0x31d9, 0x31da, 0x31db, 0x31dc, 0x31dd, 0x31de, 0x31df, 0x31e0, 0x31e1,
103 0x31e2, 0x31e3, 0x31e4, 0x31e5, 0x31ef, 0x3220, 0x3221, 0x3222, 0x3223, 0x3224, 0x3225, 0x3226,
104 0x3227, 0x3228, 0x3229, 0x322a, 0x322b, 0x322c, 0x322d, 0x322e, 0x322f, 0x3230, 0x3231, 0x3232,
105 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239, 0x323a, 0x323b, 0x323c, 0x323d, 0x323e,
106 0x323f, 0x3240, 0x3241, 0x3242, 0x3243, 0x3244, 0x3245, 0x3246, 0x3247, 0x3280, 0x3281, 0x3282,
107 0x3283, 0x3284, 0x3285, 0x3286, 0x3287, 0x3288, 0x3289, 0x328a, 0x328b, 0x328c, 0x328d, 0x328e,
108 0x328f, 0x3290, 0x3291, 0x3292, 0x3293, 0x3294, 0x3295, 0x3296, 0x3297, 0x3298, 0x3299, 0x329a,
109 0x329b, 0x329c, 0x329d, 0x329e, 0x329f, 0x32a0, 0x32a1, 0x32a2, 0x32a3, 0x32a4, 0x32a5, 0x32a6,
110 0x32a7, 0x32a8, 0x32a9, 0x32aa, 0x32ab, 0x32ac, 0x32ad, 0x32ae, 0x32af, 0x32b0, 0x32c0, 0x32c1,
111 0x32c2, 0x32c3, 0x32c4, 0x32c5, 0x32c6, 0x32c7, 0x32c8, 0x32c9, 0x32ca, 0x32cb, 0x32ff, 0x3358,
112 0x3359, 0x335a, 0x335b, 0x335c, 0x335d, 0x335e, 0x335f, 0x3360, 0x3361, 0x3362, 0x3363, 0x3364,
113 0x3365, 0x3366, 0x3367, 0x3368, 0x3369, 0x336a, 0x336b, 0x336c, 0x336d, 0x336e, 0x336f, 0x3370,
114 0x337b, 0x337c, 0x337d, 0x337e, 0x337f, 0x33e0, 0x33e1, 0x33e2, 0x33e3, 0x33e4, 0x33e5, 0x33e6,
115 0x33e7, 0x33e8, 0x33e9, 0x33ea, 0x33eb, 0x33ec, 0x33ed, 0x33ee, 0x33ef, 0x33f0, 0x33f1, 0x33f2,
116 0x33f3, 0x33f4, 0x33f5, 0x33f6, 0x33f7, 0x33f8, 0x33f9, 0x33fa, 0x33fb, 0x33fc, 0x33fd, 0x33fe,
117 0xa66f, 0xa700, 0xa701, 0xa702, 0xa703, 0xa704, 0xa705, 0xa706, 0xa707, 0xa830, 0xa831, 0xa832,
118 0xa833, 0xa834, 0xa835, 0xa836, 0xa837, 0xa838, 0xa839, 0xa8f1, 0xa8f3, 0xa92e, 0xa9cf, 0xfd3e,
119 0xfd3f, 0xfdf2, 0xfdfd, 0xfe45, 0xfe46, 0xff61, 0xff62, 0xff63, 0xff64, 0xff65, 0xff70, 0xff9e,
120 0xff9f, 0x10100, 0x10101, 0x10102, 0x10107, 0x10108, 0x10109, 0x1010a, 0x1010b, 0x1010c, 0x1010d,
121 0x1010e, 0x1010f, 0x10110, 0x10111, 0x10112, 0x10113, 0x10114, 0x10115, 0x10116, 0x10117, 0x10118,
122 0x10119, 0x1011a, 0x1011b, 0x1011c, 0x1011d, 0x1011e, 0x1011f, 0x10120, 0x10121, 0x10122, 0x10123,
123 0x10124, 0x10125, 0x10126, 0x10127, 0x10128, 0x10129, 0x1012a, 0x1012b, 0x1012c, 0x1012d, 0x1012e,
124 0x1012f, 0x10130, 0x10131, 0x10132, 0x10133, 0x10137, 0x10138, 0x10139, 0x1013a, 0x1013b, 0x1013c,
125 0x1013d, 0x1013e, 0x1013f, 0x102e0, 0x102e1, 0x102e2, 0x102e3, 0x102e4, 0x102e5, 0x102e6, 0x102e7,
126 0x102e8, 0x102e9, 0x102ea, 0x102eb, 0x102ec, 0x102ed, 0x102ee, 0x102ef, 0x102f0, 0x102f1, 0x102f2,
127 0x102f3, 0x102f4, 0x102f5, 0x102f6, 0x102f7, 0x102f8, 0x102f9, 0x102fa, 0x102fb, 0x10af2, 0x11301,
128 0x11303, 0x1133b, 0x1133c, 0x11fd0, 0x11fd1, 0x11fd3, 0x1bca0, 0x1bca1, 0x1bca2, 0x1bca3, 0x1d360,
129 0x1d361, 0x1d362, 0x1d363, 0x1d364, 0x1d365, 0x1d366, 0x1d367, 0x1d368, 0x1d369, 0x1d36a, 0x1d36b,
130 0x1d36c, 0x1d36d, 0x1d36e, 0x1d36f, 0x1d370, 0x1d371, 0x1f250, 0x1f251,
131 };
132
133 void handleError(ErrorCode& status, int line, const char* context) {
134 if (status.isFailure()) {
135 std::cerr << "Error[" << line << "]: " << context << ": " << status.errorName() << std::endl;
136 exit(status.reset());
137 }
138 }
139
140 class PropertyValueNameGetter : public ValueNameGetter {
141 public:
142 PropertyValueNameGetter(UProperty prop) : property(prop) {}
143 ~PropertyValueNameGetter() override;
144 const char *getName(uint32_t value) override {
145 return u_getPropertyValueName(property, value, U_SHORT_PROPERTY_NAME);
146 }
147
148 private:
149 UProperty property;
150 };
151
152 PropertyValueNameGetter::~PropertyValueNameGetter() {}
153
154 // Dump an aliases = [...] key for properties with aliases
155 void dumpPropertyAliases(UProperty uproperty, FILE* f) {
156 int i = U_LONG_PROPERTY_NAME + 1;
157
158 while(true) {
159 // The API works by having extra names after U_LONG_PROPERTY_NAME, sequentially,
160 // and returning null after that
161 const char* alias = u_getPropertyName(uproperty, static_cast<UPropertyNameChoice>(i));
162 if (!alias) {
163 break;
164 }
165 if (i == U_LONG_PROPERTY_NAME + 1) {
166 fprintf(f, "aliases = [\"%s\"", alias);
167 } else {
168 fprintf(f, ", \"%s\"", alias);
169 }
170 i++;
171 }
172 if (i != U_LONG_PROPERTY_NAME + 1) {
173 fprintf(f, "]\n");
174 }
175 }
176
177 void dumpBinaryProperty(UProperty uproperty, FILE* f) {
178 IcuToolErrorCode status("icuexportdata: dumpBinaryProperty");
179 const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
180 const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
181 const USet* uset = u_getBinaryPropertySet(uproperty, status);
182 handleError(status, __LINE__, fullPropName);
183
184 fputs("[[binary_property]]\n", f);
185 fprintf(f, "long_name = \"%s\"\n", fullPropName);
186 if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
187 fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
188 dumpPropertyAliases(uproperty, f);
189 usrc_writeUnicodeSet(f, uset, UPRV_TARGET_SYNTAX_TOML);
190 }
191
192 // If the value exists, dump an indented entry of the format
193 // `" {discr = <discriminant>, long = <longname>, short = <shortname>, aliases = [<aliases>]},"`
194 void dumpValueEntry(UProperty uproperty, int v, bool is_mask, FILE* f) {
195 const char* fullValueName = u_getPropertyValueName(uproperty, v, U_LONG_PROPERTY_NAME);
196 const char* shortValueName = u_getPropertyValueName(uproperty, v, U_SHORT_PROPERTY_NAME);
197 if (!fullValueName) {
198 return;
199 }
200 if (is_mask) {
201 fprintf(f, " {discr = 0x%X", v);
202 } else {
203 fprintf(f, " {discr = %i", v);
204 }
205 fprintf(f, ", long = \"%s\"", fullValueName);
206 if (shortValueName) {
207 fprintf(f, ", short = \"%s\"", shortValueName);
208 }
209 int i = U_LONG_PROPERTY_NAME + 1;
210 while(true) {
211 // The API works by having extra names after U_LONG_PROPERTY_NAME, sequentially,
212 // and returning null after that
213 const char* alias = u_getPropertyValueName(uproperty, v, static_cast<UPropertyNameChoice>(i));
214 if (!alias) {
215 break;
216 }
217 if (i == U_LONG_PROPERTY_NAME + 1) {
218 fprintf(f, ", aliases = [\"%s\"", alias);
219 } else {
220 fprintf(f, ", \"%s\"", alias);
221 }
222 i++;
223 }
224 if (i != U_LONG_PROPERTY_NAME + 1) {
225 fprintf(f, "]");
226 }
227 fprintf(f, "},\n");
228 }
229
230 void dumpEnumeratedProperty(UProperty uproperty, FILE* f) {
231 IcuToolErrorCode status("icuexportdata: dumpEnumeratedProperty");
232 const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
233 const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
234 const UCPMap* umap = u_getIntPropertyMap(uproperty, status);
235 handleError(status, __LINE__, fullPropName);
236
237 fputs("[[enum_property]]\n", f);
238 fprintf(f, "long_name = \"%s\"\n", fullPropName);
239 if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
240 fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
241 dumpPropertyAliases(uproperty, f);
242
243 int32_t minValue = u_getIntPropertyMinValue(uproperty);
244 U_ASSERT(minValue >= 0);
245 int32_t maxValue = u_getIntPropertyMaxValue(uproperty);
246 U_ASSERT(maxValue >= 0);
247
248 fprintf(f, "values = [\n");
249 for (int v = minValue; v <= maxValue; v++) {
250 dumpValueEntry(uproperty, v, false, f);
251 }
252 fprintf(f, "]\n");
253
254 PropertyValueNameGetter valueNameGetter(uproperty);
255 usrc_writeUCPMap(f, umap, &valueNameGetter, UPRV_TARGET_SYNTAX_TOML);
256 fputs("\n", f);
257
258
259 UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_32;
260 if (maxValue <= 0xff) {
261 width = UCPTRIE_VALUE_BITS_8;
262 } else if (maxValue <= 0xffff) {
263 width = UCPTRIE_VALUE_BITS_16;
264 }
265 LocalUMutableCPTriePointer builder(umutablecptrie_fromUCPMap(umap, status));
266 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
267 builder.getAlias(),
268 trieType,
269 width,
270 status));
271 handleError(status, __LINE__, fullPropName);
272
273 fputs("[enum_property.code_point_trie]\n", f);
274 usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
275 }
276
277 /*
278 * Export Bidi_Mirroring_Glyph values (code points) in a similar way to how enumerated
279 * properties are dumped to file.
280 * Note: the data will store 0 for code points without a value defined for
281 * Bidi_Mirroring_Glyph.
282 */
283 void dumpBidiMirroringGlyph(FILE* f) {
284 UProperty uproperty = UCHAR_BIDI_MIRRORING_GLYPH;
285 IcuToolErrorCode status("icuexportdata: dumpBidiMirroringGlyph");
286 const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
287 const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
288 handleError(status, __LINE__, fullPropName);
289
290 // Store 21-bit code point as is
291 UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_32;
292
293 // note: unlike dumpEnumeratedProperty, which can get inversion map data using
294 // u_getIntPropertyMap(uproperty), the only reliable way to get Bidi_Mirroring_Glyph
295 // is to use u_charMirror(cp) over the code point space.
296 LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
297 for(UChar32 c = UCHAR_MIN_VALUE; c <= UCHAR_MAX_VALUE; c++) {
298 UChar32 mirroringGlyph = u_charMirror(c);
299 // The trie builder code throws an error when it cannot compress the data sufficiently.
300 // Therefore, when the value is undefined for a code point, keep a 0 in the trie
301 // instead of the ICU API behavior of returning the code point value. Using 0
302 // results in a relatively significant space savings by not including redundant data.
303 if (c != mirroringGlyph) {
304 umutablecptrie_set(builder.getAlias(), c, mirroringGlyph, status);
305 }
306 }
307
308 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
309 builder.getAlias(),
310 trieType,
311 width,
312 status));
313 handleError(status, __LINE__, fullPropName);
314
315 // currently a trie and inversion map are the same (as relied upon in characterproperties.cpp)
316 const UCPMap* umap = reinterpret_cast<UCPMap *>(utrie.getAlias());
317
318 fputs("[[enum_property]]\n", f);
319 fprintf(f, "long_name = \"%s\"\n", fullPropName);
320 if (shortPropName) {
321 fprintf(f, "short_name = \"%s\"\n", shortPropName);
322 }
323 fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
324 dumpPropertyAliases(uproperty, f);
325
326 usrc_writeUCPMap(f, umap, nullptr, UPRV_TARGET_SYNTAX_TOML);
327 fputs("\n", f);
328
329 fputs("[enum_property.code_point_trie]\n", f);
330 usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
331 }
332
333 // After printing property value `v`, print `mask` if and only if `mask` comes immediately
334 // after the property in the listing
335 void maybeDumpMaskValue(UProperty uproperty, uint32_t v, uint32_t mask, FILE* f) {
336 if (U_MASK(v) < mask && U_MASK(v + 1) > mask)
337 dumpValueEntry(uproperty, mask, true, f);
338 }
339
340 void dumpGeneralCategoryMask(FILE* f) {
341 IcuToolErrorCode status("icuexportdata: dumpGeneralCategoryMask");
342 UProperty uproperty = UCHAR_GENERAL_CATEGORY_MASK;
343
344 fputs("[[mask_property]]\n", f);
345 const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
346 const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
347 fprintf(f, "long_name = \"%s\"\n", fullPropName);
348 if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
349 fprintf(f, "uproperty_discr = 0x%X\n", uproperty);
350 dumpPropertyAliases(uproperty, f);
351
352
353 fprintf(f, "mask_for = \"General_Category\"\n");
354 int32_t minValue = u_getIntPropertyMinValue(UCHAR_GENERAL_CATEGORY);
355 U_ASSERT(minValue >= 0);
356 int32_t maxValue = u_getIntPropertyMaxValue(UCHAR_GENERAL_CATEGORY);
357 U_ASSERT(maxValue >= 0);
358
359 fprintf(f, "values = [\n");
360 for (int32_t v = minValue; v <= maxValue; v++) {
361 dumpValueEntry(uproperty, U_MASK(v), true, f);
362
363 // We want to dump these masks "in order", which means they
364 // should come immediately after every property they contain
365 maybeDumpMaskValue(uproperty, v, U_GC_L_MASK, f);
366 maybeDumpMaskValue(uproperty, v, U_GC_LC_MASK, f);
367 maybeDumpMaskValue(uproperty, v, U_GC_M_MASK, f);
368 maybeDumpMaskValue(uproperty, v, U_GC_N_MASK, f);
369 maybeDumpMaskValue(uproperty, v, U_GC_Z_MASK, f);
370 maybeDumpMaskValue(uproperty, v, U_GC_C_MASK, f);
371 maybeDumpMaskValue(uproperty, v, U_GC_P_MASK, f);
372 maybeDumpMaskValue(uproperty, v, U_GC_S_MASK, f);
373 }
374 fprintf(f, "]\n");
375 }
376
377 void dumpScriptExtensions(FILE* f) {
378 IcuToolErrorCode status("icuexportdata: dumpScriptExtensions");
379
380 fputs("[[script_extensions]]\n", f);
381 const char* scxFullPropName = u_getPropertyName(UCHAR_SCRIPT_EXTENSIONS, U_LONG_PROPERTY_NAME);
382 const char* scxShortPropName = u_getPropertyName(UCHAR_SCRIPT_EXTENSIONS, U_SHORT_PROPERTY_NAME);
383 fprintf(f, "long_name = \"%s\"\n", scxFullPropName);
384 if (scxShortPropName) fprintf(f, "short_name = \"%s\"\n", scxShortPropName);
385 fprintf(f, "uproperty_discr = 0x%X\n", UCHAR_SCRIPT_EXTENSIONS);
386 dumpPropertyAliases(UCHAR_SCRIPT_EXTENSIONS, f);
387
388 // We want to use 16 bits for our exported trie of sc/scx data because we
389 // need 12 bits to match the 12 bits of data stored for sc/scx in the trie
390 // in the uprops.icu data file.
391 UCPTrieValueWidth scWidth = UCPTRIE_VALUE_BITS_16;
392
393 // Create a mutable UCPTrie builder populated with Script property values data.
394 const UCPMap* scInvMap = u_getIntPropertyMap(UCHAR_SCRIPT, status);
395 handleError(status, __LINE__, scxFullPropName);
396 LocalUMutableCPTriePointer builder(umutablecptrie_fromUCPMap(scInvMap, status));
397 handleError(status, __LINE__, scxFullPropName);
398
399 // The values for the output scx companion array.
400 // Invariant is that all subvectors are distinct.
401 std::vector< std::vector<uint16_t> > outputDedupVec;
402
403 // The sc/scx companion array is an array of arrays (of script codes)
404 fputs("script_code_array = [\n", f);
405 for(const UChar32 cp : scxCodePoints) {
406 // Get the Script value
407 uint32_t scVal = umutablecptrie_get(builder.getAlias(), cp);
408 // Get the Script_Extensions value (array of Script codes)
409 const int32_t SCX_ARRAY_CAPACITY = 32;
410 UScriptCode scxValArray[SCX_ARRAY_CAPACITY];
411 int32_t numScripts = uscript_getScriptExtensions(cp, scxValArray, SCX_ARRAY_CAPACITY, status);
412 handleError(status, __LINE__, scxFullPropName);
413
414 // Convert the scx array into a vector
415 std::vector<uint16_t> scxValVec;
416 for(int i = 0; i < numScripts; i++) {
417 scxValVec.push_back(scxValArray[i]);
418 }
419 // Ensure that it is sorted
420 std::sort(scxValVec.begin(), scxValVec.end());
421 // Copy the Script value into the first position of the scx array only
422 // if we have the "other" case (Script value is not Common nor Inherited).
423 // This offers faster access when users want only the Script value.
424 if (scVal != USCRIPT_COMMON && scVal != USCRIPT_INHERITED) {
425 scxValVec.insert(scxValVec.begin(), scVal);
426 }
427
428 // See if there is already an scx value array matching the newly built one.
429 // If there is, then use its index.
430 // If not, then append the new value array.
431 bool isScxValUnique = true;
432 size_t outputIndex = 0;
433 for (outputIndex = 0; outputIndex < outputDedupVec.size(); outputIndex++) {
434 if (outputDedupVec[outputIndex] == scxValVec) {
435 isScxValUnique = false;
436 break;
437 }
438 }
439
440 if (isScxValUnique) {
441 outputDedupVec.push_back(scxValVec);
442 usrc_writeArray(f, " [", scxValVec.data(), 16, scxValVec.size(), " ", "],\n");
443 }
444
445 // We must update the value in the UCPTrie for the code point to contain:
446 // 9..0 the Script code in the lower 10 bits when 11..10 is 0, else it is
447 // the index into the companion array
448 // 11..10 the same higher-order 2 bits in the trie in uprops.icu indicating whether
449 // 3: other
450 // 2: Script=Inherited
451 // 1: Script=Common
452 // 0: Script=value in 9..0 (N/A because we are in this loop to create the companion array for non-0 cases)
453 uint16_t mask = 0;
454 if (scVal == USCRIPT_COMMON) {
455 mask = DATAEXPORT_SCRIPT_X_WITH_COMMON;
456 } else if (scVal == USCRIPT_INHERITED) {
457 mask = DATAEXPORT_SCRIPT_X_WITH_INHERITED;
458 } else {
459 mask = DATAEXPORT_SCRIPT_X_WITH_OTHER;
460 }
461
462 // The new trie value is the index into the new array with the high order bits set
463 uint32_t newScVal = outputIndex | mask;
464
465 // Update the code point in the mutable trie builder with the trie value
466 umutablecptrie_set(builder.getAlias(), cp, newScVal, status);
467 handleError(status, __LINE__, scxFullPropName);
468 }
469 fputs("]\n\n", f); // Print the TOML close delimiter for the outer array.
470
471 // Convert from mutable trie builder to immutable trie.
472 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
473 builder.getAlias(),
474 trieType,
475 scWidth,
476 status));
477 handleError(status, __LINE__, scxFullPropName);
478
479 fputs("[script_extensions.code_point_trie]\n", f);
480 usrc_writeUCPTrie(f, scxShortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
481 }
482
483 FILE* prepareOutputFile(const char* basename) {
484 IcuToolErrorCode status("icuexportdata");
485 CharString outFileName;
486 if (destdir != nullptr && *destdir != 0) {
487 outFileName.append(destdir, status).ensureEndsWithFileSeparator(status);
488 }
489 outFileName.append(basename, status);
490 outFileName.append(".toml", status);
491 handleError(status, __LINE__, basename);
492
493 FILE* f = fopen(outFileName.data(), "w");
494 if (f == nullptr) {
495 std::cerr << "Unable to open file: " << outFileName.data() << std::endl;
496 exit(U_FILE_ACCESS_ERROR);
497 }
498 if (!QUIET) {
499 std::cout << "Writing to: " << outFileName.data() << std::endl;
500 }
501
502 if (haveCopyright) {
503 usrc_writeCopyrightHeader(f, "#", 2021);
504 }
505 usrc_writeFileNameGeneratedBy(f, "#", basename, "icuexportdata.cpp");
506
507 return f;
508 }
509
510 #if !UCONFIG_NO_NORMALIZATION
511
512 class PendingDescriptor {
513 public:
514 UChar32 scalar;
515 uint32_t descriptorOrFlags;
516 // If false, we use the above fields only. If true, descriptor only
517 // contains the two highest-bit flags and the rest is computed later
518 // from the fields below.
519 UBool complex;
520 UBool supplementary;
521 UBool onlyNonStartersInTrail;
522 uint32_t len;
523 uint32_t offset;
524
525 PendingDescriptor(UChar32 scalar, uint32_t descriptor);
526 PendingDescriptor(UChar32 scalar, uint32_t flags, UBool supplementary, UBool onlyNonStartersInTrail, uint32_t len, uint32_t offset);
527 };
528
529 PendingDescriptor::PendingDescriptor(UChar32 scalar, uint32_t descriptor)
530 : scalar(scalar), descriptorOrFlags(descriptor), complex(false), supplementary(false), onlyNonStartersInTrail(false), len(0), offset(0) {}
531
532 PendingDescriptor::PendingDescriptor(UChar32 scalar, uint32_t flags, UBool supplementary, UBool onlyNonStartersInTrail, uint32_t len, uint32_t offset)
533 : scalar(scalar), descriptorOrFlags(flags), complex(true), supplementary(supplementary), onlyNonStartersInTrail(onlyNonStartersInTrail), len(len), offset(offset) {}
534
535 void writeCanonicalCompositions(USet* backwardCombiningStarters) {
536 IcuToolErrorCode status("icuexportdata: computeCanonicalCompositions");
537 const char* basename = "compositions";
538 FILE* f = prepareOutputFile(basename);
539
540 LocalPointer<UCharsTrieBuilder> backwardBuilder(new UCharsTrieBuilder(status), status);
541
542 const int32_t DECOMPOSITION_BUFFER_SIZE = 20;
543 UChar32 utf32[DECOMPOSITION_BUFFER_SIZE];
544
545 const Normalizer2* nfc = Normalizer2::getNFCInstance(status);
546 for (UChar32 c = 0; c <= 0x10FFFF; ++c) {
547 if (c >= 0xD800 && c < 0xE000) {
548 // Surrogate
549 continue;
550 }
551 UnicodeString decomposition;
552 if (!nfc->getRawDecomposition(c, decomposition)) {
553 continue;
554 }
555 int32_t len = decomposition.toUTF32(utf32, DECOMPOSITION_BUFFER_SIZE, status);
556 if (len != 2) {
557 continue;
558 }
559 UChar32 starter = utf32[0];
560 UChar32 second = utf32[1];
561 UChar32 composite = nfc->composePair(starter, second);
562 if (composite < 0) {
563 continue;
564 }
565 if (c != composite) {
566 status.set(U_INTERNAL_PROGRAM_ERROR);
567 handleError(status, __LINE__, basename);
568 }
569 if (!u_getCombiningClass(second)) {
570 uset_add(backwardCombiningStarters, second);
571 }
572 if (composite >= 0xAC00 && composite <= 0xD7A3) {
573 // Hangul syllable
574 continue;
575 }
576
577 UnicodeString backward;
578 backward.append(second);
579 backward.append(starter);
580 backwardBuilder->add(backward, static_cast<int32_t>(composite), status);
581 }
582 UnicodeString canonicalCompositionTrie;
583 backwardBuilder->buildUnicodeString(USTRINGTRIE_BUILD_SMALL, canonicalCompositionTrie, status);
584
585 usrc_writeArray(f, "compositions = [\n ", canonicalCompositionTrie.getBuffer(), 16, canonicalCompositionTrie.length(), " ", "\n]\n");
586 fclose(f);
587 handleError(status, __LINE__, basename);
588 }
589
590 void writeDecompositionTables(const char* basename, const uint16_t* ptr16, size_t len16, const uint32_t* ptr32, size_t len32) {
591 FILE* f = prepareOutputFile(basename);
592 usrc_writeArray(f, "scalars16 = [\n ", ptr16, 16, len16, " ", "\n]\n");
593 usrc_writeArray(f, "scalars32 = [\n ", ptr32, 32, len32, " ", "\n]\n");
594 fclose(f);
595 }
596
597 void pendingInsertionsToTrie(const char* basename, UMutableCPTrie* trie, const std::vector<PendingDescriptor>& pendingTrieInsertions, uint32_t baseSize16, uint32_t baseSize32, uint32_t supplementSize16) {
598 IcuToolErrorCode status("icuexportdata: pendingInsertionsToTrie");
599 // Iterate backwards to insert lower code points in the trie first in case it matters
600 // for trie block allocation.
601 for (int32_t i = pendingTrieInsertions.size() - 1; i >= 0; --i) {
602 const PendingDescriptor& pending = pendingTrieInsertions[i];
603 if (pending.complex) {
604 uint32_t additional = 0;
605 uint32_t offset = pending.offset;
606 uint32_t len = pending.len;
607 if (!pending.supplementary) {
608 len -= 2;
609 if (offset >= baseSize16) {
610 // This is a offset to supplementary 16-bit data. We have
611 // 16-bit base data and 32-bit base data before. However,
612 // the 16-bit base data length is already part of offset.
613 additional = baseSize32;
614 }
615 } else {
616 len -= 1;
617 if (offset >= baseSize32) {
618 // This is an offset to supplementary 32-bit data. We have 16-bit
619 // base data, 32-bit base data, and 16-bit supplementary data before.
620 // However, the 32-bit base data length is already part
621 // of offset.
622 additional = baseSize16 + supplementSize16;
623 } else {
624 // This is an offset to 32-bit base data. We have 16-bit
625 // base data before.
626 additional = baseSize16;
627 }
628 }
629 // +1 to make offset always non-zero
630 offset += 1;
631 if (offset + additional > 0xFFF) {
632 status.set(U_INTERNAL_PROGRAM_ERROR);
633 handleError(status, __LINE__, basename);
634 }
635 if (len > 7) {
636 status.set(U_INTERNAL_PROGRAM_ERROR);
637 handleError(status, __LINE__, basename);
638 }
639 umutablecptrie_set(trie, pending.scalar, pending.descriptorOrFlags | (uint32_t(pending.onlyNonStartersInTrail) << 4) | len | (offset + additional) << 16, status);
640 } else {
641 umutablecptrie_set(trie, pending.scalar, pending.descriptorOrFlags, status);
642 }
643 }
644 }
645
646 /// Marker that the decomposition does not round trip via NFC.
647 const uint32_t NON_ROUND_TRIP_MASK = (1 << 30);
648
649 /// Marker that the first character of the decomposition can combine
650 /// backwards.
651 const uint32_t BACKWARD_COMBINING_MASK = (1 << 31);
652
653 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) {
654 IcuToolErrorCode status("icuexportdata: writeDecompositionData");
655 FILE* f = prepareOutputFile(basename);
656
657 // Zero is a magic number that means the character decomposes to itself.
658 LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
659
660 if (uprv_strcmp(basename, "uts46d") != 0) {
661 // Make surrogates decompose to U+FFFD. Don't do this for UTS 46, since this
662 // optimization is only used by the UTF-16 slice mode, and UTS 46 is not
663 // supported in slice modes (which do not support ignorables).
664 // Mark these as potentially backward-combining, to make lead surrogates
665 // for non-BMP characters that are backward-combining count as
666 // backward-combining just in case, though the backward-combiningness
667 // is not actually being looked at today.
668 umutablecptrie_setRange(builder.getAlias(), 0xD800, 0xDFFF, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK | 0xFFFD, status);
669 }
670
671 // Add a marker value for Hangul syllables
672 umutablecptrie_setRange(builder.getAlias(), 0xAC00, 0xD7A3, 1, status);
673
674 // First put the NFD data in the trie, to be partially overwritten in the NFKD and UTS 46 cases.
675 // This is easier that changing the logic that computes the pending insertions.
676 pendingInsertionsToTrie(basename, builder.getAlias(), nfdPendingTrieInsertions, baseSize16, baseSize32, supplementSize16);
677 pendingInsertionsToTrie(basename, builder.getAlias(), pendingTrieInsertions, baseSize16, baseSize32, supplementSize16);
678 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
679 builder.getAlias(),
680 trieType,
681 UCPTRIE_VALUE_BITS_32,
682 status));
683 handleError(status, __LINE__, basename);
684
685 // The ICU4X side has changed enough this whole block of expectation checking might be more appropriate to remove.
686 if (reference) {
687 if (uset_contains(reference, 0xFF9E) || uset_contains(reference, 0xFF9F) || !uset_contains(reference, 0x0345)) {
688 // NFD expectations don't hold. The set must not contain the half-width
689 // kana voicing marks and must contain iota subscript.
690 status.set(U_INTERNAL_PROGRAM_ERROR);
691 handleError(status, __LINE__, basename);
692 }
693
694 USet* halfWidthVoicing = uset_openEmpty();
695 uset_add(halfWidthVoicing, 0xFF9E);
696 uset_add(halfWidthVoicing, 0xFF9F);
697
698 USet* iotaSubscript = uset_openEmpty();
699 uset_add(iotaSubscript, 0x0345);
700
701 USet* halfWidthCheck = uset_cloneAsThawed(uset);
702 uset_removeAll(halfWidthCheck, reference);
703 if (!uset_equals(halfWidthCheck, halfWidthVoicing) && !uset_isEmpty(halfWidthCheck)) {
704 // The result was neither empty nor contained exactly
705 // the two half-width voicing marks. The ICU4X
706 // normalizer doesn't know how to deal with this case.
707 status.set(U_INTERNAL_PROGRAM_ERROR);
708 handleError(status, __LINE__, basename);
709 }
710 uset_close(halfWidthCheck);
711
712 USet* iotaCheck = uset_cloneAsThawed(reference);
713 uset_removeAll(iotaCheck, uset);
714 if (!(uset_equals(iotaCheck, iotaSubscript)) && !uset_isEmpty(iotaCheck)) {
715 // The result was neither empty nor contained exactly
716 // the iota subscript. The ICU4X normalizer doesn't
717 // know how to deal with this case.
718 status.set(U_INTERNAL_PROGRAM_ERROR);
719 handleError(status, __LINE__, basename);
720 }
721
722 uset_close(iotaSubscript);
723 uset_close(halfWidthVoicing);
724 }
725 fprintf(f, "cap = 0x%X\n", passthroughCap);
726 fprintf(f, "[trie]\n");
727 usrc_writeUCPTrie(f, "trie", utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
728 fclose(f);
729 handleError(status, __LINE__, basename);
730 }
731
732 // Find the slice `needle` within `storage` and return its index, failing which,
733 // append all elements of `needle` to `storage` and return the index of it at the end.
734 template<typename T>
735 size_t findOrAppend(std::vector<T>& storage, const UChar32* needle, size_t needleLen) {
736 // Last index where we might find the start of the complete needle.
737 // bounds check is `i + needleLen <= storage.size()` since the inner
738 // loop will range from `i` to `i + needleLen - 1` (the `-1` is why we use `<=`)
739 for (size_t i = 0; i + needleLen <= storage.size(); i++) {
740 for (size_t j = 0;; j++) {
741 if (j == needleLen) {
742 return i; // found a match
743 }
744 if (storage[i + j] != static_cast<uint32_t>(needle[j])) {
745 break;
746 }
747 }
748 }
749 // We didn't find anything. Append, keeping the append index in mind.
750 size_t index = storage.size();
751 for(size_t i = 0; i < needleLen; i++) {
752 storage.push_back(static_cast<T>(needle[i]));
753 }
754
755 return index;
756 }
757
758
759 // Computes data for canonical decompositions
760 // See components/normalizer/trie-value-format.md in the ICU4X repo
761 // for documentation of the trie value format.
762 void computeDecompositions(const char* basename,
763 const USet* backwardCombiningStarters,
764 std::vector<uint16_t>& storage16,
765 std::vector<uint32_t>& storage32,
766 USet* decompositionStartsWithNonStarter,
767 USet* decompositionStartsWithBackwardCombiningStarter,
768 std::vector<PendingDescriptor>& pendingTrieInsertions,
769 UChar32& decompositionPassthroughBound,
770 UChar32& compositionPassthroughBound) {
771 IcuToolErrorCode status("icuexportdata: computeDecompositions");
772 const Normalizer2* mainNormalizer;
773 const Normalizer2* nfdNormalizer = Normalizer2::getNFDInstance(status);
774 const Normalizer2* nfcNormalizer = Normalizer2::getNFCInstance(status);
775 FILE* f = nullptr;
776 std::vector<uint32_t> nonRecursive32;
777 LocalUMutableCPTriePointer nonRecursiveBuilder(umutablecptrie_open(0, 0, status));
778
779 UBool uts46 = false;
780
781 if (uprv_strcmp(basename, "nfkd") == 0) {
782 mainNormalizer = Normalizer2::getNFKDInstance(status);
783 } else if (uprv_strcmp(basename, "uts46d") == 0) {
784 uts46 = true;
785 mainNormalizer = Normalizer2::getInstance(nullptr, "uts46", UNORM2_COMPOSE, status);
786 } else {
787 mainNormalizer = nfdNormalizer;
788 f = prepareOutputFile("decompositionex");
789 }
790
791 // Max length as of Unicode 14 is 4 for NFD. For NFKD the max
792 // is 18 (U+FDFA; special-cased), and the next longest is 8 (U+FDFB).
793 const int32_t LONGEST_ENCODABLE_LENGTH_16 = 9;
794 const int32_t LONGEST_ENCODABLE_LENGTH_32 = 8;
795 const int32_t DECOMPOSITION_BUFFER_SIZE = 20;
796 UChar32 utf32[DECOMPOSITION_BUFFER_SIZE];
797 const int32_t RAW_DECOMPOSITION_BUFFER_SIZE = 2;
798 UChar32 rawUtf32[RAW_DECOMPOSITION_BUFFER_SIZE];
799
800 // Iterate over all scalar values excluding Hangul syllables.
801 //
802 // We go backwards in order to better find overlapping decompositions.
803 //
804 // As of Unicode 14:
805 // Iterate forward without overlap search:
806 // nfd: 16 size: 896, 32 size: 173
807 // nfkd: 16 size: 3854, 32 size: 179
808 //
809 // Iterate forward with overlap search:
810 // nfd: 16 size: 888, 32 size: 173
811 // nfkd: 16 size: 3266, 32 size: 179
812 //
813 // Iterate backward with overlap search:
814 // nfd: 16 size: 776, 32 size: 173
815 // nfkd: 16 size: 2941, 32 size: 179
816 //
817 // UChar32 is signed!
818 for (UChar32 c = 0x10FFFF; c >= 0; --c) {
819 if (c >= 0xAC00 && c <= 0xD7A3) {
820 // Hangul syllable
821 continue;
822 }
823 if (c >= 0xD800 && c < 0xE000) {
824 // Surrogate
825 continue;
826 }
827 if (c == 0xFFFD) {
828 // REPLACEMENT CHARACTER
829 // This character is a starter that decomposes to self,
830 // so without a special case here it would end up as
831 // passthrough-eligible in all normalizations forms.
832 // However, in the potentially-ill-formed UTF-8 case
833 // UTF-8 errors return U+FFFD from the iterator, and
834 // errors need to be treated as ineligible for
835 // passthrough on the slice fast path. By giving
836 // U+FFFD a trie value whose flags make it ineligible
837 // for passthrough avoids a specific U+FFFD branch on
838 // the passthrough fast path.
839 pendingTrieInsertions.push_back({c, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK});
840 continue;
841 }
842 UnicodeString src;
843 UnicodeString dst;
844 src.append(c);
845 if (mainNormalizer != nfdNormalizer) {
846 UnicodeString inter;
847 mainNormalizer->normalize(src, inter, status);
848 nfdNormalizer->normalize(inter, dst, status);
849 } else {
850 nfdNormalizer->normalize(src, dst, status);
851 }
852
853 UnicodeString nfc;
854 nfcNormalizer->normalize(dst, nfc, status);
855 UBool roundTripsViaCanonicalComposition = (src == nfc);
856
857 int32_t len = dst.toUTF32(utf32, DECOMPOSITION_BUFFER_SIZE, status);
858
859 if (!len || (len == 1 && utf32[0] == 0xFFFD && c != 0xFFFD)) {
860 if (!uts46) {
861 status.set(U_INTERNAL_PROGRAM_ERROR);
862 handleError(status, __LINE__, basename);
863 }
864 }
865 if (len > DECOMPOSITION_BUFFER_SIZE) {
866 status.set(U_INTERNAL_PROGRAM_ERROR);
867 handleError(status, __LINE__, basename);
868 }
869 uint8_t firstCombiningClass = u_getCombiningClass(utf32[0]);
870 bool specialNonStarterDecomposition = false;
871 bool startsWithBackwardCombiningStarter = false;
872 if (firstCombiningClass) {
873 decompositionPassthroughBound = c;
874 compositionPassthroughBound = c;
875 uset_add(decompositionStartsWithNonStarter, c);
876 if (src != dst) {
877 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)) {
878 specialNonStarterDecomposition = true;
879 } else {
880 // 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.
881 status.set(U_INTERNAL_PROGRAM_ERROR);
882 handleError(status, __LINE__, basename);
883 }
884 }
885 } else if (uset_contains(backwardCombiningStarters, utf32[0])) {
886 compositionPassthroughBound = c;
887 startsWithBackwardCombiningStarter = true;
888 uset_add(decompositionStartsWithBackwardCombiningStarter, c);
889 }
890 if (mainNormalizer != nfdNormalizer) {
891 UnicodeString nfd;
892 nfdNormalizer->normalize(src, nfd, status);
893 if (dst == nfd) {
894 continue;
895 }
896 decompositionPassthroughBound = c;
897 compositionPassthroughBound = c;
898 }
899 if (firstCombiningClass) {
900 len = 1;
901 if (specialNonStarterDecomposition) {
902 // Special marker
903 pendingTrieInsertions.push_back({c, NON_ROUND_TRIP_MASK | BACKWARD_COMBINING_MASK | 0xD900 | u_getCombiningClass(c)});
904 } else {
905 // Use the surrogate range to store the canonical combining class
906 // XXX: Should non-started that decompose to self be marked as non-round-trippable in
907 // case such semantics turn out to be more useful for `NON_ROUND_TRIP_MASK`?
908 pendingTrieInsertions.push_back({c, BACKWARD_COMBINING_MASK | 0xD800 | static_cast<uint32_t>(firstCombiningClass)});
909 }
910 continue;
911 } else {
912 if (src == dst) {
913 if (startsWithBackwardCombiningStarter) {
914 pendingTrieInsertions.push_back({c, BACKWARD_COMBINING_MASK});
915 }
916 continue;
917 }
918 decompositionPassthroughBound = c;
919 // ICU4X hard-codes ANGSTROM SIGN
920 if (c != 0x212B && mainNormalizer == nfdNormalizer) {
921 UnicodeString raw;
922 if (!nfdNormalizer->getRawDecomposition(c, raw)) {
923 // We're always supposed to have a non-recursive decomposition
924 // if we had a recursive one.
925 status.set(U_INTERNAL_PROGRAM_ERROR);
926 handleError(status, __LINE__, basename);
927 }
928 // In addition to actual difference, put the whole range that contains characters
929 // with oxia into the non-recursive trie in order to catch cases where characters
930 // with oxia have singleton decompositions to corresponding characters with tonos.
931 // This way, the run-time decision to fall through can be done on the range
932 // without checking for individual characters inside the range.
933 if (raw != dst || (c >= 0x1F71 && c <= 0x1FFB)) {
934 int32_t rawLen = raw.toUTF32(rawUtf32, RAW_DECOMPOSITION_BUFFER_SIZE, status);
935 if (!rawLen) {
936 status.set(U_INTERNAL_PROGRAM_ERROR);
937 handleError(status, __LINE__, basename);
938 }
939 if (rawLen == 1) {
940 if (c >= 0xFFFF) {
941 status.set(U_INTERNAL_PROGRAM_ERROR);
942 handleError(status, __LINE__, basename);
943 }
944 umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, static_cast<uint32_t>(rawUtf32[0]), status);
945 } else if (rawUtf32[0] <= 0xFFFF && rawUtf32[1] <= 0xFFFF) {
946 if (!rawUtf32[0] || !rawUtf32[1]) {
947 status.set(U_INTERNAL_PROGRAM_ERROR);
948 handleError(status, __LINE__, basename);
949 }
950 // Swapped for consistency with the primary trie
951 uint32_t bmpPair = static_cast<uint32_t>(rawUtf32[1]) << 16 | static_cast<uint32_t>(rawUtf32[0]);
952 umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, bmpPair, status);
953 } else {
954 // Let's add 1 to index to make it always non-zero to distinguish
955 // it from the default zero.
956 uint32_t index = nonRecursive32.size() + 1;
957 nonRecursive32.push_back(static_cast<uint32_t>(rawUtf32[0]));
958 nonRecursive32.push_back(static_cast<uint32_t>(rawUtf32[1]));
959 if (index > 0xFFFF) {
960 status.set(U_INTERNAL_PROGRAM_ERROR);
961 handleError(status, __LINE__, basename);
962 }
963 umutablecptrie_set(nonRecursiveBuilder.getAlias(), c, index << 16, status);
964 }
965 }
966 }
967 }
968 if (!roundTripsViaCanonicalComposition) {
969 compositionPassthroughBound = c;
970 }
971 if (!len) {
972 if (!uts46) {
973 status.set(U_INTERNAL_PROGRAM_ERROR);
974 handleError(status, __LINE__, basename);
975 }
976 pendingTrieInsertions.push_back({c, uint32_t(0xFFFFFFFF)});
977 } else if (len == 1 && ((utf32[0] >= 0x1161 && utf32[0] <= 0x1175) || (utf32[0] >= 0x11A8 && utf32[0] <= 0x11C2))) {
978 // Singleton decompositions to conjoining jamo.
979 if (mainNormalizer == nfdNormalizer) {
980 // Not supposed to happen in NFD
981 status.set(U_INTERNAL_PROGRAM_ERROR);
982 handleError(status, __LINE__, basename);
983 }
984 pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | NON_ROUND_TRIP_MASK | (startsWithBackwardCombiningStarter ? BACKWARD_COMBINING_MASK : 0)});
985 } else if (!startsWithBackwardCombiningStarter && len == 1 && utf32[0] <= 0xFFFF) {
986 pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | NON_ROUND_TRIP_MASK | (startsWithBackwardCombiningStarter ? BACKWARD_COMBINING_MASK : 0)});
987 } else if (c != 0x212B && // ANGSTROM SIGN is special to make the Harfbuzz case branch less in the more common case.
988 !startsWithBackwardCombiningStarter &&
989 len == 2 &&
990 utf32[0] <= 0x7FFF &&
991 utf32[1] <= 0x7FFF &&
992 utf32[0] > 0x1F &&
993 utf32[1] > 0x1F &&
994 !u_getCombiningClass(utf32[0]) &&
995 u_getCombiningClass(utf32[1])) {
996 for (int32_t i = 0; i < len; ++i) {
997 if (((utf32[i] == 0x0345) && (uprv_strcmp(basename, "uts46d") == 0)) || utf32[i] == 0xFF9E || utf32[i] == 0xFF9F) {
998 // Assert that iota subscript and half-width voicing marks never occur in these
999 // expansions in the normalization forms where they are special.
1000 status.set(U_INTERNAL_PROGRAM_ERROR);
1001 handleError(status, __LINE__, basename);
1002 }
1003 }
1004 pendingTrieInsertions.push_back({c, static_cast<uint32_t>(utf32[0]) | (static_cast<uint32_t>(utf32[1]) << 15) | (roundTripsViaCanonicalComposition ? 0 : NON_ROUND_TRIP_MASK)});
1005 } else {
1006 UBool supplementary = false;
1007 UBool nonInitialStarter = false;
1008 for (int32_t i = 0; i < len; ++i) {
1009 if (((utf32[i] == 0x0345) && (uprv_strcmp(basename, "uts46d") == 0)) || utf32[i] == 0xFF9E || utf32[i] == 0xFF9F) {
1010 // Assert that iota subscript and half-width voicing marks never occur in these
1011 // expansions in the normalization forms where they are special.
1012 status.set(U_INTERNAL_PROGRAM_ERROR);
1013 handleError(status, __LINE__, basename);
1014 }
1015
1016 if (utf32[i] > 0xFFFF) {
1017 supplementary = true;
1018 }
1019 if (utf32[i] == 0) {
1020 status.set(U_INTERNAL_PROGRAM_ERROR);
1021 handleError(status, __LINE__, basename);
1022 }
1023 if (i != 0 && !u_getCombiningClass(utf32[i])) {
1024 nonInitialStarter = true;
1025 }
1026 }
1027 if (len == 1) {
1028 // The format doesn't allow for length 1 for BMP,
1029 // so if these ever occur, they need to be promoted
1030 // to wider storage. As of Unicode 16 alpha, this
1031 // case does not arise.
1032 supplementary = true;
1033 }
1034 if (!supplementary) {
1035 if (len > LONGEST_ENCODABLE_LENGTH_16 || !len || len == 1) {
1036 if (len == 18 && c == 0xFDFA) {
1037 // Special marker for the one character whose decomposition
1038 // is too long. (Too long even if we took the fourth bit into use!)
1039 pendingTrieInsertions.push_back({c, NON_ROUND_TRIP_MASK | 1});
1040 continue;
1041 } else {
1042 // Note: There's a fourth bit available, but let's error out
1043 // if it's ever needed so that it doesn't get used without
1044 // updating docs.
1045 status.set(U_INTERNAL_PROGRAM_ERROR);
1046 handleError(status, __LINE__, basename);
1047 }
1048 }
1049 } else if (len > LONGEST_ENCODABLE_LENGTH_32 || !len) {
1050 // Note: There's a fourth bit available, but let's error out
1051 // if it's ever needed so that it doesn't get used without
1052 // updating docs.
1053 status.set(U_INTERNAL_PROGRAM_ERROR);
1054 handleError(status, __LINE__, basename);
1055 }
1056
1057 size_t index = 0;
1058 if (!supplementary) {
1059 index = findOrAppend(storage16, utf32, len);
1060 } else {
1061 index = findOrAppend(storage32, utf32, len);
1062 }
1063 pendingTrieInsertions.push_back({c, (startsWithBackwardCombiningStarter ? BACKWARD_COMBINING_MASK : 0) | (roundTripsViaCanonicalComposition ? 0 : NON_ROUND_TRIP_MASK), supplementary, !nonInitialStarter, uint32_t(len), uint32_t(index)});
1064 }
1065 }
1066 if (storage16.size() + storage32.size() > 0xFFF) {
1067 // We actually have 14 bits available, but let's error out so
1068 // that docs can be updated when taking a reserved bit out of
1069 // potential future flag usage.
1070 status.set(U_INTERNAL_PROGRAM_ERROR);
1071 }
1072 if (f) {
1073 usrc_writeArray(f, "scalars32 = [\n ", nonRecursive32.data(), 32, nonRecursive32.size(), " ", "\n]\n");
1074
1075 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
1076 nonRecursiveBuilder.getAlias(),
1077 trieType,
1078 UCPTRIE_VALUE_BITS_32,
1079 status));
1080 handleError(status, __LINE__, basename);
1081
1082 fprintf(f, "[trie]\n");
1083 usrc_writeUCPTrie(f, "trie", utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
1084
1085 fclose(f);
1086 }
1087 handleError(status, __LINE__, basename);
1088 }
1089
1090 #endif // !UCONFIG_NO_NORMALIZATION
1091
1092 enum {
1093 OPT_HELP_H,
1094 OPT_HELP_QUESTION_MARK,
1095 OPT_MODE,
1096 OPT_TRIE_TYPE,
1097 OPT_VERSION,
1098 OPT_DESTDIR,
1099 OPT_ALL,
1100 OPT_INDEX,
1101 OPT_COPYRIGHT,
1102 OPT_VERBOSE,
1103 OPT_QUIET,
1104
1105 OPT_COUNT
1106 };
1107
1108 #define UOPTION_MODE UOPTION_DEF("mode", 'm', UOPT_REQUIRES_ARG)
1109 #define UOPTION_TRIE_TYPE UOPTION_DEF("trie-type", '\1', UOPT_REQUIRES_ARG)
1110 #define UOPTION_ALL UOPTION_DEF("all", '\1', UOPT_NO_ARG)
1111 #define UOPTION_INDEX UOPTION_DEF("index", '\1', UOPT_NO_ARG)
1112
1113 static UOption options[]={
1114 UOPTION_HELP_H,
1115 UOPTION_HELP_QUESTION_MARK,
1116 UOPTION_MODE,
1117 UOPTION_TRIE_TYPE,
1118 UOPTION_VERSION,
1119 UOPTION_DESTDIR,
1120 UOPTION_ALL,
1121 UOPTION_INDEX,
1122 UOPTION_COPYRIGHT,
1123 UOPTION_VERBOSE,
1124 UOPTION_QUIET,
1125 };
1126
1127 void printHelp(FILE* stdfile, const char* program) {
1128 fprintf(stdfile,
1129 "usage: %s -m mode [-options] [--all | properties...]\n"
1130 "\tdump Unicode property data to .toml files\n"
1131 "options:\n"
1132 "\t-h or -? or --help this usage text\n"
1133 "\t-V or --version show a version message\n"
1134 "\t-m or --mode mode: currently only 'uprops', 'ucase', and 'norm', but more may be added\n"
1135 "\t --trie-type set the trie type (small or fast, default small)\n"
1136 "\t-d or --destdir destination directory, followed by the path\n"
1137 "\t --all write out all properties known to icuexportdata\n"
1138 "\t --index write an _index.toml summarizing all data exported\n"
1139 "\t-c or --copyright include a copyright notice\n"
1140 "\t-v or --verbose Turn on verbose output\n"
1141 "\t-q or --quiet do not display warnings and progress\n",
1142 program);
1143 }
1144
1145 int exportUprops(int argc, char* argv[]) {
1146 // Load list of Unicode properties
1147 std::vector<const char*> propNames;
1148 for (int i=1; i<argc; i++) {
1149 propNames.push_back(argv[i]);
1150 }
1151 if (options[OPT_ALL].doesOccur) {
1152 int i = UCHAR_BINARY_START;
1153 while (true) {
1154 if (i == UCHAR_BINARY_LIMIT) {
1155 i = UCHAR_INT_START;
1156 }
1157 if (i == UCHAR_INT_LIMIT) {
1158 i = UCHAR_GENERAL_CATEGORY_MASK;
1159 }
1160 if (i == UCHAR_GENERAL_CATEGORY_MASK + 1) {
1161 i = UCHAR_BIDI_MIRRORING_GLYPH;
1162 }
1163 if (i == UCHAR_BIDI_MIRRORING_GLYPH + 1) {
1164 i = UCHAR_SCRIPT_EXTENSIONS;
1165 }
1166 if (i == UCHAR_SCRIPT_EXTENSIONS + 1) {
1167 break;
1168 }
1169 UProperty uprop = static_cast<UProperty>(i);
1170 const char* propName = u_getPropertyName(uprop, U_SHORT_PROPERTY_NAME);
1171 if (propName == nullptr) {
1172 propName = u_getPropertyName(uprop, U_LONG_PROPERTY_NAME);
1173 if (propName != nullptr && VERBOSE) {
1174 std::cerr << "Note: falling back to long name for: " << propName << std::endl;
1175 }
1176 }
1177 if (propName != nullptr) {
1178 propNames.push_back(propName);
1179 } else {
1180 std::cerr << "Warning: Could not find name for: " << uprop << std::endl;
1181 }
1182 i++;
1183 }
1184 }
1185
1186 if (propNames.empty()
1187 || options[OPT_HELP_H].doesOccur
1188 || options[OPT_HELP_QUESTION_MARK].doesOccur
1189 || !options[OPT_MODE].doesOccur) {
1190 FILE *stdfile=argc<0 ? stderr : stdout;
1191 fprintf(stdfile,
1192 "usage: %s -m uprops [-options] [--all | properties...]\n"
1193 "\tdump Unicode property data to .toml files\n"
1194 "options:\n"
1195 "\t-h or -? or --help this usage text\n"
1196 "\t-V or --version show a version message\n"
1197 "\t-m or --mode mode: currently only 'uprops', but more may be added\n"
1198 "\t --trie-type set the trie type (small or fast, default small)\n"
1199 "\t-d or --destdir destination directory, followed by the path\n"
1200 "\t --all write out all properties known to icuexportdata\n"
1201 "\t --index write an _index.toml summarizing all data exported\n"
1202 "\t-c or --copyright include a copyright notice\n"
1203 "\t-v or --verbose Turn on verbose output\n"
1204 "\t-q or --quiet do not display warnings and progress\n",
1205 argv[0]);
1206 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
1207 }
1208
1209 const char* mode = options[OPT_MODE].value;
1210 if (uprv_strcmp(mode, "uprops") != 0) {
1211 fprintf(stderr, "Invalid option for --mode (must be uprops)\n");
1212 return U_ILLEGAL_ARGUMENT_ERROR;
1213 }
1214
1215 if (options[OPT_TRIE_TYPE].doesOccur) {
1216 if (uprv_strcmp(options[OPT_TRIE_TYPE].value, "fast") == 0) {
1217 trieType = UCPTRIE_TYPE_FAST;
1218 } else if (uprv_strcmp(options[OPT_TRIE_TYPE].value, "small") == 0) {
1219 trieType = UCPTRIE_TYPE_SMALL;
1220 } else {
1221 fprintf(stderr, "Invalid option for --trie-type (must be small or fast)\n");
1222 return U_ILLEGAL_ARGUMENT_ERROR;
1223 }
1224 }
1225
1226 for (const char* propName : propNames) {
1227 UProperty propEnum = u_getPropertyEnum(propName);
1228 if (propEnum == UCHAR_INVALID_CODE) {
1229 std::cerr << "Error: Invalid property alias: " << propName << std::endl;
1230 return U_ILLEGAL_ARGUMENT_ERROR;
1231 }
1232
1233 FILE* f = prepareOutputFile(propName);
1234
1235 UVersionInfo versionInfo;
1236 u_getUnicodeVersion(versionInfo);
1237 char uvbuf[U_MAX_VERSION_STRING_LENGTH];
1238 u_versionToString(versionInfo, uvbuf);
1239 fprintf(f, "icu_version = \"%s\"\nunicode_version = \"%s\"\n\n",
1240 U_ICU_VERSION,
1241 uvbuf);
1242
1243 if (propEnum < UCHAR_BINARY_LIMIT) {
1244 dumpBinaryProperty(propEnum, f);
1245 } else if (UCHAR_INT_START <= propEnum && propEnum <= UCHAR_INT_LIMIT) {
1246 dumpEnumeratedProperty(propEnum, f);
1247 } else if (propEnum == UCHAR_GENERAL_CATEGORY_MASK) {
1248 dumpGeneralCategoryMask(f);
1249 } else if (propEnum == UCHAR_BIDI_MIRRORING_GLYPH) {
1250 dumpBidiMirroringGlyph(f);
1251 } else if (propEnum == UCHAR_SCRIPT_EXTENSIONS) {
1252 dumpScriptExtensions(f);
1253 } else {
1254 std::cerr << "Don't know how to write property: " << propEnum << std::endl;
1255 return U_INTERNAL_PROGRAM_ERROR;
1256 }
1257
1258 fclose(f);
1259 }
1260
1261 if (options[OPT_INDEX].doesOccur) {
1262 FILE* f = prepareOutputFile("_index");
1263 fprintf(f, "index = [\n");
1264 for (const char* propName : propNames) {
1265 // At this point, propName is a valid property name, so it should be alphanum ASCII
1266 fprintf(f, " { filename=\"%s.toml\" },\n", propName);
1267 }
1268 fprintf(f, "]\n");
1269 fclose(f);
1270 }
1271
1272 return 0;
1273 }
1274
1275 struct AddRangeHelper {
1276 UMutableCPTrie* ucptrie;
1277 };
1278
1279 static UBool U_CALLCONV
1280 addRangeToUCPTrie(const void* context, UChar32 start, UChar32 end, uint32_t value) {
1281 IcuToolErrorCode status("addRangeToUCPTrie");
1282 UMutableCPTrie* ucptrie = static_cast<const AddRangeHelper*>(context)->ucptrie;
1283 umutablecptrie_setRange(ucptrie, start, end, value, status);
1284 handleError(status, __LINE__, "setRange");
1285
1286 return true;
1287 }
1288
1289 int exportCase(int argc, char* argv[]) {
1290 if (argc > 1) {
1291 fprintf(stderr, "ucase mode does not expect additional arguments\n");
1292 return U_ILLEGAL_ARGUMENT_ERROR;
1293 }
1294 (void) argv; // Suppress unused variable warning
1295
1296 IcuToolErrorCode status("icuexportdata");
1297 LocalUMutableCPTriePointer builder(umutablecptrie_open(0, 0, status));
1298 handleError(status, __LINE__, "exportCase");
1299
1300 int32_t exceptionsLength, unfoldLength;
1301 const UCaseProps *caseProps = ucase_getSingleton(&exceptionsLength, &unfoldLength);
1302 const UTrie2* caseTrie = &caseProps->trie;
1303
1304 AddRangeHelper helper = { builder.getAlias() };
1305 utrie2_enum(caseTrie, nullptr, addRangeToUCPTrie, &helper);
1306
1307 UCPTrieValueWidth width = UCPTRIE_VALUE_BITS_16;
1308 LocalUCPTriePointer utrie(umutablecptrie_buildImmutable(
1309 builder.getAlias(),
1310 trieType,
1311 width,
1312 status));
1313 handleError(status, __LINE__, "exportCase");
1314
1315 FILE* f = prepareOutputFile("ucase");
1316
1317 UVersionInfo versionInfo;
1318 u_getUnicodeVersion(versionInfo);
1319 char uvbuf[U_MAX_VERSION_STRING_LENGTH];
1320 u_versionToString(versionInfo, uvbuf);
1321 fprintf(f, "icu_version = \"%s\"\nunicode_version = \"%s\"\n\n",
1322 U_ICU_VERSION,
1323 uvbuf);
1324
1325 fputs("[ucase.code_point_trie]\n", f);
1326 usrc_writeUCPTrie(f, "case_trie", utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
1327 fputs("\n", f);
1328
1329 const char* indent = " ";
1330 const char* suffix = "\n]\n";
1331
1332 fputs("[ucase.exceptions]\n", f);
1333 const char* exceptionsPrefix = "exceptions = [\n ";
1334 int32_t exceptionsWidth = 16;
1335 usrc_writeArray(f, exceptionsPrefix, caseProps->exceptions, exceptionsWidth,
1336 exceptionsLength, indent, suffix);
1337 fputs("\n", f);
1338
1339 fputs("[ucase.unfold]\n", f);
1340 const char* unfoldPrefix = "unfold = [\n ";
1341 int32_t unfoldWidth = 16;
1342 usrc_writeArray(f, unfoldPrefix, caseProps->unfold, unfoldWidth,
1343 unfoldLength, indent, suffix);
1344
1345 return 0;
1346 }
1347
1348 #if !UCONFIG_NO_NORMALIZATION
1349
1350 int exportNorm() {
1351 IcuToolErrorCode status("icuexportdata: exportNorm");
1352 USet* backwardCombiningStarters = uset_openEmpty();
1353 writeCanonicalCompositions(backwardCombiningStarters);
1354
1355 std::vector<uint16_t> storage16;
1356 std::vector<uint32_t> storage32;
1357
1358 // Note: the USets are not exported. They are only used to check that a new
1359 // Unicode version doesn't violate expectations that are hard-coded in ICU4X.
1360 USet* nfdDecompositionStartsWithNonStarter = uset_openEmpty();
1361 USet* nfdDecompositionStartsWithBackwardCombiningStarter = uset_openEmpty();
1362 std::vector<PendingDescriptor> nfdPendingTrieInsertions;
1363 UChar32 nfdBound = 0x10FFFF;
1364 UChar32 nfcBound = 0x10FFFF;
1365 computeDecompositions("nfd",
1366 backwardCombiningStarters,
1367 storage16,
1368 storage32,
1369 nfdDecompositionStartsWithNonStarter,
1370 nfdDecompositionStartsWithBackwardCombiningStarter,
1371 nfdPendingTrieInsertions,
1372 nfdBound,
1373 nfcBound);
1374 if (!(nfdBound == 0xC0 && nfcBound == 0x300)) {
1375 // Unexpected bounds for NFD/NFC.
1376 status.set(U_INTERNAL_PROGRAM_ERROR);
1377 handleError(status, __LINE__, "exportNorm");
1378 }
1379
1380 uint32_t baseSize16 = storage16.size();
1381 uint32_t baseSize32 = storage32.size();
1382
1383 USet* nfkdDecompositionStartsWithNonStarter = uset_openEmpty();
1384 USet* nfkdDecompositionStartsWithBackwardCombiningStarter = uset_openEmpty();
1385 std::vector<PendingDescriptor> nfkdPendingTrieInsertions;
1386 UChar32 nfkdBound = 0x10FFFF;
1387 UChar32 nfkcBound = 0x10FFFF;
1388 computeDecompositions("nfkd",
1389 backwardCombiningStarters,
1390 storage16,
1391 storage32,
1392 nfkdDecompositionStartsWithNonStarter,
1393 nfkdDecompositionStartsWithBackwardCombiningStarter,
1394 nfkdPendingTrieInsertions,
1395 nfkdBound,
1396 nfkcBound);
1397 if (!(nfkdBound <= 0xC0 && nfkcBound <= 0x300)) {
1398 status.set(U_INTERNAL_PROGRAM_ERROR);
1399 handleError(status, __LINE__, "exportNorm");
1400 }
1401 if (nfkcBound > 0xC0) {
1402 if (nfkdBound != 0xC0) {
1403 status.set(U_INTERNAL_PROGRAM_ERROR);
1404 handleError(status, __LINE__, "exportNorm");
1405 }
1406 } else {
1407 if (nfkdBound != nfkcBound) {
1408 status.set(U_INTERNAL_PROGRAM_ERROR);
1409 handleError(status, __LINE__, "exportNorm");
1410 }
1411 }
1412
1413 USet* uts46DecompositionStartsWithNonStarter = uset_openEmpty();
1414 USet* uts46DecompositionStartsWithBackwardCombiningStarter = uset_openEmpty();
1415 std::vector<PendingDescriptor> uts46PendingTrieInsertions;
1416 UChar32 uts46dBound = 0x10FFFF;
1417 UChar32 uts46Bound = 0x10FFFF;
1418 computeDecompositions("uts46d",
1419 backwardCombiningStarters,
1420 storage16,
1421 storage32,
1422 uts46DecompositionStartsWithNonStarter,
1423 uts46DecompositionStartsWithBackwardCombiningStarter,
1424 uts46PendingTrieInsertions,
1425 uts46dBound,
1426 uts46Bound);
1427 if (!(uts46dBound <= 0xC0 && uts46Bound <= 0x300)) {
1428 status.set(U_INTERNAL_PROGRAM_ERROR);
1429 handleError(status, __LINE__, "exportNorm");
1430 }
1431 if (uts46Bound > 0xC0) {
1432 if (uts46dBound != 0xC0) {
1433 status.set(U_INTERNAL_PROGRAM_ERROR);
1434 handleError(status, __LINE__, "exportNorm");
1435 }
1436 } else {
1437 if (uts46dBound != uts46Bound) {
1438 status.set(U_INTERNAL_PROGRAM_ERROR);
1439 handleError(status, __LINE__, "exportNorm");
1440 }
1441 }
1442
1443 uint32_t supplementSize16 = storage16.size() - baseSize16;
1444 uint32_t supplementSize32 = storage32.size() - baseSize32;
1445
1446 writeDecompositionData("nfd", baseSize16, baseSize32, supplementSize16, nfdDecompositionStartsWithNonStarter, nullptr, nfdPendingTrieInsertions, nfdPendingTrieInsertions, static_cast<char16_t>(nfcBound));
1447 writeDecompositionData("nfkd", baseSize16, baseSize32, supplementSize16, nfkdDecompositionStartsWithNonStarter, nfdDecompositionStartsWithNonStarter, nfkdPendingTrieInsertions, nfdPendingTrieInsertions, static_cast<char16_t>(nfkcBound));
1448 writeDecompositionData("uts46d", baseSize16, baseSize32, supplementSize16, uts46DecompositionStartsWithNonStarter, nfdDecompositionStartsWithNonStarter, uts46PendingTrieInsertions, nfdPendingTrieInsertions, static_cast<char16_t>(uts46Bound));
1449
1450 writeDecompositionTables("nfdex", storage16.data(), baseSize16, storage32.data(), baseSize32);
1451 writeDecompositionTables("nfkdex", storage16.data() + baseSize16, supplementSize16, storage32.data() + baseSize32, supplementSize32);
1452
1453 uset_close(nfdDecompositionStartsWithNonStarter);
1454 uset_close(nfkdDecompositionStartsWithNonStarter);
1455 uset_close(uts46DecompositionStartsWithNonStarter);
1456
1457 uset_close(nfdDecompositionStartsWithBackwardCombiningStarter);
1458 uset_close(nfkdDecompositionStartsWithBackwardCombiningStarter);
1459 uset_close(uts46DecompositionStartsWithBackwardCombiningStarter);
1460
1461 uset_close(backwardCombiningStarters);
1462 handleError(status, __LINE__, "exportNorm");
1463 return 0;
1464 }
1465
1466 #endif // !UCONFIG_NO_NORMALIZATION
1467
1468 int main(int argc, char* argv[]) {
1469 U_MAIN_INIT_ARGS(argc, argv);
1470
1471 /* preset then read command line options */
1472 options[OPT_DESTDIR].value=u_getDataDirectory();
1473 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
1474
1475 if(options[OPT_VERSION].doesOccur) {
1476 printf("icuexportdata version %s, ICU tool to dump data files for external consumers\n",
1477 U_ICU_DATA_VERSION);
1478 printf("%s\n", U_COPYRIGHT_STRING);
1479 exit(0);
1480 }
1481
1482 /* error handling, printing usage message */
1483 if(argc<0) {
1484 fprintf(stderr,
1485 "error in command line argument \"%s\"\n",
1486 argv[-argc]);
1487 }
1488
1489 if (argc < 0
1490 || options[OPT_HELP_H].doesOccur
1491 || options[OPT_HELP_QUESTION_MARK].doesOccur
1492 || !options[OPT_MODE].doesOccur) {
1493 FILE *stdfile=argc<0 ? stderr : stdout;
1494 printHelp(stdfile, argv[0]);
1495 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
1496 }
1497
1498 /* get the options values */
1499 haveCopyright = options[OPT_COPYRIGHT].doesOccur;
1500 destdir = options[OPT_DESTDIR].value;
1501 VERBOSE = options[OPT_VERBOSE].doesOccur;
1502 QUIET = options[OPT_QUIET].doesOccur;
1503
1504 if (options[OPT_TRIE_TYPE].doesOccur) {
1505 if (uprv_strcmp(options[OPT_TRIE_TYPE].value, "fast") == 0) {
1506 trieType = UCPTRIE_TYPE_FAST;
1507 } else if (uprv_strcmp(options[OPT_TRIE_TYPE].value, "small") == 0) {
1508 trieType = UCPTRIE_TYPE_SMALL;
1509 } else {
1510 fprintf(stderr, "Invalid option for --trie-type (must be small or fast)\n");
1511 return U_ILLEGAL_ARGUMENT_ERROR;
1512 }
1513 }
1514
1515 const char* mode = options[OPT_MODE].value;
1516 if (uprv_strcmp(mode, "norm") == 0) {
1517 #if !UCONFIG_NO_NORMALIZATION
1518 return exportNorm();
1519 #else
1520 fprintf(stderr, "Exporting normalization data not supported when compiling without normalization support.\n");
1521 return U_ILLEGAL_ARGUMENT_ERROR;
1522 #endif
1523 }
1524 if (uprv_strcmp(mode, "uprops") == 0) {
1525 return exportUprops(argc, argv);
1526 } else if (uprv_strcmp(mode, "ucase") == 0) {
1527 return exportCase(argc, argv);
1528 }
1529
1530 fprintf(stderr, "Invalid option for --mode (must be uprops, ucase, or norm)\n");
1531 return U_ILLEGAL_ARGUMENT_ERROR;
1532 }