GenerateUnicodeData.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GeneratorUtil.h"
  7. #include <AK/AllOf.h>
  8. #include <AK/Array.h>
  9. #include <AK/ByteString.h>
  10. #include <AK/CharacterTypes.h>
  11. #include <AK/Error.h>
  12. #include <AK/Find.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/Optional.h>
  15. #include <AK/QuickSort.h>
  16. #include <AK/SourceGenerator.h>
  17. #include <AK/StringUtils.h>
  18. #include <AK/Types.h>
  19. #include <AK/Vector.h>
  20. #include <LibCore/ArgsParser.h>
  21. #include <LibUnicode/CharacterTypes.h>
  22. // https://www.unicode.org/reports/tr44/#SpecialCasing.txt
  23. struct SpecialCasing {
  24. u32 index { 0 };
  25. u32 code_point { 0 };
  26. Vector<u32> lowercase_mapping;
  27. Vector<u32> uppercase_mapping;
  28. Vector<u32> titlecase_mapping;
  29. ByteString locale;
  30. ByteString condition;
  31. };
  32. // https://www.unicode.org/reports/tr44/#CaseFolding.txt
  33. struct CaseFolding {
  34. u32 code_point { 0 };
  35. StringView status { "Common"sv };
  36. Vector<u32> mapping { 0 };
  37. };
  38. // https://www.unicode.org/reports/tr44/#Character_Decomposition_Mappings
  39. struct CodePointDecomposition {
  40. // `tag` is a string since it's used for codegen as an enum value.
  41. ByteString tag { "Canonical"sv };
  42. size_t decomposition_index { 0 };
  43. size_t decomposition_size { 0 };
  44. };
  45. // https://www.unicode.org/reports/tr44/#PropList.txt
  46. using PropList = HashMap<ByteString, Vector<Unicode::CodePointRange>>;
  47. // https://www.unicode.org/reports/tr44/#DerivedNormalizationProps.txt
  48. enum class QuickCheck {
  49. Yes,
  50. No,
  51. Maybe,
  52. };
  53. struct Normalization {
  54. Unicode::CodePointRange code_point_range;
  55. Vector<u32> value;
  56. QuickCheck quick_check { QuickCheck::Yes };
  57. };
  58. using NormalizationProps = HashMap<ByteString, Vector<Normalization>>;
  59. struct CasingTable {
  60. bool operator==(CasingTable const& other) const
  61. {
  62. return canonical_combining_class == other.canonical_combining_class
  63. && simple_lowercase_mapping == other.simple_lowercase_mapping
  64. && simple_uppercase_mapping == other.simple_uppercase_mapping
  65. && simple_titlecase_mapping == other.simple_titlecase_mapping
  66. && special_casing_indices == other.special_casing_indices
  67. && case_folding_indices == other.case_folding_indices;
  68. }
  69. u8 canonical_combining_class { 0 };
  70. Optional<u32> simple_uppercase_mapping;
  71. Optional<u32> simple_lowercase_mapping;
  72. Optional<u32> simple_titlecase_mapping;
  73. Vector<u32> special_casing_indices;
  74. Vector<u32> case_folding_indices;
  75. };
  76. // https://www.unicode.org/reports/tr44/#UnicodeData.txt
  77. struct CodePointData {
  78. u32 code_point { 0 };
  79. ByteString name;
  80. ByteString bidi_class;
  81. Optional<CodePointDecomposition> decomposition_mapping;
  82. Optional<i8> numeric_value_decimal;
  83. Optional<i8> numeric_value_digit;
  84. Optional<i8> numeric_value_numeric;
  85. bool bidi_mirrored { false };
  86. ByteString unicode_1_name;
  87. ByteString iso_comment;
  88. CasingTable casing;
  89. };
  90. using PropertyTable = Vector<bool>;
  91. static constexpr auto CODE_POINT_TABLES_MSB_COUNT = 16u;
  92. static_assert(CODE_POINT_TABLES_MSB_COUNT < 24u);
  93. static constexpr auto CODE_POINT_TABLES_LSB_COUNT = 24u - CODE_POINT_TABLES_MSB_COUNT;
  94. static constexpr auto CODE_POINT_TABLES_LSB_MASK = NumericLimits<u32>::max() >> (NumericLimits<u32>::digits() - CODE_POINT_TABLES_LSB_COUNT);
  95. template<typename PropertyType>
  96. struct CodePointTables {
  97. Vector<size_t> stage1;
  98. Vector<size_t> stage2;
  99. Vector<PropertyType> unique_properties;
  100. };
  101. struct CodePointBidiClass {
  102. Unicode::CodePointRange code_point_range;
  103. ByteString bidi_class;
  104. };
  105. struct CodePointComposition {
  106. u32 second_code_point { 0 };
  107. u32 combined_code_point { 0 };
  108. };
  109. struct UnicodeData {
  110. u32 code_points_with_decomposition_mapping { 0 };
  111. Vector<u32> decomposition_mappings;
  112. HashMap<u32, Vector<CodePointComposition>> composition_mappings;
  113. Vector<ByteString> compatibility_tags;
  114. Vector<SpecialCasing> special_casing;
  115. u32 largest_special_casing_mapping_size { 0 };
  116. Vector<ByteString> conditions;
  117. Vector<ByteString> locales;
  118. Vector<CaseFolding> case_folding;
  119. u32 largest_case_folding_mapping_size { 0 };
  120. Vector<StringView> statuses;
  121. Vector<CodePointData> code_point_data;
  122. // https://www.unicode.org/reports/tr44/#General_Category_Values
  123. PropList general_categories;
  124. Vector<Alias> general_category_aliases;
  125. // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in
  126. // any UCD file. Assigned code point ranges are derived as this generator is executed.
  127. // https://unicode.org/reports/tr18/#General_Category_Property
  128. PropList prop_list {
  129. { "Any"sv, { { 0, 0x10ffff } } },
  130. { "Assigned"sv, {} },
  131. { "ASCII"sv, { { 0, 0x7f } } },
  132. };
  133. Vector<Alias> prop_aliases;
  134. PropList script_list {
  135. { "Unknown"sv, {} },
  136. };
  137. Vector<Alias> script_aliases;
  138. PropList script_extensions;
  139. // FIXME: We are not yet doing anything with this data. It will be needed for String.prototype.normalize.
  140. NormalizationProps normalization_props;
  141. PropList grapheme_break_props;
  142. PropList word_break_props;
  143. PropList sentence_break_props;
  144. CodePointTables<CasingTable> casing_tables;
  145. CodePointTables<PropertyTable> general_category_tables;
  146. CodePointTables<PropertyTable> property_tables;
  147. CodePointTables<PropertyTable> script_tables;
  148. CodePointTables<PropertyTable> script_extension_tables;
  149. CodePointTables<PropertyTable> grapheme_break_tables;
  150. CodePointTables<PropertyTable> word_break_tables;
  151. CodePointTables<PropertyTable> sentence_break_tables;
  152. HashTable<ByteString> bidirectional_classes;
  153. Vector<CodePointBidiClass> code_point_bidirectional_classes;
  154. };
  155. static ByteString sanitize_entry(ByteString const& entry)
  156. {
  157. auto sanitized = entry.replace("-"sv, "_"sv, ReplaceMode::All);
  158. sanitized = sanitized.replace(" "sv, "_"sv, ReplaceMode::All);
  159. StringBuilder builder;
  160. bool next_is_upper = true;
  161. for (auto ch : sanitized) {
  162. if (next_is_upper)
  163. builder.append_code_point(to_ascii_uppercase(ch));
  164. else
  165. builder.append_code_point(ch);
  166. next_is_upper = ch == '_';
  167. }
  168. return builder.to_byte_string();
  169. }
  170. static ErrorOr<void> parse_special_casing(Core::InputBufferedFile& file, UnicodeData& unicode_data)
  171. {
  172. Array<u8, 1024> buffer;
  173. while (TRY(file.can_read_line())) {
  174. auto line = TRY(file.read_line(buffer));
  175. if (line.is_empty() || line.starts_with('#'))
  176. continue;
  177. if (auto index = line.find('#'); index.has_value())
  178. line = line.substring_view(0, *index);
  179. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  180. VERIFY(segments.size() == 5 || segments.size() == 6);
  181. SpecialCasing casing {};
  182. casing.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  183. casing.lowercase_mapping = parse_code_point_list(segments[1]);
  184. casing.titlecase_mapping = parse_code_point_list(segments[2]);
  185. casing.uppercase_mapping = parse_code_point_list(segments[3]);
  186. if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) {
  187. auto conditions = condition.split_view(' ', SplitBehavior::KeepEmpty);
  188. VERIFY(conditions.size() == 1 || conditions.size() == 2);
  189. if (conditions.size() == 2) {
  190. casing.locale = conditions[0];
  191. casing.condition = conditions[1];
  192. } else if (all_of(conditions[0], is_ascii_lower_alpha)) {
  193. casing.locale = conditions[0];
  194. } else {
  195. casing.condition = conditions[0];
  196. }
  197. if (!casing.locale.is_empty()) {
  198. casing.locale = ByteString::formatted("{:c}{}", to_ascii_uppercase(casing.locale[0]), casing.locale.substring_view(1));
  199. if (!unicode_data.locales.contains_slow(casing.locale))
  200. unicode_data.locales.append(casing.locale);
  201. }
  202. casing.condition = casing.condition.replace("_"sv, ""sv, ReplaceMode::All);
  203. if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition))
  204. unicode_data.conditions.append(casing.condition);
  205. }
  206. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.lowercase_mapping.size());
  207. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.titlecase_mapping.size());
  208. unicode_data.largest_special_casing_mapping_size = max(unicode_data.largest_special_casing_mapping_size, casing.uppercase_mapping.size());
  209. unicode_data.special_casing.append(move(casing));
  210. }
  211. quick_sort(unicode_data.special_casing, [](auto const& lhs, auto const& rhs) {
  212. if (lhs.code_point != rhs.code_point)
  213. return lhs.code_point < rhs.code_point;
  214. if (lhs.locale.is_empty() && !rhs.locale.is_empty())
  215. return false;
  216. if (!lhs.locale.is_empty() && rhs.locale.is_empty())
  217. return true;
  218. return lhs.locale < rhs.locale;
  219. });
  220. for (u32 i = 0; i < unicode_data.special_casing.size(); ++i)
  221. unicode_data.special_casing[i].index = i;
  222. return {};
  223. }
  224. static ErrorOr<void> parse_case_folding(Core::InputBufferedFile& file, UnicodeData& unicode_data)
  225. {
  226. Array<u8, 1024> buffer;
  227. while (TRY(file.can_read_line())) {
  228. auto line = TRY(file.read_line(buffer));
  229. if (line.is_empty() || line.starts_with('#'))
  230. continue;
  231. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  232. VERIFY(segments.size() == 4);
  233. CaseFolding folding {};
  234. folding.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  235. folding.mapping = parse_code_point_list(segments[2]);
  236. switch (segments[1].trim_whitespace()[0]) {
  237. case 'C':
  238. folding.status = "Common"sv;
  239. break;
  240. case 'F':
  241. folding.status = "Full"sv;
  242. break;
  243. case 'S':
  244. folding.status = "Simple"sv;
  245. break;
  246. case 'T':
  247. folding.status = "Special"sv;
  248. break;
  249. }
  250. unicode_data.largest_case_folding_mapping_size = max(unicode_data.largest_case_folding_mapping_size, folding.mapping.size());
  251. if (!unicode_data.statuses.contains_slow(folding.status))
  252. unicode_data.statuses.append(folding.status);
  253. unicode_data.case_folding.append(move(folding));
  254. }
  255. quick_sort(unicode_data.case_folding, [](auto const& lhs, auto const& rhs) {
  256. if (lhs.code_point != rhs.code_point)
  257. return lhs.code_point < rhs.code_point;
  258. return lhs.status < rhs.status;
  259. });
  260. return {};
  261. }
  262. static ErrorOr<void> parse_prop_list(Core::InputBufferedFile& file, PropList& prop_list, bool multi_value_property = false, bool sanitize_property = false)
  263. {
  264. Array<u8, 1024> buffer;
  265. while (TRY(file.can_read_line())) {
  266. auto line = TRY(file.read_line(buffer));
  267. if (line.is_empty() || line.starts_with('#'))
  268. continue;
  269. if (auto index = line.find('#'); index.has_value())
  270. line = line.substring_view(0, *index);
  271. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  272. VERIFY(segments.size() == 2 || segments.size() == 3);
  273. String combined_segment_buffer;
  274. if (segments.size() == 3) {
  275. // For example, in DerivedCoreProperties.txt, there are lines such as:
  276. //
  277. // 094D ; InCB; Linker # Mn DEVANAGARI SIGN VIRAMA
  278. //
  279. // These are used in text segmentation to prevent breaking within some extended grapheme clusters.
  280. // So here, we combine the segments into a single property, which allows us to simply do code point
  281. // property lookups at runtime for specific Indic Conjunct Break sequences.
  282. combined_segment_buffer = MUST(String::join('_', Array { segments[1].trim_whitespace(), segments[2].trim_whitespace() }));
  283. segments[1] = combined_segment_buffer;
  284. }
  285. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  286. Vector<StringView> properties;
  287. if (multi_value_property)
  288. properties = segments[1].trim_whitespace().split_view(' ');
  289. else
  290. properties = { segments[1].trim_whitespace() };
  291. for (auto& property : properties) {
  292. auto& code_points = prop_list.ensure(sanitize_property ? sanitize_entry(property).trim_whitespace() : ByteString { property.trim_whitespace() });
  293. code_points.append(code_point_range);
  294. }
  295. }
  296. return {};
  297. }
  298. static ErrorOr<void> parse_alias_list(Core::InputBufferedFile& file, PropList const& prop_list, Vector<Alias>& prop_aliases)
  299. {
  300. ByteString current_property;
  301. Array<u8, 1024> buffer;
  302. auto append_alias = [&](auto alias, auto property) {
  303. // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip.
  304. if (alias == property)
  305. return;
  306. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  307. if (!prop_list.contains(property))
  308. return;
  309. prop_aliases.append({ property, alias });
  310. };
  311. while (TRY(file.can_read_line())) {
  312. auto line = TRY(file.read_line(buffer));
  313. if (line.is_empty() || line.starts_with('#')) {
  314. if (line.ends_with("Properties"sv))
  315. current_property = line.substring_view(2);
  316. continue;
  317. }
  318. // Note: For now, we only care about Binary Property aliases for Unicode property escapes.
  319. if (current_property != "Binary Properties"sv)
  320. continue;
  321. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  322. VERIFY((segments.size() == 2) || (segments.size() == 3));
  323. auto alias = segments[0].trim_whitespace();
  324. auto property = segments[1].trim_whitespace();
  325. append_alias(alias, property);
  326. if (segments.size() == 3) {
  327. alias = segments[2].trim_whitespace();
  328. append_alias(alias, property);
  329. }
  330. }
  331. return {};
  332. }
  333. static ErrorOr<void> parse_value_alias_list(Core::InputBufferedFile& file, StringView desired_category, Vector<ByteString> const& value_list, Vector<Alias>& prop_aliases, bool primary_value_is_first = true, bool sanitize_alias = false)
  334. {
  335. TRY(file.seek(0, SeekMode::SetPosition));
  336. Array<u8, 1024> buffer;
  337. auto append_alias = [&](auto alias, auto value) {
  338. // Note: The value alias file contains lines such as "Ahom = Ahom", which we should just skip.
  339. if (alias == value)
  340. return;
  341. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  342. if (!value_list.contains_slow(value))
  343. return;
  344. prop_aliases.append({ value, alias });
  345. };
  346. while (TRY(file.can_read_line())) {
  347. auto line = TRY(file.read_line(buffer));
  348. if (line.is_empty() || line.starts_with('#'))
  349. continue;
  350. if (auto index = line.find('#'); index.has_value())
  351. line = line.substring_view(0, *index);
  352. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  353. auto category = segments[0].trim_whitespace();
  354. if (category != desired_category)
  355. continue;
  356. VERIFY((segments.size() == 3) || (segments.size() == 4));
  357. auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
  358. auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
  359. append_alias(sanitize_alias ? sanitize_entry(alias) : ByteString { alias }, value);
  360. if (segments.size() == 4) {
  361. alias = segments[3].trim_whitespace();
  362. append_alias(sanitize_alias ? sanitize_entry(alias) : ByteString { alias }, value);
  363. }
  364. }
  365. return {};
  366. }
  367. static ErrorOr<void> parse_normalization_props(Core::InputBufferedFile& file, UnicodeData& unicode_data)
  368. {
  369. Array<u8, 1024> buffer;
  370. while (TRY(file.can_read_line())) {
  371. auto line = TRY(file.read_line(buffer));
  372. if (line.is_empty() || line.starts_with('#'))
  373. continue;
  374. if (auto index = line.find('#'); index.has_value())
  375. line = line.substring_view(0, *index);
  376. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  377. VERIFY((segments.size() == 2) || (segments.size() == 3));
  378. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  379. auto property = segments[1].trim_whitespace().to_byte_string();
  380. Vector<u32> value;
  381. QuickCheck quick_check = QuickCheck::Yes;
  382. if (segments.size() == 3) {
  383. auto value_or_quick_check = segments[2].trim_whitespace();
  384. if ((value_or_quick_check == "N"sv))
  385. quick_check = QuickCheck::No;
  386. else if ((value_or_quick_check == "M"sv))
  387. quick_check = QuickCheck::Maybe;
  388. else
  389. value = parse_code_point_list(value_or_quick_check);
  390. }
  391. auto& normalizations = unicode_data.normalization_props.ensure(property);
  392. normalizations.append({ code_point_range, move(value), quick_check });
  393. auto& prop_list = unicode_data.prop_list.ensure(property);
  394. prop_list.append(move(code_point_range));
  395. }
  396. return {};
  397. }
  398. static Optional<CodePointDecomposition> parse_decomposition_mapping(StringView string, UnicodeData& unicode_data)
  399. {
  400. if (string.is_empty())
  401. return {};
  402. CodePointDecomposition mapping;
  403. auto parts = string.split_view(' ');
  404. VERIFY(parts.size() > 0);
  405. if (parts.first().starts_with('<')) {
  406. auto const tag = parts.take_first().trim("<>"sv);
  407. mapping.tag = ByteString::formatted("{:c}{}", to_ascii_uppercase(tag[0]), tag.substring_view(1));
  408. if (!unicode_data.compatibility_tags.contains_slow(mapping.tag))
  409. unicode_data.compatibility_tags.append(mapping.tag);
  410. }
  411. mapping.decomposition_index = unicode_data.decomposition_mappings.size();
  412. mapping.decomposition_size = parts.size();
  413. for (auto part : parts) {
  414. unicode_data.decomposition_mappings.append(AK::StringUtils::convert_to_uint_from_hex<u32>(part).value());
  415. }
  416. return mapping;
  417. }
  418. static void add_composition_mapping(u32 code_point, CodePointDecomposition& decomposition, UnicodeData& unicode_data, Vector<Unicode::CodePointRange> const& full_composition_exclusion_code_points)
  419. {
  420. if (decomposition.decomposition_size != 2)
  421. return;
  422. if (decomposition.tag != "Canonical"sv)
  423. return;
  424. static Unicode::CodePointRangeComparator comparator {};
  425. for (auto const& range : full_composition_exclusion_code_points) {
  426. auto comparison = comparator(code_point, range);
  427. if (comparison == 0)
  428. return;
  429. if (comparison < 0)
  430. break;
  431. }
  432. u32 const first_code_point = unicode_data.decomposition_mappings[decomposition.decomposition_index];
  433. u32 const second_code_point = unicode_data.decomposition_mappings[decomposition.decomposition_index + 1];
  434. unicode_data.composition_mappings.ensure(first_code_point).append(CodePointComposition { .second_code_point = second_code_point, .combined_code_point = code_point });
  435. }
  436. static ErrorOr<void> parse_unicode_data(Core::InputBufferedFile& file, UnicodeData& unicode_data)
  437. {
  438. Optional<u32> code_point_range_start;
  439. auto& assigned_code_points = unicode_data.prop_list.find("Assigned"sv)->value;
  440. auto const& full_composition_exclusion_code_points = unicode_data.prop_list.find("Full_Composition_Exclusion"sv)->value;
  441. Optional<u32> assigned_code_point_range_start = 0;
  442. u32 previous_code_point = 0;
  443. Array<u8, 1024> buffer;
  444. while (TRY(file.can_read_line())) {
  445. auto line = TRY(file.read_line(buffer));
  446. if (line.is_empty())
  447. continue;
  448. auto segments = line.split_view(';', SplitBehavior::KeepEmpty);
  449. VERIFY(segments.size() == 15);
  450. CodePointData data {};
  451. data.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  452. data.name = segments[1];
  453. data.casing.canonical_combining_class = AK::StringUtils::convert_to_uint<u8>(segments[3]).value();
  454. data.bidi_class = segments[4];
  455. data.decomposition_mapping = parse_decomposition_mapping(segments[5], unicode_data);
  456. data.numeric_value_decimal = AK::StringUtils::convert_to_int<i8>(segments[6]);
  457. data.numeric_value_digit = AK::StringUtils::convert_to_int<i8>(segments[7]);
  458. data.numeric_value_numeric = AK::StringUtils::convert_to_int<i8>(segments[8]);
  459. data.bidi_mirrored = segments[9] == "Y"sv;
  460. data.unicode_1_name = segments[10];
  461. data.iso_comment = segments[11];
  462. data.casing.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[12]);
  463. data.casing.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[13]);
  464. data.casing.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[14]);
  465. if (!assigned_code_point_range_start.has_value())
  466. assigned_code_point_range_start = data.code_point;
  467. if (data.name.starts_with("<"sv) && data.name.ends_with(", First>"sv)) {
  468. VERIFY(!code_point_range_start.has_value() && assigned_code_point_range_start.has_value());
  469. code_point_range_start = data.code_point;
  470. data.name = data.name.substring(1, data.name.length() - 9);
  471. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  472. assigned_code_point_range_start.clear();
  473. } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>"sv)) {
  474. VERIFY(code_point_range_start.has_value());
  475. Unicode::CodePointRange code_point_range { *code_point_range_start, data.code_point };
  476. assigned_code_points.append(code_point_range);
  477. data.name = data.name.substring(1, data.name.length() - 8);
  478. code_point_range_start.clear();
  479. unicode_data.code_point_bidirectional_classes.append({ code_point_range, data.bidi_class });
  480. } else {
  481. unicode_data.code_point_bidirectional_classes.append({ { data.code_point, data.code_point }, data.bidi_class });
  482. if ((data.code_point > 0) && (data.code_point - previous_code_point) != 1) {
  483. VERIFY(assigned_code_point_range_start.has_value());
  484. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  485. assigned_code_point_range_start = data.code_point;
  486. }
  487. }
  488. for (auto const& casing : unicode_data.special_casing) {
  489. if (casing.code_point == data.code_point)
  490. data.casing.special_casing_indices.append(casing.index);
  491. }
  492. for (size_t i = 0; i < unicode_data.case_folding.size(); ++i) {
  493. if (auto const& folding = unicode_data.case_folding[i]; folding.code_point == data.code_point)
  494. data.casing.case_folding_indices.append(i);
  495. }
  496. unicode_data.code_points_with_decomposition_mapping += data.decomposition_mapping.has_value();
  497. if (data.decomposition_mapping.has_value())
  498. add_composition_mapping(data.code_point, *data.decomposition_mapping, unicode_data, full_composition_exclusion_code_points);
  499. unicode_data.bidirectional_classes.set(data.bidi_class, AK::HashSetExistingEntryBehavior::Keep);
  500. previous_code_point = data.code_point;
  501. unicode_data.code_point_data.append(move(data));
  502. }
  503. return {};
  504. }
  505. static ErrorOr<void> generate_unicode_data_header(Core::InputBufferedFile& file, UnicodeData& unicode_data)
  506. {
  507. StringBuilder builder;
  508. SourceGenerator generator { builder };
  509. generator.set("special_casing_mapping_size", ByteString::number(unicode_data.largest_special_casing_mapping_size));
  510. generator.set("case_folding_mapping_size", ByteString::number(unicode_data.largest_case_folding_mapping_size));
  511. auto generate_enum = [&](StringView name, StringView default_, auto values, Vector<Alias> aliases = {}) {
  512. quick_sort(values);
  513. quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; });
  514. generator.set("name", name);
  515. generator.set("underlying", ByteString::formatted("{}UnderlyingType", name));
  516. generator.set("type", ((values.size() + !default_.is_empty()) < 256) ? "u8"sv : "u16"sv);
  517. generator.append(R"~~~(
  518. using @underlying@ = @type@;
  519. enum class @name@ : @underlying@ {)~~~");
  520. if (!default_.is_empty()) {
  521. generator.set("default", default_);
  522. generator.append(R"~~~(
  523. @default@,)~~~");
  524. }
  525. for (auto const& value : values) {
  526. generator.set("value", value);
  527. generator.append(R"~~~(
  528. @value@,)~~~");
  529. }
  530. for (auto const& alias : aliases) {
  531. generator.set("alias", alias.alias);
  532. generator.set("value", alias.name);
  533. generator.append(R"~~~(
  534. @alias@ = @value@,)~~~");
  535. }
  536. generator.append(R"~~~(
  537. };
  538. )~~~");
  539. };
  540. generator.append(R"~~~(
  541. #pragma once
  542. #include <AK/Types.h>
  543. #include <LibUnicode/Forward.h>
  544. namespace Unicode {
  545. )~~~");
  546. generate_enum("Locale"sv, "None"sv, unicode_data.locales);
  547. generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions));
  548. generate_enum("CaseFoldingStatus"sv, {}, move(unicode_data.statuses));
  549. generate_enum("GeneralCategory"sv, {}, unicode_data.general_categories.keys(), unicode_data.general_category_aliases);
  550. generate_enum("Property"sv, {}, unicode_data.prop_list.keys(), unicode_data.prop_aliases);
  551. generate_enum("Script"sv, {}, unicode_data.script_list.keys(), unicode_data.script_aliases);
  552. generate_enum("GraphemeBreakProperty"sv, {}, unicode_data.grapheme_break_props.keys());
  553. generate_enum("WordBreakProperty"sv, {}, unicode_data.word_break_props.keys());
  554. generate_enum("SentenceBreakProperty"sv, {}, unicode_data.sentence_break_props.keys());
  555. generate_enum("CompatibilityFormattingTag"sv, "Canonical"sv, unicode_data.compatibility_tags);
  556. generate_enum("BidirectionalClass"sv, {}, unicode_data.bidirectional_classes.values());
  557. generator.append(R"~~~(
  558. struct SpecialCasing {
  559. u32 code_point { 0 };
  560. u32 lowercase_mapping[@special_casing_mapping_size@];
  561. u32 lowercase_mapping_size { 0 };
  562. u32 uppercase_mapping[@special_casing_mapping_size@];
  563. u32 uppercase_mapping_size { 0 };
  564. u32 titlecase_mapping[@special_casing_mapping_size@];
  565. u32 titlecase_mapping_size { 0 };
  566. Locale locale { Locale::None };
  567. Condition condition { Condition::None };
  568. };
  569. struct CaseFolding {
  570. u32 code_point { 0 };
  571. CaseFoldingStatus status { CaseFoldingStatus::Common };
  572. u32 mapping[@case_folding_mapping_size@];
  573. u32 mapping_size { 0 };
  574. };
  575. struct CodePointDecompositionRaw {
  576. u32 code_point { 0 };
  577. CompatibilityFormattingTag tag { CompatibilityFormattingTag::Canonical };
  578. size_t decomposition_index { 0 };
  579. size_t decomposition_count { 0 };
  580. };
  581. struct CodePointDecomposition {
  582. u32 code_point { 0 };
  583. CompatibilityFormattingTag tag { CompatibilityFormattingTag::Canonical };
  584. ReadonlySpan<u32> decomposition;
  585. };
  586. struct CodePointCompositionRaw {
  587. u32 code_point { 0 };
  588. u32 second_code_point { 0 };
  589. u32 combined_code_point { 0 };
  590. };
  591. Optional<Locale> locale_from_string(StringView locale);
  592. ReadonlySpan<SpecialCasing> special_case_mapping(u32 code_point);
  593. ReadonlySpan<CaseFolding> case_folding_mapping(u32 code_point);
  594. }
  595. )~~~");
  596. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  597. return {};
  598. }
  599. static ErrorOr<void> generate_unicode_data_implementation(Core::InputBufferedFile& file, UnicodeData const& unicode_data)
  600. {
  601. StringBuilder builder;
  602. SourceGenerator generator { builder };
  603. generator.set("special_casing_size", ByteString::number(unicode_data.special_casing.size()));
  604. generator.set("case_folding_size", ByteString::number(unicode_data.case_folding.size()));
  605. generator.set("CODE_POINT_TABLES_LSB_COUNT", TRY(String::number(CODE_POINT_TABLES_LSB_COUNT)));
  606. generator.set("CODE_POINT_TABLES_LSB_MASK", TRY(String::formatted("{:#x}", CODE_POINT_TABLES_LSB_MASK)));
  607. generator.append(R"~~~(
  608. #include <AK/Array.h>
  609. #include <AK/BinarySearch.h>
  610. #include <AK/CharacterTypes.h>
  611. #include <AK/Optional.h>
  612. #include <AK/Span.h>
  613. #include <AK/ByteString.h>
  614. #include <AK/StringView.h>
  615. #include <LibUnicode/CharacterTypes.h>
  616. #include <LibUnicode/UnicodeData.h>
  617. #include <LibUnicode/Normalize.h>
  618. namespace Unicode {
  619. )~~~");
  620. auto append_list_and_size = [&](auto const& list, StringView format) {
  621. if (list.is_empty()) {
  622. generator.append(", {}, 0");
  623. return;
  624. }
  625. bool first = true;
  626. generator.append(", {");
  627. for (auto const& item : list) {
  628. generator.append(first ? " "sv : ", "sv);
  629. generator.append(ByteString::formatted(format, item));
  630. first = false;
  631. }
  632. generator.append(ByteString::formatted(" }}, {}", list.size()));
  633. };
  634. generator.append(R"~~~(
  635. static constexpr Array<SpecialCasing, @special_casing_size@> s_special_case { {)~~~");
  636. for (auto const& casing : unicode_data.special_casing) {
  637. generator.set("code_point", ByteString::formatted("{:#x}", casing.code_point));
  638. generator.append(R"~~~(
  639. { @code_point@)~~~");
  640. constexpr auto format = "{:#x}"sv;
  641. append_list_and_size(casing.lowercase_mapping, format);
  642. append_list_and_size(casing.uppercase_mapping, format);
  643. append_list_and_size(casing.titlecase_mapping, format);
  644. generator.set("locale", casing.locale.is_empty() ? "None" : casing.locale);
  645. generator.append(", Locale::@locale@");
  646. generator.set("condition", casing.condition.is_empty() ? "None" : casing.condition);
  647. generator.append(", Condition::@condition@");
  648. generator.append(" },");
  649. }
  650. generator.append(R"~~~(
  651. } };
  652. static constexpr Array<CaseFolding, @case_folding_size@> s_case_folding { {)~~~");
  653. for (auto const& folding : unicode_data.case_folding) {
  654. generator.set("code_point", ByteString::formatted("{:#x}", folding.code_point));
  655. generator.set("status", folding.status);
  656. generator.append(R"~~~(
  657. { @code_point@, CaseFoldingStatus::@status@)~~~");
  658. append_list_and_size(folding.mapping, "{:#x}"sv);
  659. generator.append(" },");
  660. }
  661. generator.append(R"~~~(
  662. } };
  663. struct CasingTable {
  664. u8 canonical_combining_class { 0 };
  665. i32 simple_uppercase_mapping { -1 };
  666. i32 simple_lowercase_mapping { -1 };
  667. i32 simple_titlecase_mapping { -1 };
  668. u32 special_casing_start_index { 0 };
  669. u32 special_casing_size { 0 };
  670. u32 case_folding_start_index { 0 };
  671. u32 case_folding_size { 0 };
  672. };
  673. template<typename MappingType>
  674. struct CodePointComparator {
  675. constexpr int operator()(u32 code_point, MappingType const& mapping)
  676. {
  677. return code_point - mapping.code_point;
  678. }
  679. };
  680. struct BidiClassData {
  681. CodePointRange code_point_range {};
  682. BidirectionalClass bidi_class {};
  683. };
  684. struct CodePointBidiClassComparator : public CodePointRangeComparator {
  685. constexpr int operator()(u32 code_point, BidiClassData const& bidi_class)
  686. {
  687. return CodePointRangeComparator::operator()(code_point, bidi_class.code_point_range);
  688. }
  689. };
  690. )~~~");
  691. generator.set("decomposition_mappings_size", ByteString::number(unicode_data.decomposition_mappings.size()));
  692. generator.append("\nstatic constexpr Array<u32, @decomposition_mappings_size@> s_decomposition_mappings_data { ");
  693. generator.append(ByteString::join(", "sv, unicode_data.decomposition_mappings, "{:#x}"sv));
  694. generator.append(" };\n");
  695. auto append_code_point_mappings = [&](StringView name, StringView mapping_type, u32 size, auto mapping_getter) {
  696. generator.set("name", name);
  697. generator.set("mapping_type", mapping_type);
  698. generator.set("size", ByteString::number(size));
  699. generator.append(R"~~~(
  700. static constexpr Array<@mapping_type@, @size@> s_@name@_mappings { {
  701. )~~~");
  702. constexpr size_t max_mappings_per_row = 20;
  703. size_t mappings_in_current_row = 0;
  704. for (auto const& data : unicode_data.code_point_data) {
  705. auto mapping = mapping_getter(data);
  706. if constexpr (requires { mapping.has_value(); }) {
  707. if (!mapping.has_value())
  708. continue;
  709. } else {
  710. if (mapping.is_empty())
  711. continue;
  712. }
  713. if (mappings_in_current_row++ > 0)
  714. generator.append(" ");
  715. generator.set("code_point", ByteString::formatted("{:#x}", data.code_point));
  716. generator.append("{ @code_point@");
  717. if constexpr (IsSame<decltype(mapping), Optional<u32>> || IsSame<decltype(mapping), Optional<size_t>>) {
  718. generator.set("mapping", ByteString::formatted("{:#x}", *mapping));
  719. generator.append(", @mapping@ },");
  720. } else if constexpr (IsSame<decltype(mapping), Optional<CodePointDecomposition>>) {
  721. generator.set("tag", mapping->tag);
  722. generator.set("start", ByteString::number(mapping->decomposition_index));
  723. generator.set("size", ByteString::number(mapping->decomposition_size));
  724. generator.append(", CompatibilityFormattingTag::@tag@, @start@, @size@ },");
  725. } else {
  726. append_list_and_size(mapping, "&s_@name@[{}]"sv);
  727. generator.append(" },");
  728. }
  729. if (mappings_in_current_row == max_mappings_per_row) {
  730. mappings_in_current_row = 0;
  731. generator.append("\n ");
  732. }
  733. }
  734. generator.append(R"~~~(
  735. } };
  736. )~~~");
  737. };
  738. append_code_point_mappings("decomposition"sv, "CodePointDecompositionRaw"sv, unicode_data.code_points_with_decomposition_mapping, [](auto const& data) { return data.decomposition_mapping; });
  739. size_t composition_mappings_size = 0;
  740. for (auto const& entry : unicode_data.composition_mappings)
  741. composition_mappings_size += entry.value.size();
  742. generator.set("composition_mappings_size", ByteString::number(composition_mappings_size));
  743. generator.append(R"~~~(
  744. static constexpr Array<CodePointCompositionRaw, @composition_mappings_size@> s_composition_mappings { {
  745. )~~~");
  746. constexpr size_t max_mappings_per_row = 40;
  747. size_t mappings_in_current_row = 0;
  748. auto first_code_points = unicode_data.composition_mappings.keys();
  749. quick_sort(first_code_points);
  750. for (auto const first_code_point : first_code_points) {
  751. for (auto const& mapping : unicode_data.composition_mappings.find(first_code_point)->value) {
  752. if (mappings_in_current_row++ > 0)
  753. generator.append(" ");
  754. generator.set("code_point", ByteString::formatted("{:#x}", first_code_point));
  755. generator.set("second_code_point", ByteString::formatted("{:#x}", mapping.second_code_point));
  756. generator.set("combined_code_point", ByteString::formatted("{:#x}", mapping.combined_code_point));
  757. generator.append("{ @code_point@, @second_code_point@, @combined_code_point@ },");
  758. if (mappings_in_current_row == max_mappings_per_row) {
  759. mappings_in_current_row = 0;
  760. generator.append("\n ");
  761. }
  762. }
  763. }
  764. generator.append(R"~~~(
  765. } };
  766. )~~~");
  767. auto append_casing_table = [&](auto collection_snake, auto const& unique_properties) -> ErrorOr<void> {
  768. generator.set("name", TRY(String::formatted("{}_unique_properties", collection_snake)));
  769. generator.set("size", TRY(String::number(unique_properties.size())));
  770. auto optional_code_point_to_string = [](auto const& code_point) -> ErrorOr<String> {
  771. if (!code_point.has_value())
  772. return "-1"_string;
  773. return String::number(*code_point);
  774. };
  775. auto first_index_to_string = [](auto const& list) -> ErrorOr<String> {
  776. if (list.is_empty())
  777. return "0"_string;
  778. return String::number(list.first());
  779. };
  780. generator.append(R"~~~(
  781. static constexpr Array<CasingTable, @size@> @name@ { {)~~~");
  782. for (auto const& casing : unique_properties) {
  783. generator.set("canonical_combining_class", TRY(String::number(casing.canonical_combining_class)));
  784. generator.set("simple_uppercase_mapping", TRY(optional_code_point_to_string(casing.simple_uppercase_mapping)));
  785. generator.set("simple_lowercase_mapping", TRY(optional_code_point_to_string(casing.simple_lowercase_mapping)));
  786. generator.set("simple_titlecase_mapping", TRY(optional_code_point_to_string(casing.simple_titlecase_mapping)));
  787. generator.set("special_casing_start_index", TRY(first_index_to_string(casing.special_casing_indices)));
  788. generator.set("special_casing_size", TRY(String::number(casing.special_casing_indices.size())));
  789. generator.set("case_folding_start_index", TRY(first_index_to_string(casing.case_folding_indices)));
  790. generator.set("case_folding_size", TRY(String::number(casing.case_folding_indices.size())));
  791. generator.append(R"~~~(
  792. { @canonical_combining_class@, @simple_uppercase_mapping@, @simple_lowercase_mapping@, @simple_titlecase_mapping@, @special_casing_start_index@, @special_casing_size@, @case_folding_start_index@, @case_folding_size@ },)~~~");
  793. }
  794. generator.append(R"~~~(
  795. } };
  796. )~~~");
  797. return {};
  798. };
  799. auto append_property_table = [&](auto collection_snake, auto const& unique_properties) -> ErrorOr<void> {
  800. generator.set("name", TRY(String::formatted("{}_unique_properties", collection_snake)));
  801. generator.set("outer_size", TRY(String::number(unique_properties.size())));
  802. generator.set("inner_size", TRY(String::number(unique_properties[0].size())));
  803. generator.append(R"~~~(
  804. static constexpr Array<Array<bool, @inner_size@>, @outer_size@> @name@ { {)~~~");
  805. for (auto const& property_set : unique_properties) {
  806. generator.append(R"~~~(
  807. { )~~~");
  808. for (auto value : property_set) {
  809. generator.set("value", TRY(String::formatted("{}", value)));
  810. generator.append("@value@, ");
  811. }
  812. generator.append(" },");
  813. }
  814. generator.append(R"~~~(
  815. } };
  816. )~~~");
  817. return {};
  818. };
  819. auto append_code_point_tables = [&](StringView collection_snake, auto const& tables, auto& append_unique_properties) -> ErrorOr<void> {
  820. auto append_stage = [&](auto const& stage, auto name, auto type) -> ErrorOr<void> {
  821. generator.set("name", TRY(String::formatted("{}_{}", collection_snake, name)));
  822. generator.set("size", TRY(String::number(stage.size())));
  823. generator.set("type", type);
  824. generator.append(R"~~~(
  825. static constexpr Array<@type@, @size@> @name@ { {
  826. )~~~");
  827. static constexpr size_t max_values_per_row = 300;
  828. size_t values_in_current_row = 0;
  829. for (auto value : stage) {
  830. if (values_in_current_row++ > 0)
  831. generator.append(", ");
  832. generator.set("value", TRY(String::number(value)));
  833. generator.append("@value@");
  834. if (values_in_current_row == max_values_per_row) {
  835. values_in_current_row = 0;
  836. generator.append(",\n ");
  837. }
  838. }
  839. generator.append(R"~~~(
  840. } };
  841. )~~~");
  842. return {};
  843. };
  844. TRY(append_stage(tables.stage1, "stage1"sv, "u16"sv));
  845. TRY(append_stage(tables.stage2, "stage2"sv, "u16"sv));
  846. TRY(append_unique_properties(collection_snake, tables.unique_properties));
  847. return {};
  848. };
  849. TRY(append_code_point_tables("s_casings"sv, unicode_data.casing_tables, append_casing_table));
  850. TRY(append_code_point_tables("s_general_categories"sv, unicode_data.general_category_tables, append_property_table));
  851. TRY(append_code_point_tables("s_properties"sv, unicode_data.property_tables, append_property_table));
  852. TRY(append_code_point_tables("s_scripts"sv, unicode_data.script_tables, append_property_table));
  853. TRY(append_code_point_tables("s_script_extensions"sv, unicode_data.script_extension_tables, append_property_table));
  854. TRY(append_code_point_tables("s_grapheme_break_properties"sv, unicode_data.grapheme_break_tables, append_property_table));
  855. TRY(append_code_point_tables("s_word_break_properties"sv, unicode_data.word_break_tables, append_property_table));
  856. TRY(append_code_point_tables("s_sentence_break_properties"sv, unicode_data.sentence_break_tables, append_property_table));
  857. {
  858. constexpr size_t max_bidi_classes_per_row = 20;
  859. size_t bidi_classes_in_current_row = 0;
  860. generator.set("size"sv, ByteString::number(unicode_data.code_point_bidirectional_classes.size()));
  861. generator.append(R"~~~(
  862. static constexpr Array<BidiClassData, @size@> s_bidirectional_classes { {
  863. )~~~");
  864. for (auto const& data : unicode_data.code_point_bidirectional_classes) {
  865. if (bidi_classes_in_current_row++ > 0)
  866. generator.append(", ");
  867. generator.set("first", ByteString::formatted("{:#x}", data.code_point_range.first));
  868. generator.set("last", ByteString::formatted("{:#x}", data.code_point_range.last));
  869. generator.set("bidi_class", data.bidi_class);
  870. generator.append("{ { @first@, @last@ }, BidirectionalClass::@bidi_class@ }");
  871. if (bidi_classes_in_current_row == max_bidi_classes_per_row) {
  872. bidi_classes_in_current_row = 0;
  873. generator.append(",\n ");
  874. }
  875. }
  876. generator.append(R"~~~(
  877. } };
  878. )~~~");
  879. }
  880. generator.append(R"~~~(
  881. static CasingTable const& casing_table_for_code_point(u32 code_point)
  882. {
  883. auto stage1_index = code_point >> @CODE_POINT_TABLES_LSB_COUNT@;
  884. auto stage2_index = s_casings_stage1[stage1_index] + (code_point & @CODE_POINT_TABLES_LSB_MASK@);
  885. auto unique_properties_index = s_casings_stage2[stage2_index];
  886. return s_casings_unique_properties[unique_properties_index];
  887. }
  888. )~~~");
  889. auto append_code_point_mapping_search = [&](StringView method, StringView mapping, Optional<StringView> const& fallback = {}) {
  890. generator.set("method", method);
  891. generator.set("mapping", mapping);
  892. generator.append(R"~~~(
  893. u32 @method@(u32 code_point)
  894. {
  895. auto const& casing_table = casing_table_for_code_point(code_point);
  896. auto mapping = casing_table.@mapping@;
  897. )~~~");
  898. if (fallback.has_value()) {
  899. generator.set("fallback", *fallback);
  900. generator.append(R"~~~(
  901. return mapping == -1 ? @fallback@ : static_cast<u32>(mapping);)~~~");
  902. } else {
  903. generator.append(R"~~~(
  904. return mapping;)~~~");
  905. }
  906. generator.append(R"~~~(
  907. }
  908. )~~~");
  909. };
  910. append_code_point_mapping_search("canonical_combining_class"sv, "canonical_combining_class"sv);
  911. append_code_point_mapping_search("to_unicode_uppercase"sv, "simple_uppercase_mapping"sv, "code_point"sv);
  912. append_code_point_mapping_search("to_unicode_lowercase"sv, "simple_lowercase_mapping"sv, "code_point"sv);
  913. append_code_point_mapping_search("to_unicode_titlecase"sv, "simple_titlecase_mapping"sv, "code_point"sv);
  914. generator.append(R"~~~(
  915. ReadonlySpan<SpecialCasing> special_case_mapping(u32 code_point)
  916. {
  917. auto const& casing_table = casing_table_for_code_point(code_point);
  918. if (casing_table.special_casing_size == 0)
  919. return {};
  920. return s_special_case.span().slice(casing_table.special_casing_start_index, casing_table.special_casing_size);
  921. }
  922. ReadonlySpan<CaseFolding> case_folding_mapping(u32 code_point)
  923. {
  924. auto const& casing_table = casing_table_for_code_point(code_point);
  925. if (casing_table.case_folding_size == 0)
  926. return {};
  927. return s_case_folding.span().slice(casing_table.case_folding_start_index, casing_table.case_folding_size);
  928. }
  929. Optional<CodePointDecomposition const> code_point_decomposition(u32 code_point)
  930. {
  931. auto const* mapping = binary_search(s_decomposition_mappings, code_point, nullptr, CodePointComparator<CodePointDecompositionRaw> {});
  932. if (mapping == nullptr)
  933. return {};
  934. return CodePointDecomposition { mapping->code_point, mapping->tag, ReadonlySpan<u32> { s_decomposition_mappings_data.data() + mapping->decomposition_index, mapping->decomposition_count } };
  935. }
  936. Optional<u32> code_point_composition(u32 first_code_point, u32 second_code_point)
  937. {
  938. size_t mapping_index;
  939. if (!binary_search(s_composition_mappings, first_code_point, &mapping_index, CodePointComparator<CodePointCompositionRaw> {}))
  940. return {};
  941. while (mapping_index > 0 && s_composition_mappings[mapping_index - 1].code_point == first_code_point)
  942. mapping_index--;
  943. for (; mapping_index < s_composition_mappings.size() && s_composition_mappings[mapping_index].code_point == first_code_point; ++mapping_index) {
  944. if (s_composition_mappings[mapping_index].second_code_point == second_code_point)
  945. return s_composition_mappings[mapping_index].combined_code_point;
  946. }
  947. return {};
  948. }
  949. Optional<BidirectionalClass> bidirectional_class(u32 code_point)
  950. {
  951. if (auto const* entry = binary_search(s_bidirectional_classes, code_point, nullptr, CodePointBidiClassComparator {}))
  952. return entry->bidi_class;
  953. return {};
  954. }
  955. )~~~");
  956. auto append_prop_search = [&](StringView enum_title, StringView enum_snake, StringView collection_name) -> ErrorOr<void> {
  957. generator.set("enum_title", enum_title);
  958. generator.set("enum_snake", enum_snake);
  959. generator.set("collection_name", collection_name);
  960. generator.append(R"~~~(
  961. bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
  962. {
  963. auto stage1_index = code_point >> @CODE_POINT_TABLES_LSB_COUNT@;
  964. auto stage2_index = @collection_name@_stage1[stage1_index] + (code_point & @CODE_POINT_TABLES_LSB_MASK@);
  965. auto unique_properties_index = @collection_name@_stage2[stage2_index];
  966. auto const& property_set = @collection_name@_unique_properties[unique_properties_index];
  967. return property_set[to_underlying(@enum_snake@)];
  968. }
  969. )~~~");
  970. return {};
  971. };
  972. auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& prop_list, Vector<Alias> const& aliases) -> ErrorOr<void> {
  973. HashValueMap<StringView> hashes;
  974. TRY(hashes.try_ensure_capacity(prop_list.size() + aliases.size()));
  975. ValueFromStringOptions options {};
  976. for (auto const& prop : prop_list) {
  977. if constexpr (IsSame<RemoveCVReference<decltype(prop)>, ByteString>) {
  978. hashes.set(CaseInsensitiveASCIIStringViewTraits::hash(prop), prop);
  979. options.sensitivity = CaseSensitivity::CaseInsensitive;
  980. } else {
  981. hashes.set(prop.key.hash(), prop.key);
  982. }
  983. }
  984. for (auto const& alias : aliases)
  985. hashes.set(alias.alias.hash(), alias.alias);
  986. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
  987. return {};
  988. };
  989. TRY(append_from_string("Locale"sv, "locale"sv, unicode_data.locales, {}));
  990. TRY(append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv));
  991. TRY(append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases));
  992. TRY(append_prop_search("Property"sv, "property"sv, "s_properties"sv));
  993. TRY(append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases));
  994. TRY(append_prop_search("Script"sv, "script"sv, "s_scripts"sv));
  995. TRY(append_prop_search("Script"sv, "script_extension"sv, "s_script_extensions"sv));
  996. TRY(append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases));
  997. TRY(append_prop_search("GraphemeBreakProperty"sv, "grapheme_break_property"sv, "s_grapheme_break_properties"sv));
  998. TRY(append_prop_search("WordBreakProperty"sv, "word_break_property"sv, "s_word_break_properties"sv));
  999. TRY(append_prop_search("SentenceBreakProperty"sv, "sentence_break_property"sv, "s_sentence_break_properties"sv));
  1000. TRY(append_from_string("BidirectionalClass"sv, "bidirectional_class"sv, unicode_data.bidirectional_classes, {}));
  1001. generator.append(R"~~~(
  1002. }
  1003. )~~~");
  1004. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  1005. return {};
  1006. }
  1007. static Vector<u32> flatten_code_point_ranges(Vector<Unicode::CodePointRange> const& code_points)
  1008. {
  1009. Vector<u32> flattened;
  1010. for (auto const& range : code_points) {
  1011. flattened.grow_capacity(range.last - range.first);
  1012. for (u32 code_point = range.first; code_point <= range.last; ++code_point)
  1013. flattened.append(code_point);
  1014. }
  1015. return flattened;
  1016. }
  1017. static Vector<Unicode::CodePointRange> form_code_point_ranges(Vector<u32> code_points)
  1018. {
  1019. Vector<Unicode::CodePointRange> ranges;
  1020. u32 range_start = code_points[0];
  1021. u32 range_end = range_start;
  1022. for (size_t i = 1; i < code_points.size(); ++i) {
  1023. u32 code_point = code_points[i];
  1024. if ((code_point - range_end) == 1) {
  1025. range_end = code_point;
  1026. } else {
  1027. ranges.append({ range_start, range_end });
  1028. range_start = code_point;
  1029. range_end = code_point;
  1030. }
  1031. }
  1032. ranges.append({ range_start, range_end });
  1033. return ranges;
  1034. }
  1035. static void sort_and_merge_code_point_ranges(Vector<Unicode::CodePointRange>& code_points)
  1036. {
  1037. quick_sort(code_points, [](auto const& range1, auto const& range2) {
  1038. return range1.first < range2.first;
  1039. });
  1040. for (size_t i = 0; i < code_points.size() - 1;) {
  1041. if (code_points[i].last >= code_points[i + 1].first) {
  1042. code_points[i].last = max(code_points[i].last, code_points[i + 1].last);
  1043. code_points.remove(i + 1);
  1044. } else {
  1045. ++i;
  1046. }
  1047. }
  1048. auto all_code_points = flatten_code_point_ranges(code_points);
  1049. code_points = form_code_point_ranges(all_code_points);
  1050. }
  1051. static void populate_general_category_unions(PropList& general_categories)
  1052. {
  1053. // The Unicode standard defines General Category values which are not in any UCD file. These
  1054. // values are simply unions of other values.
  1055. // https://www.unicode.org/reports/tr44/#GC_Values_Table
  1056. auto populate_union = [&](auto alias, auto categories) {
  1057. auto& code_points = general_categories.ensure(alias);
  1058. for (auto const& category : categories)
  1059. code_points.extend(general_categories.find(category)->value);
  1060. sort_and_merge_code_point_ranges(code_points);
  1061. };
  1062. populate_union("LC"sv, Array { "Ll"sv, "Lu"sv, "Lt"sv });
  1063. populate_union("L"sv, Array { "Lu"sv, "Ll"sv, "Lt"sv, "Lm"sv, "Lo"sv });
  1064. populate_union("M"sv, Array { "Mn"sv, "Mc"sv, "Me"sv });
  1065. populate_union("N"sv, Array { "Nd"sv, "Nl"sv, "No"sv });
  1066. populate_union("P"sv, Array { "Pc"sv, "Pd"sv, "Ps"sv, "Pe"sv, "Pi"sv, "Pf"sv, "Po"sv });
  1067. populate_union("S"sv, Array { "Sm"sv, "Sc"sv, "Sk"sv, "So"sv });
  1068. populate_union("Z"sv, Array { "Zs"sv, "Zl"sv, "Zp"sv });
  1069. populate_union("C"sv, Array { "Cc"sv, "Cf"sv, "Cs"sv, "Co"sv, "Cn"sv });
  1070. }
  1071. static ErrorOr<void> normalize_script_extensions(PropList& script_extensions, PropList const& script_list, Vector<Alias> const& script_aliases)
  1072. {
  1073. // The ScriptExtensions UCD file lays out its code point ranges rather uniquely compared to
  1074. // other files. The Script listed on each line may either be a full Script string or an aliased
  1075. // abbreviation. Further, the extensions may or may not include the base Script list. Normalize
  1076. // the extensions here to be keyed by the full Script name and always include the base list.
  1077. auto extensions = move(script_extensions);
  1078. script_extensions = TRY(script_list.clone());
  1079. for (auto const& extension : extensions) {
  1080. auto it = find_if(script_aliases.begin(), script_aliases.end(), [&](auto const& alias) { return extension.key == alias.alias; });
  1081. auto const& key = (it == script_aliases.end()) ? extension.key : it->name;
  1082. auto& code_points = script_extensions.find(key)->value;
  1083. code_points.extend(extension.value);
  1084. sort_and_merge_code_point_ranges(code_points);
  1085. }
  1086. // Lastly, the Common and Inherited script extensions are special. They must not contain any
  1087. // code points which appear in other script extensions. The ScriptExtensions UCD file does not
  1088. // list these extensions, therefore this peculiarity must be handled programmatically.
  1089. // https://www.unicode.org/reports/tr24/#Assignment_ScriptX_Values
  1090. auto code_point_has_other_extension = [&](StringView key, u32 code_point) {
  1091. for (auto const& extension : extensions) {
  1092. if (extension.key == key)
  1093. continue;
  1094. if (any_of(extension.value, [&](auto const& r) { return (r.first <= code_point) && (code_point <= r.last); }))
  1095. return true;
  1096. }
  1097. return false;
  1098. };
  1099. auto get_code_points_without_other_extensions = [&](StringView key) {
  1100. auto code_points = flatten_code_point_ranges(script_list.find(key)->value);
  1101. code_points.remove_all_matching([&](u32 c) { return code_point_has_other_extension(key, c); });
  1102. return code_points;
  1103. };
  1104. auto common_code_points = get_code_points_without_other_extensions("Common"sv);
  1105. script_extensions.set("Common"sv, form_code_point_ranges(common_code_points));
  1106. auto inherited_code_points = get_code_points_without_other_extensions("Inherited"sv);
  1107. script_extensions.set("Inherited"sv, form_code_point_ranges(inherited_code_points));
  1108. return {};
  1109. }
  1110. struct CasingMetadata {
  1111. using ConstIterator = typename Vector<CodePointData>::ConstIterator;
  1112. CasingMetadata(Vector<CodePointData> const& code_point_data)
  1113. : iterator(code_point_data.begin())
  1114. , end(code_point_data.end())
  1115. {
  1116. }
  1117. ConstIterator iterator;
  1118. ConstIterator const end;
  1119. Vector<size_t> current_block;
  1120. HashMap<decltype(current_block), size_t> unique_blocks;
  1121. };
  1122. struct PropertyMetadata {
  1123. static ErrorOr<PropertyMetadata> create(PropList& property_list)
  1124. {
  1125. PropertyMetadata data;
  1126. TRY(data.property_values.try_ensure_capacity(property_list.size()));
  1127. TRY(data.property_set.try_ensure_capacity(property_list.size()));
  1128. auto property_names = property_list.keys();
  1129. quick_sort(property_names);
  1130. for (auto& property_name : property_names) {
  1131. auto& code_point_ranges = property_list.get(property_name).value();
  1132. data.property_values.unchecked_append(move(code_point_ranges));
  1133. }
  1134. return data;
  1135. }
  1136. Vector<typename PropList::ValueType> property_values;
  1137. PropertyTable property_set;
  1138. Vector<size_t> current_block;
  1139. HashMap<decltype(current_block), size_t> unique_blocks;
  1140. };
  1141. // The goal here is to produce a set of tables that represent a category of code point properties for every code point.
  1142. // The most naive method would be to generate a single table per category, each with one entry per code point. Each of
  1143. // those tables would have a size of 0x10ffff though, which is a non-starter. Instead, we create a set of 2-stage lookup
  1144. // tables per category.
  1145. //
  1146. // To do so, it's important to note that Unicode tends to organize code points with similar properties together. This
  1147. // leads to long series of code points with identical properties. Therefore, if we divide the 0x10ffff code points into
  1148. // fixed-size blocks, many of those blocks will also be identical.
  1149. //
  1150. // So we iterate over every code point, classifying each one for the category of interest. We represent a classification
  1151. // as a list of booleans. We store the classification in the CodePointTables::unique_properties list for this category.
  1152. // As the name implies, this list is de-duplicated; we store the index into this list in a separate list, which we call
  1153. // a "block".
  1154. //
  1155. // As we iterate, we "pause" every BLOCK_SIZE code points to examine the block. If the block is unique so far, we extend
  1156. // CodePointTables::stage2 with the entries of that block (so CodePointTables::stage2 is also a list of indices into
  1157. // CodePointTables::unique_properties). We then append the index of the start of that block in CodePointTables::stage2
  1158. // to CodePointTables::stage1.
  1159. //
  1160. // The value of BLOCK_SIZE is determined by CodePointTables::MSB_COUNT and CodePointTables::LSB_COUNT. We need 24 bits
  1161. // to describe all code points; the blocks we create are based on splitting these bits into 2 segments. We currently use
  1162. // a 16:8 bit split. So when perform a runtime lookup of a code point in the 2-stage tables, we:
  1163. //
  1164. // 1. Use most-significant 16 bits of the code point as the index into CodePointTables::stage1. That value is the
  1165. // index into CodePointTables::stage2 of the start of the block that contains properties for this code point.
  1166. //
  1167. // 2. Add the least-significant 8 bits of the code point to that value, to use as the index into
  1168. // CodePointTables::stage2. As described above, that value is the index into CodePointTables::unique_properties,
  1169. // which contains the classification for this code point.
  1170. //
  1171. // Using the code point GeneralCategory as an example, we end up with a CodePointTables::stage1 with a size of ~4000,
  1172. // a CodePointTables::stage2 with a size of ~40,000, and a CodePointTables::unique_properties with a size of ~30. So
  1173. // this process reduces over 1 million entries (0x10ffff) to ~44,030.
  1174. //
  1175. // For much more in-depth reading, see: https://icu.unicode.org/design/struct/utrie
  1176. static constexpr auto MAX_CODE_POINT = 0x10ffffu;
  1177. template<typename T>
  1178. static ErrorOr<void> update_tables(u32 code_point, CodePointTables<T>& tables, auto& metadata, auto const& values)
  1179. {
  1180. static constexpr auto BLOCK_SIZE = CODE_POINT_TABLES_LSB_MASK + 1;
  1181. size_t unique_properties_index = 0;
  1182. if (auto block_index = tables.unique_properties.find_first_index(values); block_index.has_value()) {
  1183. unique_properties_index = *block_index;
  1184. } else {
  1185. unique_properties_index = tables.unique_properties.size();
  1186. TRY(tables.unique_properties.try_append(values));
  1187. }
  1188. TRY(metadata.current_block.try_append(unique_properties_index));
  1189. if (metadata.current_block.size() == BLOCK_SIZE || code_point == MAX_CODE_POINT) {
  1190. size_t stage2_index = 0;
  1191. if (auto block_index = metadata.unique_blocks.get(metadata.current_block); block_index.has_value()) {
  1192. stage2_index = *block_index;
  1193. } else {
  1194. stage2_index = tables.stage2.size();
  1195. TRY(tables.stage2.try_extend(metadata.current_block));
  1196. TRY(metadata.unique_blocks.try_set(metadata.current_block, stage2_index));
  1197. }
  1198. TRY(tables.stage1.try_append(stage2_index));
  1199. metadata.current_block.clear_with_capacity();
  1200. }
  1201. return {};
  1202. }
  1203. static ErrorOr<void> create_code_point_tables(UnicodeData& unicode_data)
  1204. {
  1205. auto update_casing_tables = [&]<typename T>(u32 code_point, CodePointTables<T>& tables, CasingMetadata& metadata) -> ErrorOr<void> {
  1206. CasingTable casing {};
  1207. while (metadata.iterator != metadata.end) {
  1208. if (code_point < metadata.iterator->code_point)
  1209. break;
  1210. if (code_point == metadata.iterator->code_point) {
  1211. casing = move(metadata.iterator->casing);
  1212. break;
  1213. }
  1214. ++metadata.iterator;
  1215. }
  1216. TRY(update_tables(code_point, tables, metadata, casing));
  1217. return {};
  1218. };
  1219. auto update_property_tables = [&]<typename T>(u32 code_point, CodePointTables<T>& tables, PropertyMetadata& metadata) -> ErrorOr<void> {
  1220. static Unicode::CodePointRangeComparator comparator {};
  1221. for (auto& property_values : metadata.property_values) {
  1222. size_t ranges_to_remove = 0;
  1223. auto has_property = false;
  1224. for (auto const& range : property_values) {
  1225. if (auto comparison = comparator(code_point, range); comparison <= 0) {
  1226. has_property = comparison == 0;
  1227. break;
  1228. }
  1229. ++ranges_to_remove;
  1230. }
  1231. metadata.property_set.unchecked_append(has_property);
  1232. property_values.remove(0, ranges_to_remove);
  1233. }
  1234. TRY(update_tables(code_point, tables, metadata, metadata.property_set));
  1235. metadata.property_set.clear_with_capacity();
  1236. return {};
  1237. };
  1238. CasingMetadata casing_metadata { unicode_data.code_point_data };
  1239. auto general_category_metadata = TRY(PropertyMetadata::create(unicode_data.general_categories));
  1240. auto property_metadata = TRY(PropertyMetadata::create(unicode_data.prop_list));
  1241. auto script_metadata = TRY(PropertyMetadata::create(unicode_data.script_list));
  1242. auto script_extension_metadata = TRY(PropertyMetadata::create(unicode_data.script_extensions));
  1243. auto grapheme_break_metadata = TRY(PropertyMetadata::create(unicode_data.grapheme_break_props));
  1244. auto word_break_metadata = TRY(PropertyMetadata::create(unicode_data.word_break_props));
  1245. auto sentence_break_metadata = TRY(PropertyMetadata::create(unicode_data.sentence_break_props));
  1246. for (u32 code_point = 0; code_point <= MAX_CODE_POINT; ++code_point) {
  1247. TRY(update_casing_tables(code_point, unicode_data.casing_tables, casing_metadata));
  1248. TRY(update_property_tables(code_point, unicode_data.general_category_tables, general_category_metadata));
  1249. TRY(update_property_tables(code_point, unicode_data.property_tables, property_metadata));
  1250. TRY(update_property_tables(code_point, unicode_data.script_tables, script_metadata));
  1251. TRY(update_property_tables(code_point, unicode_data.script_extension_tables, script_extension_metadata));
  1252. TRY(update_property_tables(code_point, unicode_data.grapheme_break_tables, grapheme_break_metadata));
  1253. TRY(update_property_tables(code_point, unicode_data.word_break_tables, word_break_metadata));
  1254. TRY(update_property_tables(code_point, unicode_data.sentence_break_tables, sentence_break_metadata));
  1255. }
  1256. return {};
  1257. }
  1258. ErrorOr<int> serenity_main(Main::Arguments arguments)
  1259. {
  1260. StringView generated_header_path;
  1261. StringView generated_implementation_path;
  1262. StringView unicode_data_path;
  1263. StringView special_casing_path;
  1264. StringView case_folding_path;
  1265. StringView derived_general_category_path;
  1266. StringView prop_list_path;
  1267. StringView derived_core_prop_path;
  1268. StringView derived_binary_prop_path;
  1269. StringView prop_alias_path;
  1270. StringView prop_value_alias_path;
  1271. StringView scripts_path;
  1272. StringView script_extensions_path;
  1273. StringView emoji_data_path;
  1274. StringView normalization_path;
  1275. StringView grapheme_break_path;
  1276. StringView word_break_path;
  1277. StringView sentence_break_path;
  1278. Core::ArgsParser args_parser;
  1279. args_parser.add_option(generated_header_path, "Path to the Unicode Data header file to generate", "generated-header-path", 'h', "generated-header-path");
  1280. args_parser.add_option(generated_implementation_path, "Path to the Unicode Data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  1281. args_parser.add_option(unicode_data_path, "Path to UnicodeData.txt file", "unicode-data-path", 'u', "unicode-data-path");
  1282. args_parser.add_option(special_casing_path, "Path to SpecialCasing.txt file", "special-casing-path", 's', "special-casing-path");
  1283. args_parser.add_option(case_folding_path, "Path to CaseFolding.txt file", "case-folding-path", 'o', "case-folding-path");
  1284. args_parser.add_option(derived_general_category_path, "Path to DerivedGeneralCategory.txt file", "derived-general-category-path", 'g', "derived-general-category-path");
  1285. args_parser.add_option(prop_list_path, "Path to PropList.txt file", "prop-list-path", 'p', "prop-list-path");
  1286. args_parser.add_option(derived_core_prop_path, "Path to DerivedCoreProperties.txt file", "derived-core-prop-path", 'd', "derived-core-prop-path");
  1287. args_parser.add_option(derived_binary_prop_path, "Path to DerivedBinaryProperties.txt file", "derived-binary-prop-path", 'b', "derived-binary-prop-path");
  1288. args_parser.add_option(prop_alias_path, "Path to PropertyAliases.txt file", "prop-alias-path", 'a', "prop-alias-path");
  1289. args_parser.add_option(prop_value_alias_path, "Path to PropertyValueAliases.txt file", "prop-value-alias-path", 'v', "prop-value-alias-path");
  1290. args_parser.add_option(scripts_path, "Path to Scripts.txt file", "scripts-path", 'r', "scripts-path");
  1291. args_parser.add_option(script_extensions_path, "Path to ScriptExtensions.txt file", "script-extensions-path", 'x', "script-extensions-path");
  1292. args_parser.add_option(emoji_data_path, "Path to emoji-data.txt file", "emoji-data-path", 'e', "emoji-data-path");
  1293. args_parser.add_option(normalization_path, "Path to DerivedNormalizationProps.txt file", "normalization-path", 'n', "normalization-path");
  1294. args_parser.add_option(grapheme_break_path, "Path to GraphemeBreakProperty.txt file", "grapheme-break-path", 'f', "grapheme-break-path");
  1295. args_parser.add_option(word_break_path, "Path to WordBreakProperty.txt file", "word-break-path", 'w', "word-break-path");
  1296. args_parser.add_option(sentence_break_path, "Path to SentenceBreakProperty.txt file", "sentence-break-path", 'i', "sentence-break-path");
  1297. args_parser.parse(arguments);
  1298. auto generated_header_file = TRY(open_file(generated_header_path, Core::File::OpenMode::Write));
  1299. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::File::OpenMode::Write));
  1300. auto unicode_data_file = TRY(open_file(unicode_data_path, Core::File::OpenMode::Read));
  1301. auto derived_general_category_file = TRY(open_file(derived_general_category_path, Core::File::OpenMode::Read));
  1302. auto special_casing_file = TRY(open_file(special_casing_path, Core::File::OpenMode::Read));
  1303. auto case_folding_file = TRY(open_file(case_folding_path, Core::File::OpenMode::Read));
  1304. auto prop_list_file = TRY(open_file(prop_list_path, Core::File::OpenMode::Read));
  1305. auto derived_core_prop_file = TRY(open_file(derived_core_prop_path, Core::File::OpenMode::Read));
  1306. auto derived_binary_prop_file = TRY(open_file(derived_binary_prop_path, Core::File::OpenMode::Read));
  1307. auto prop_alias_file = TRY(open_file(prop_alias_path, Core::File::OpenMode::Read));
  1308. auto prop_value_alias_file = TRY(open_file(prop_value_alias_path, Core::File::OpenMode::Read));
  1309. auto scripts_file = TRY(open_file(scripts_path, Core::File::OpenMode::Read));
  1310. auto script_extensions_file = TRY(open_file(script_extensions_path, Core::File::OpenMode::Read));
  1311. auto emoji_data_file = TRY(open_file(emoji_data_path, Core::File::OpenMode::Read));
  1312. auto normalization_file = TRY(open_file(normalization_path, Core::File::OpenMode::Read));
  1313. auto grapheme_break_file = TRY(open_file(grapheme_break_path, Core::File::OpenMode::Read));
  1314. auto word_break_file = TRY(open_file(word_break_path, Core::File::OpenMode::Read));
  1315. auto sentence_break_file = TRY(open_file(sentence_break_path, Core::File::OpenMode::Read));
  1316. UnicodeData unicode_data {};
  1317. TRY(parse_special_casing(*special_casing_file, unicode_data));
  1318. TRY(parse_case_folding(*case_folding_file, unicode_data));
  1319. TRY(parse_prop_list(*derived_general_category_file, unicode_data.general_categories));
  1320. TRY(parse_prop_list(*prop_list_file, unicode_data.prop_list));
  1321. TRY(parse_prop_list(*derived_core_prop_file, unicode_data.prop_list));
  1322. TRY(parse_prop_list(*derived_binary_prop_file, unicode_data.prop_list));
  1323. TRY(parse_prop_list(*emoji_data_file, unicode_data.prop_list));
  1324. TRY(parse_normalization_props(*normalization_file, unicode_data));
  1325. TRY(parse_alias_list(*prop_alias_file, unicode_data.prop_list, unicode_data.prop_aliases));
  1326. TRY(parse_prop_list(*scripts_file, unicode_data.script_list));
  1327. TRY(parse_prop_list(*script_extensions_file, unicode_data.script_extensions, true));
  1328. TRY(parse_prop_list(*grapheme_break_file, unicode_data.grapheme_break_props));
  1329. TRY(parse_prop_list(*word_break_file, unicode_data.word_break_props));
  1330. TRY(parse_prop_list(*sentence_break_file, unicode_data.sentence_break_props));
  1331. populate_general_category_unions(unicode_data.general_categories);
  1332. TRY(parse_unicode_data(*unicode_data_file, unicode_data));
  1333. TRY(parse_value_alias_list(*prop_value_alias_file, "gc"sv, unicode_data.general_categories.keys(), unicode_data.general_category_aliases));
  1334. TRY(parse_value_alias_list(*prop_value_alias_file, "sc"sv, unicode_data.script_list.keys(), unicode_data.script_aliases, false));
  1335. TRY(normalize_script_extensions(unicode_data.script_extensions, unicode_data.script_list, unicode_data.script_aliases));
  1336. TRY(create_code_point_tables(unicode_data));
  1337. TRY(generate_unicode_data_header(*generated_header_file, unicode_data));
  1338. TRY(generate_unicode_data_implementation(*generated_implementation_file, unicode_data));
  1339. return 0;
  1340. }