GenerateUnicodeData.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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/CharacterTypes.h>
  10. #include <AK/Find.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/Optional.h>
  13. #include <AK/QuickSort.h>
  14. #include <AK/SourceGenerator.h>
  15. #include <AK/String.h>
  16. #include <AK/StringUtils.h>
  17. #include <AK/Types.h>
  18. #include <AK/Vector.h>
  19. #include <LibCore/ArgsParser.h>
  20. #include <LibCore/Stream.h>
  21. // Some code points are excluded from UnicodeData.txt, and instead are part of a "range" of code
  22. // points, as indicated by the "name" field. For example:
  23. // 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;
  24. // 4DBF;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;;
  25. struct CodePointRange {
  26. u32 first;
  27. u32 last;
  28. };
  29. // SpecialCasing source: https://www.unicode.org/Public/13.0.0/ucd/SpecialCasing.txt
  30. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#SpecialCasing.txt
  31. struct SpecialCasing {
  32. u32 index { 0 };
  33. u32 code_point { 0 };
  34. Vector<u32> lowercase_mapping;
  35. Vector<u32> uppercase_mapping;
  36. Vector<u32> titlecase_mapping;
  37. String locale;
  38. String condition;
  39. };
  40. // PropList source: https://www.unicode.org/Public/13.0.0/ucd/PropList.txt
  41. // Property descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#PropList.txt
  42. using PropList = HashMap<String, Vector<CodePointRange>>;
  43. // Normalization source: https://www.unicode.org/Public/13.0.0/ucd/DerivedNormalizationProps.txt
  44. // Normalization descriptions: https://www.unicode.org/reports/tr44/#DerivedNormalizationProps.txt
  45. enum class QuickCheck {
  46. Yes,
  47. No,
  48. Maybe,
  49. };
  50. struct Normalization {
  51. CodePointRange code_point_range;
  52. Vector<u32> value;
  53. QuickCheck quick_check { QuickCheck::Yes };
  54. };
  55. using NormalizationProps = HashMap<String, Vector<Normalization>>;
  56. struct CodePointName {
  57. CodePointRange code_point_range;
  58. StringView name;
  59. };
  60. // UnicodeData source: https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt
  61. // Field descriptions: https://www.unicode.org/reports/tr44/tr44-13.html#UnicodeData.txt
  62. // https://www.unicode.org/reports/tr44/#General_Category_Values
  63. struct CodePointData {
  64. u32 code_point { 0 };
  65. String name;
  66. Optional<StringView> abbreviation;
  67. u8 canonical_combining_class { 0 };
  68. String bidi_class;
  69. String decomposition_type;
  70. Optional<i8> numeric_value_decimal;
  71. Optional<i8> numeric_value_digit;
  72. Optional<i8> numeric_value_numeric;
  73. bool bidi_mirrored { false };
  74. String unicode_1_name;
  75. String iso_comment;
  76. Optional<u32> simple_uppercase_mapping;
  77. Optional<u32> simple_lowercase_mapping;
  78. Optional<u32> simple_titlecase_mapping;
  79. Vector<u32> special_casing_indices;
  80. };
  81. struct UnicodeData {
  82. u32 code_points_with_non_zero_combining_class { 0 };
  83. u32 simple_uppercase_mapping_size { 0 };
  84. u32 simple_lowercase_mapping_size { 0 };
  85. Vector<SpecialCasing> special_casing;
  86. u32 code_points_with_special_casing { 0 };
  87. u32 largest_casing_transform_size { 0 };
  88. u32 largest_special_casing_size { 0 };
  89. Vector<String> conditions;
  90. Vector<CodePointData> code_point_data;
  91. HashMap<u32, String> code_point_abbreviations;
  92. HashMap<u32, String> code_point_display_name_aliases;
  93. Vector<CodePointName> code_point_display_names;
  94. PropList general_categories;
  95. Vector<Alias> general_category_aliases;
  96. // The Unicode standard defines additional properties (Any, Assigned, ASCII) which are not in
  97. // any UCD file. Assigned code point ranges are derived as this generator is executed.
  98. // https://unicode.org/reports/tr18/#General_Category_Property
  99. PropList prop_list {
  100. { "Any"sv, { { 0, 0x10ffff } } },
  101. { "Assigned"sv, {} },
  102. { "ASCII"sv, { { 0, 0x7f } } },
  103. };
  104. Vector<Alias> prop_aliases;
  105. PropList script_list {
  106. { "Unknown"sv, {} },
  107. };
  108. Vector<Alias> script_aliases;
  109. PropList script_extensions;
  110. // FIXME: We are not yet doing anything with this data. It will be needed for String.prototype.normalize.
  111. NormalizationProps normalization_props;
  112. PropList grapheme_break_props;
  113. PropList word_break_props;
  114. PropList sentence_break_props;
  115. };
  116. static Vector<u32> parse_code_point_list(StringView list)
  117. {
  118. Vector<u32> code_points;
  119. auto segments = list.split_view(' ');
  120. for (auto const& code_point : segments)
  121. code_points.append(AK::StringUtils::convert_to_uint_from_hex<u32>(code_point).value());
  122. return code_points;
  123. }
  124. static CodePointRange parse_code_point_range(StringView list)
  125. {
  126. CodePointRange code_point_range {};
  127. if (list.contains(".."sv)) {
  128. auto segments = list.split_view(".."sv);
  129. VERIFY(segments.size() == 2);
  130. auto begin = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  131. auto end = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[1]).value();
  132. code_point_range = { begin, end };
  133. } else {
  134. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(list).value();
  135. code_point_range = { code_point, code_point };
  136. }
  137. return code_point_range;
  138. }
  139. static ErrorOr<void> parse_special_casing(Core::Stream::BufferedFile& file, UnicodeData& unicode_data)
  140. {
  141. Array<u8, 1024> buffer;
  142. while (TRY(file.can_read_line())) {
  143. auto nread = TRY(file.read_line(buffer));
  144. StringView line { buffer.data(), nread };
  145. if (line.is_empty() || line.starts_with('#'))
  146. continue;
  147. if (auto index = line.find('#'); index.has_value())
  148. line = line.substring_view(0, *index);
  149. auto segments = line.split_view(';', true);
  150. VERIFY(segments.size() == 5 || segments.size() == 6);
  151. SpecialCasing casing {};
  152. casing.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  153. casing.lowercase_mapping = parse_code_point_list(segments[1]);
  154. casing.titlecase_mapping = parse_code_point_list(segments[2]);
  155. casing.uppercase_mapping = parse_code_point_list(segments[3]);
  156. if (auto condition = segments[4].trim_whitespace(); !condition.is_empty()) {
  157. auto conditions = condition.split_view(' ', true);
  158. VERIFY(conditions.size() == 1 || conditions.size() == 2);
  159. if (conditions.size() == 2) {
  160. casing.locale = conditions[0];
  161. casing.condition = conditions[1];
  162. } else if (all_of(conditions[0], is_ascii_lower_alpha)) {
  163. casing.locale = conditions[0];
  164. } else {
  165. casing.condition = conditions[0];
  166. }
  167. if (!casing.locale.is_empty())
  168. casing.locale = String::formatted("{:c}{}", to_ascii_uppercase(casing.locale[0]), casing.locale.substring_view(1));
  169. casing.condition = casing.condition.replace("_", "", true);
  170. if (!casing.condition.is_empty() && !unicode_data.conditions.contains_slow(casing.condition))
  171. unicode_data.conditions.append(casing.condition);
  172. }
  173. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.lowercase_mapping.size());
  174. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.titlecase_mapping.size());
  175. unicode_data.largest_casing_transform_size = max(unicode_data.largest_casing_transform_size, casing.uppercase_mapping.size());
  176. unicode_data.special_casing.append(move(casing));
  177. }
  178. quick_sort(unicode_data.special_casing, [](auto const& lhs, auto const& rhs) {
  179. if (lhs.code_point != rhs.code_point)
  180. return lhs.code_point < rhs.code_point;
  181. if (lhs.locale.is_empty() && !rhs.locale.is_empty())
  182. return false;
  183. if (!lhs.locale.is_empty() && rhs.locale.is_empty())
  184. return true;
  185. return lhs.locale < rhs.locale;
  186. });
  187. for (u32 i = 0; i < unicode_data.special_casing.size(); ++i)
  188. unicode_data.special_casing[i].index = i;
  189. return {};
  190. }
  191. static ErrorOr<void> parse_prop_list(Core::Stream::BufferedFile& file, PropList& prop_list, bool multi_value_property = false)
  192. {
  193. Array<u8, 1024> buffer;
  194. while (TRY(file.can_read_line())) {
  195. auto nread = TRY(file.read_line(buffer));
  196. StringView line { buffer.data(), nread };
  197. if (line.is_empty() || line.starts_with('#'))
  198. continue;
  199. if (auto index = line.find('#'); index.has_value())
  200. line = line.substring_view(0, *index);
  201. auto segments = line.split_view(';', true);
  202. VERIFY(segments.size() == 2);
  203. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  204. Vector<StringView> properties;
  205. if (multi_value_property)
  206. properties = segments[1].trim_whitespace().split_view(' ');
  207. else
  208. properties = { segments[1].trim_whitespace() };
  209. for (auto const& property : properties) {
  210. auto& code_points = prop_list.ensure(property.trim_whitespace());
  211. code_points.append(code_point_range);
  212. }
  213. }
  214. return {};
  215. }
  216. static ErrorOr<void> parse_alias_list(Core::Stream::BufferedFile& file, PropList const& prop_list, Vector<Alias>& prop_aliases)
  217. {
  218. String current_property;
  219. Array<u8, 1024> buffer;
  220. auto append_alias = [&](auto alias, auto property) {
  221. // Note: The alias files contain lines such as "Hyphen = Hyphen", which we should just skip.
  222. if (alias == property)
  223. return;
  224. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  225. if (!prop_list.contains(property))
  226. return;
  227. prop_aliases.append({ property, alias });
  228. };
  229. while (TRY(file.can_read_line())) {
  230. auto nread = TRY(file.read_line(buffer));
  231. StringView line { buffer.data(), nread };
  232. if (line.is_empty() || line.starts_with('#')) {
  233. if (line.ends_with("Properties"sv))
  234. current_property = line.substring_view(2);
  235. continue;
  236. }
  237. // Note: For now, we only care about Binary Property aliases for Unicode property escapes.
  238. if (current_property != "Binary Properties"sv)
  239. continue;
  240. auto segments = line.split_view(';', true);
  241. VERIFY((segments.size() == 2) || (segments.size() == 3));
  242. auto alias = segments[0].trim_whitespace();
  243. auto property = segments[1].trim_whitespace();
  244. append_alias(alias, property);
  245. if (segments.size() == 3) {
  246. alias = segments[2].trim_whitespace();
  247. append_alias(alias, property);
  248. }
  249. }
  250. return {};
  251. }
  252. static ErrorOr<void> parse_name_aliases(Core::Stream::BufferedFile& file, UnicodeData& unicode_data)
  253. {
  254. Array<u8, 1024> buffer;
  255. while (TRY(file.can_read_line())) {
  256. auto nread = TRY(file.read_line(buffer));
  257. StringView line { buffer.data(), nread };
  258. if (line.is_empty() || line.starts_with('#'))
  259. continue;
  260. auto segments = line.split_view(';', true);
  261. VERIFY(segments.size() == 3);
  262. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0].trim_whitespace());
  263. auto alias = segments[1].trim_whitespace();
  264. auto reason = segments[2].trim_whitespace();
  265. if (reason == "abbreviation"sv) {
  266. unicode_data.code_point_abbreviations.set(*code_point, alias);
  267. } else if (reason.is_one_of("correction"sv, "control"sv)) {
  268. if (!unicode_data.code_point_display_name_aliases.contains(*code_point))
  269. unicode_data.code_point_display_name_aliases.set(*code_point, alias);
  270. }
  271. }
  272. return {};
  273. }
  274. static ErrorOr<void> parse_value_alias_list(Core::Stream::BufferedFile& file, StringView desired_category, Vector<String> const& value_list, Vector<Alias>& prop_aliases, bool primary_value_is_first = true)
  275. {
  276. TRY(file.seek(0, Core::Stream::SeekMode::SetPosition));
  277. Array<u8, 1024> buffer;
  278. auto append_alias = [&](auto alias, auto value) {
  279. // Note: The value alias file contains lines such as "Ahom = Ahom", which we should just skip.
  280. if (alias == value)
  281. return;
  282. // FIXME: We will, eventually, need to find where missing properties are located and parse them.
  283. if (!value_list.contains_slow(value))
  284. return;
  285. prop_aliases.append({ value, alias });
  286. };
  287. while (TRY(file.can_read_line())) {
  288. auto nread = TRY(file.read_line(buffer));
  289. StringView line { buffer.data(), nread };
  290. if (line.is_empty() || line.starts_with('#'))
  291. continue;
  292. if (auto index = line.find('#'); index.has_value())
  293. line = line.substring_view(0, *index);
  294. auto segments = line.split_view(';', true);
  295. auto category = segments[0].trim_whitespace();
  296. if (category != desired_category)
  297. continue;
  298. VERIFY((segments.size() == 3) || (segments.size() == 4));
  299. auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
  300. auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
  301. append_alias(alias, value);
  302. if (segments.size() == 4) {
  303. alias = segments[3].trim_whitespace();
  304. append_alias(alias, value);
  305. }
  306. }
  307. return {};
  308. }
  309. static ErrorOr<void> parse_normalization_props(Core::Stream::BufferedFile& file, UnicodeData& unicode_data)
  310. {
  311. Array<u8, 1024> buffer;
  312. while (TRY(file.can_read_line())) {
  313. auto nread = TRY(file.read_line(buffer));
  314. StringView line { buffer.data(), nread };
  315. if (line.is_empty() || line.starts_with('#'))
  316. continue;
  317. if (auto index = line.find('#'); index.has_value())
  318. line = line.substring_view(0, *index);
  319. auto segments = line.split_view(';', true);
  320. VERIFY((segments.size() == 2) || (segments.size() == 3));
  321. auto code_point_range = parse_code_point_range(segments[0].trim_whitespace());
  322. auto property = segments[1].trim_whitespace().to_string();
  323. Vector<u32> value;
  324. QuickCheck quick_check = QuickCheck::Yes;
  325. if (segments.size() == 3) {
  326. auto value_or_quick_check = segments[2].trim_whitespace();
  327. if ((value_or_quick_check == "N"sv))
  328. quick_check = QuickCheck::No;
  329. else if ((value_or_quick_check == "M"sv))
  330. quick_check = QuickCheck::Maybe;
  331. else
  332. value = parse_code_point_list(value_or_quick_check);
  333. }
  334. auto& normalizations = unicode_data.normalization_props.ensure(property);
  335. normalizations.append({ code_point_range, move(value), quick_check });
  336. auto& prop_list = unicode_data.prop_list.ensure(property);
  337. prop_list.append(move(code_point_range));
  338. }
  339. return {};
  340. }
  341. static void add_canonical_code_point_name(CodePointRange range, StringView name, UnicodeData& unicode_data)
  342. {
  343. // https://www.unicode.org/versions/Unicode14.0.0/ch04.pdf#G142981
  344. // FIXME: Implement the NR1 rules for Hangul syllables.
  345. // These code point ranges are the NR2 set of name replacements defined by Table 4-8.
  346. constexpr Array<CodePointName, 15> s_ideographic_replacements { {
  347. { { 0x3400, 0x4DBF }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  348. { { 0x4E00, 0x9FFC }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  349. { { 0xF900, 0xFA6D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  350. { { 0xFA70, 0xFAD9 }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  351. { { 0x17000, 0x187F7 }, "TANGUT IDEOGRAPH-{:X}"sv },
  352. { { 0x18B00, 0x18CD5 }, "KHITAN SMALL SCRIPT CHARACTER-{:X}"sv },
  353. { { 0x18D00, 0x18D08 }, "TANGUT IDEOGRAPH-{:X}"sv },
  354. { { 0x1B170, 0x1B2FB }, "NUSHU CHARACTER-{:X}"sv },
  355. { { 0x20000, 0x2A6DD }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  356. { { 0x2A700, 0x2B734 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  357. { { 0x2B740, 0x2B81D }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  358. { { 0x2B820, 0x2CEA1 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  359. { { 0x2CEB0, 0x2EBE0 }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  360. { { 0x2F800, 0x2FA1D }, "CJK COMPATIBILITY IDEOGRAPH-{:X}"sv },
  361. { { 0x30000, 0x3134A }, "CJK UNIFIED IDEOGRAPH-{:X}"sv },
  362. } };
  363. auto it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  364. [&](auto const& replacement) {
  365. return replacement.code_point_range.first == range.first;
  366. });
  367. if (it != s_ideographic_replacements.end()) {
  368. unicode_data.code_point_display_names.append(*it);
  369. return;
  370. }
  371. it = find_if(s_ideographic_replacements.begin(), s_ideographic_replacements.end(),
  372. [&](auto const& replacement) {
  373. return (replacement.code_point_range.first <= range.first) && (range.first <= replacement.code_point_range.last);
  374. });
  375. if (it != s_ideographic_replacements.end()) {
  376. // Drop code points that will have been captured by a range defined by the ideographic replacements.
  377. return;
  378. }
  379. if (auto alias = unicode_data.code_point_display_name_aliases.get(range.first); alias.has_value()) {
  380. // NR4 states that control code points have a null string as their name. Our implementation
  381. // uses the control code's alias as its display name.
  382. unicode_data.code_point_display_names.append({ range, *alias });
  383. return;
  384. }
  385. unicode_data.code_point_display_names.append({ range, name });
  386. }
  387. static ErrorOr<void> parse_unicode_data(Core::Stream::BufferedFile& file, UnicodeData& unicode_data)
  388. {
  389. Optional<u32> code_point_range_start;
  390. auto& assigned_code_points = unicode_data.prop_list.find("Assigned"sv)->value;
  391. Optional<u32> assigned_code_point_range_start = 0;
  392. u32 previous_code_point = 0;
  393. Array<u8, 1024> buffer;
  394. while (TRY(file.can_read_line())) {
  395. auto nread = TRY(file.read_line(buffer));
  396. StringView line { buffer.data(), nread };
  397. if (line.is_empty())
  398. continue;
  399. auto segments = line.split_view(';', true);
  400. VERIFY(segments.size() == 15);
  401. CodePointData data {};
  402. data.code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[0]).value();
  403. data.name = segments[1];
  404. data.canonical_combining_class = AK::StringUtils::convert_to_uint<u8>(segments[3]).value();
  405. data.bidi_class = segments[4];
  406. data.decomposition_type = segments[5];
  407. data.numeric_value_decimal = AK::StringUtils::convert_to_int<i8>(segments[6]);
  408. data.numeric_value_digit = AK::StringUtils::convert_to_int<i8>(segments[7]);
  409. data.numeric_value_numeric = AK::StringUtils::convert_to_int<i8>(segments[8]);
  410. data.bidi_mirrored = segments[9] == "Y"sv;
  411. data.unicode_1_name = segments[10];
  412. data.iso_comment = segments[11];
  413. data.simple_uppercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[12]);
  414. data.simple_lowercase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[13]);
  415. data.simple_titlecase_mapping = AK::StringUtils::convert_to_uint_from_hex<u32>(segments[14]);
  416. if (auto abbreviation = unicode_data.code_point_abbreviations.get(data.code_point); abbreviation.has_value())
  417. data.abbreviation = *abbreviation;
  418. if (!assigned_code_point_range_start.has_value())
  419. assigned_code_point_range_start = data.code_point;
  420. if (data.name.starts_with("<"sv) && data.name.ends_with(", First>")) {
  421. VERIFY(!code_point_range_start.has_value() && assigned_code_point_range_start.has_value());
  422. code_point_range_start = data.code_point;
  423. data.name = data.name.substring(1, data.name.length() - 9);
  424. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  425. assigned_code_point_range_start.clear();
  426. } else if (data.name.starts_with("<"sv) && data.name.ends_with(", Last>")) {
  427. VERIFY(code_point_range_start.has_value());
  428. CodePointRange code_point_range { *code_point_range_start, data.code_point };
  429. assigned_code_points.append(code_point_range);
  430. data.name = data.name.substring(1, data.name.length() - 8);
  431. code_point_range_start.clear();
  432. add_canonical_code_point_name(code_point_range, data.name, unicode_data);
  433. } else {
  434. add_canonical_code_point_name({ data.code_point, data.code_point }, data.name, unicode_data);
  435. if ((data.code_point > 0) && (data.code_point - previous_code_point) != 1) {
  436. VERIFY(assigned_code_point_range_start.has_value());
  437. assigned_code_points.append({ *assigned_code_point_range_start, previous_code_point });
  438. assigned_code_point_range_start = data.code_point;
  439. }
  440. }
  441. bool has_special_casing { false };
  442. for (auto const& casing : unicode_data.special_casing) {
  443. if (casing.code_point == data.code_point) {
  444. data.special_casing_indices.append(casing.index);
  445. has_special_casing = true;
  446. }
  447. }
  448. unicode_data.code_points_with_non_zero_combining_class += data.canonical_combining_class != 0;
  449. unicode_data.simple_uppercase_mapping_size += data.simple_uppercase_mapping.has_value();
  450. unicode_data.simple_lowercase_mapping_size += data.simple_lowercase_mapping.has_value();
  451. unicode_data.code_points_with_special_casing += has_special_casing;
  452. unicode_data.largest_special_casing_size = max(unicode_data.largest_special_casing_size, data.special_casing_indices.size());
  453. previous_code_point = data.code_point;
  454. unicode_data.code_point_data.append(move(data));
  455. }
  456. return {};
  457. }
  458. static ErrorOr<void> generate_unicode_data_header(Core::Stream::BufferedFile& file, UnicodeData& unicode_data)
  459. {
  460. StringBuilder builder;
  461. SourceGenerator generator { builder };
  462. generator.set("casing_transform_size", String::number(unicode_data.largest_casing_transform_size));
  463. auto generate_enum = [&](StringView name, StringView default_, Vector<String> values, Vector<Alias> aliases = {}) {
  464. quick_sort(values);
  465. quick_sort(aliases, [](auto& alias1, auto& alias2) { return alias1.alias < alias2.alias; });
  466. generator.set("name", name);
  467. generator.set("underlying", String::formatted("{}UnderlyingType", name));
  468. generator.append(R"~~~(
  469. using @underlying@ = u8;
  470. enum class @name@ : @underlying@ {)~~~");
  471. if (!default_.is_empty()) {
  472. generator.set("default", default_);
  473. generator.append(R"~~~(
  474. @default@,)~~~");
  475. }
  476. for (auto const& value : values) {
  477. generator.set("value", value);
  478. generator.append(R"~~~(
  479. @value@,)~~~");
  480. }
  481. for (auto const& alias : aliases) {
  482. generator.set("alias", alias.alias);
  483. generator.set("value", alias.name);
  484. generator.append(R"~~~(
  485. @alias@ = @value@,)~~~");
  486. }
  487. generator.append(R"~~~(
  488. };
  489. )~~~");
  490. };
  491. generator.append(R"~~~(
  492. #pragma once
  493. #include <AK/Types.h>
  494. #include <LibUnicode/Forward.h>
  495. #include <LibUnicode/UnicodeLocale.h>
  496. namespace Unicode {
  497. )~~~");
  498. generate_enum("Condition"sv, "None"sv, move(unicode_data.conditions));
  499. generate_enum("GeneralCategory"sv, {}, unicode_data.general_categories.keys(), unicode_data.general_category_aliases);
  500. generate_enum("Property"sv, {}, unicode_data.prop_list.keys(), unicode_data.prop_aliases);
  501. generate_enum("Script"sv, {}, unicode_data.script_list.keys(), unicode_data.script_aliases);
  502. generate_enum("GraphemeBreakProperty"sv, {}, unicode_data.grapheme_break_props.keys());
  503. generate_enum("WordBreakProperty"sv, {}, unicode_data.word_break_props.keys());
  504. generate_enum("SentenceBreakProperty"sv, {}, unicode_data.sentence_break_props.keys());
  505. generator.append(R"~~~(
  506. struct SpecialCasing {
  507. u32 code_point { 0 };
  508. u32 lowercase_mapping[@casing_transform_size@];
  509. u32 lowercase_mapping_size { 0 };
  510. u32 uppercase_mapping[@casing_transform_size@];
  511. u32 uppercase_mapping_size { 0 };
  512. u32 titlecase_mapping[@casing_transform_size@];
  513. u32 titlecase_mapping_size { 0 };
  514. Locale locale { Locale::None };
  515. Condition condition { Condition::None };
  516. };
  517. }
  518. )~~~");
  519. TRY(file.write(generator.as_string_view().bytes()));
  520. return {};
  521. }
  522. static ErrorOr<void> generate_unicode_data_implementation(Core::Stream::BufferedFile& file, UnicodeData const& unicode_data)
  523. {
  524. StringBuilder builder;
  525. SourceGenerator generator { builder };
  526. generator.set("largest_special_casing_size", String::number(unicode_data.largest_special_casing_size));
  527. generator.set("special_casing_size", String::number(unicode_data.special_casing.size()));
  528. generator.append(R"~~~(
  529. #include <AK/Array.h>
  530. #include <AK/BinarySearch.h>
  531. #include <AK/CharacterTypes.h>
  532. #include <AK/Optional.h>
  533. #include <AK/Span.h>
  534. #include <AK/String.h>
  535. #include <AK/StringView.h>
  536. #include <LibUnicode/CharacterTypes.h>
  537. #include <LibUnicode/UnicodeData.h>
  538. namespace Unicode {
  539. )~~~");
  540. auto append_list_and_size = [&](auto const& list, StringView format) {
  541. if (list.is_empty()) {
  542. generator.append(", {}, 0");
  543. return;
  544. }
  545. bool first = true;
  546. generator.append(", {");
  547. for (auto const& item : list) {
  548. generator.append(first ? " " : ", ");
  549. generator.append(String::formatted(format, item));
  550. first = false;
  551. }
  552. generator.append(String::formatted(" }}, {}", list.size()));
  553. };
  554. generator.append(R"~~~(
  555. static constexpr Array<SpecialCasing, @special_casing_size@> s_special_casing { {)~~~");
  556. for (auto const& casing : unicode_data.special_casing) {
  557. generator.set("code_point", String::formatted("{:#x}", casing.code_point));
  558. generator.append(R"~~~(
  559. { @code_point@)~~~");
  560. constexpr auto format = "0x{:x}"sv;
  561. append_list_and_size(casing.lowercase_mapping, format);
  562. append_list_and_size(casing.uppercase_mapping, format);
  563. append_list_and_size(casing.titlecase_mapping, format);
  564. generator.set("locale", casing.locale.is_empty() ? "None" : casing.locale);
  565. generator.append(", Locale::@locale@");
  566. generator.set("condition", casing.condition.is_empty() ? "None" : casing.condition);
  567. generator.append(", Condition::@condition@");
  568. generator.append(" },");
  569. }
  570. generator.append(R"~~~(
  571. } };
  572. struct CodePointMapping {
  573. u32 code_point { 0 };
  574. u32 mapping { 0 };
  575. };
  576. struct SpecialCaseMapping {
  577. u32 code_point { 0 };
  578. Array<SpecialCasing const*, @largest_special_casing_size@> special_casing {};
  579. u32 special_casing_size { 0 };
  580. };
  581. struct CodePointAbbreviation {
  582. u32 code_point { 0 };
  583. StringView abbreviation {};
  584. };
  585. template<typename MappingType>
  586. struct CodePointComparator {
  587. constexpr int operator()(u32 code_point, MappingType const& mapping)
  588. {
  589. return code_point - mapping.code_point;
  590. }
  591. };
  592. )~~~");
  593. auto append_code_point_mappings = [&](StringView name, StringView mapping_type, u32 size, auto mapping_getter) {
  594. generator.set("name", name);
  595. generator.set("mapping_type", mapping_type);
  596. generator.set("size", String::number(size));
  597. generator.append(R"~~~(
  598. static constexpr Array<@mapping_type@, @size@> s_@name@_mappings { {
  599. )~~~");
  600. constexpr size_t max_mappings_per_row = 20;
  601. size_t mappings_in_current_row = 0;
  602. for (auto const& data : unicode_data.code_point_data) {
  603. auto mapping = mapping_getter(data);
  604. if constexpr (requires { mapping.has_value(); }) {
  605. if (!mapping.has_value())
  606. continue;
  607. } else {
  608. if (mapping.is_empty())
  609. continue;
  610. }
  611. if (mappings_in_current_row++ > 0)
  612. generator.append(" ");
  613. generator.set("code_point", String::formatted("{:#x}", data.code_point));
  614. generator.append("{ @code_point@");
  615. if constexpr (IsSame<decltype(mapping), Optional<u32>>) {
  616. generator.set("mapping", String::formatted("{:#x}", *mapping));
  617. generator.append(", @mapping@ },");
  618. } else if constexpr (IsSame<decltype(mapping), Optional<StringView>>) {
  619. generator.set("mapping", String::formatted("{}", *mapping));
  620. generator.append(", \"@mapping@\"sv },");
  621. } else {
  622. append_list_and_size(data.special_casing_indices, "&s_special_casing[{}]"sv);
  623. generator.append(" },");
  624. }
  625. if (mappings_in_current_row == max_mappings_per_row) {
  626. mappings_in_current_row = 0;
  627. generator.append("\n ");
  628. }
  629. }
  630. generator.append(R"~~~(
  631. } };
  632. )~~~");
  633. };
  634. append_code_point_mappings("combining_class"sv, "CodePointMapping"sv, unicode_data.code_points_with_non_zero_combining_class,
  635. [](auto const& data) -> Optional<u32> {
  636. if (data.canonical_combining_class == 0)
  637. return {};
  638. return data.canonical_combining_class;
  639. });
  640. append_code_point_mappings("uppercase"sv, "CodePointMapping"sv, unicode_data.simple_uppercase_mapping_size, [](auto const& data) { return data.simple_uppercase_mapping; });
  641. append_code_point_mappings("lowercase"sv, "CodePointMapping"sv, unicode_data.simple_lowercase_mapping_size, [](auto const& data) { return data.simple_lowercase_mapping; });
  642. append_code_point_mappings("special_case"sv, "SpecialCaseMapping"sv, unicode_data.code_points_with_special_casing, [](auto const& data) { return data.special_casing_indices; });
  643. append_code_point_mappings("abbreviation"sv, "CodePointAbbreviation"sv, unicode_data.code_point_abbreviations.size(), [](auto const& data) { return data.abbreviation; });
  644. generator.append(R"~~~(
  645. struct CodePointRange {
  646. u32 first { 0 };
  647. u32 last { 0 };
  648. };
  649. struct CodePointRangeComparator {
  650. constexpr int operator()(u32 code_point, CodePointRange const& range)
  651. {
  652. return (code_point > range.last) - (code_point < range.first);
  653. }
  654. };
  655. )~~~");
  656. auto append_code_point_range_list = [&](String name, Vector<CodePointRange> const& ranges) {
  657. generator.set("name", name);
  658. generator.set("size", String::number(ranges.size()));
  659. generator.append(R"~~~(
  660. static constexpr Array<CodePointRange, @size@> @name@ { {
  661. )~~~");
  662. constexpr size_t max_ranges_per_row = 20;
  663. size_t ranges_in_current_row = 0;
  664. for (auto const& range : ranges) {
  665. if (ranges_in_current_row++ > 0)
  666. generator.append(" ");
  667. generator.set("first", String::formatted("{:#x}", range.first));
  668. generator.set("last", String::formatted("{:#x}", range.last));
  669. generator.append("{ @first@, @last@ },");
  670. if (ranges_in_current_row == max_ranges_per_row) {
  671. ranges_in_current_row = 0;
  672. generator.append("\n ");
  673. }
  674. }
  675. generator.append(R"~~~(
  676. } };
  677. )~~~");
  678. };
  679. auto append_prop_list = [&](StringView collection_name, StringView property_format, PropList const& property_list) {
  680. for (auto const& property : property_list) {
  681. auto name = String::formatted(property_format, property.key);
  682. append_code_point_range_list(move(name), property.value);
  683. }
  684. auto property_names = property_list.keys();
  685. quick_sort(property_names);
  686. generator.set("name", collection_name);
  687. generator.set("size", String::number(property_names.size()));
  688. generator.append(R"~~~(
  689. static constexpr Array<Span<CodePointRange const>, @size@> @name@ { {)~~~");
  690. for (auto const& property_name : property_names) {
  691. generator.set("name", String::formatted(property_format, property_name));
  692. generator.append(R"~~~(
  693. @name@.span(),)~~~");
  694. }
  695. generator.append(R"~~~(
  696. } };
  697. )~~~");
  698. };
  699. append_prop_list("s_general_categories"sv, "s_general_category_{}"sv, unicode_data.general_categories);
  700. append_prop_list("s_properties"sv, "s_property_{}"sv, unicode_data.prop_list);
  701. append_prop_list("s_scripts"sv, "s_script_{}"sv, unicode_data.script_list);
  702. append_prop_list("s_script_extensions"sv, "s_script_extension_{}"sv, unicode_data.script_extensions);
  703. append_prop_list("s_grapheme_break_properties"sv, "s_grapheme_break_property_{}"sv, unicode_data.grapheme_break_props);
  704. append_prop_list("s_word_break_properties"sv, "s_word_break_property_{}"sv, unicode_data.word_break_props);
  705. append_prop_list("s_sentence_break_properties"sv, "s_sentence_break_property_{}"sv, unicode_data.sentence_break_props);
  706. generator.append(R"~~~(
  707. struct CodePointName {
  708. CodePointRange code_point_range {};
  709. StringView display_name;
  710. };
  711. struct CodePointNameComparator : public CodePointRangeComparator {
  712. constexpr int operator()(u32 code_point, CodePointName const& name)
  713. {
  714. return CodePointRangeComparator::operator()(code_point, name.code_point_range);
  715. }
  716. };
  717. )~~~");
  718. generator.set("code_point_display_names_size", String::number(unicode_data.code_point_display_names.size()));
  719. generator.append(R"~~~(
  720. static constexpr Array<CodePointName, @code_point_display_names_size@> s_code_point_display_names { {
  721. )~~~");
  722. for (auto const& code_point_name : unicode_data.code_point_display_names) {
  723. generator.set("first", String::formatted("{:#x}", code_point_name.code_point_range.first));
  724. generator.set("last", String::formatted("{:#x}", code_point_name.code_point_range.last));
  725. generator.set("name", code_point_name.name);
  726. generator.append(R"~~~( { { @first@, @last@ }, "@name@"sv },
  727. )~~~");
  728. }
  729. generator.append(R"~~~(} };
  730. )~~~");
  731. generator.append(R"~~~(
  732. Optional<String> code_point_display_name(u32 code_point)
  733. {
  734. if (auto const* entry = binary_search(s_code_point_display_names, code_point, nullptr, CodePointNameComparator {})) {
  735. if (entry->display_name.ends_with("{:X}"sv))
  736. return String::formatted(entry->display_name, code_point);
  737. return entry->display_name;
  738. }
  739. return {};
  740. }
  741. )~~~");
  742. auto append_code_point_mapping_search = [&](StringView method, StringView mappings, StringView fallback) {
  743. generator.set("method", method);
  744. generator.set("mappings", mappings);
  745. generator.set("fallback", fallback);
  746. generator.append(R"~~~(
  747. u32 @method@(u32 code_point)
  748. {
  749. auto const* mapping = binary_search(@mappings@, code_point, nullptr, CodePointComparator<CodePointMapping> {});
  750. return mapping ? mapping->mapping : @fallback@;
  751. }
  752. )~~~");
  753. };
  754. append_code_point_mapping_search("canonical_combining_class"sv, "s_combining_class_mappings"sv, "0"sv);
  755. append_code_point_mapping_search("to_unicode_uppercase"sv, "s_uppercase_mappings"sv, "code_point"sv);
  756. append_code_point_mapping_search("to_unicode_lowercase"sv, "s_lowercase_mappings"sv, "code_point"sv);
  757. generator.append(R"~~~(
  758. Span<SpecialCasing const* const> special_case_mapping(u32 code_point)
  759. {
  760. auto const* mapping = binary_search(s_special_case_mappings, code_point, nullptr, CodePointComparator<SpecialCaseMapping> {});
  761. if (mapping == nullptr)
  762. return {};
  763. return mapping->special_casing.span().slice(0, mapping->special_casing_size);
  764. }
  765. Optional<StringView> code_point_abbreviation(u32 code_point)
  766. {
  767. auto const* mapping = binary_search(s_abbreviation_mappings, code_point, nullptr, CodePointComparator<CodePointAbbreviation> {});
  768. if (mapping == nullptr)
  769. return {};
  770. return mapping->abbreviation;
  771. }
  772. )~~~");
  773. auto append_prop_search = [&](StringView enum_title, StringView enum_snake, StringView collection_name) {
  774. generator.set("enum_title", enum_title);
  775. generator.set("enum_snake", enum_snake);
  776. generator.set("collection_name", collection_name);
  777. generator.append(R"~~~(
  778. bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
  779. {
  780. auto index = static_cast<@enum_title@UnderlyingType>(@enum_snake@);
  781. auto const& ranges = @collection_name@.at(index);
  782. auto const* range = binary_search(ranges, code_point, nullptr, CodePointRangeComparator {});
  783. return range != nullptr;
  784. }
  785. )~~~");
  786. };
  787. auto append_from_string = [&](StringView enum_title, StringView enum_snake, PropList const& prop_list, Vector<Alias> const& aliases) {
  788. HashValueMap<StringView> hashes;
  789. hashes.ensure_capacity(prop_list.size() + aliases.size());
  790. for (auto const& prop : prop_list)
  791. hashes.set(prop.key.hash(), prop.key);
  792. for (auto const& alias : aliases)
  793. hashes.set(alias.alias.hash(), alias.alias);
  794. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
  795. };
  796. append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv);
  797. append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases);
  798. append_prop_search("Property"sv, "property"sv, "s_properties"sv);
  799. append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases);
  800. append_prop_search("Script"sv, "script"sv, "s_scripts"sv);
  801. append_prop_search("Script"sv, "script_extension"sv, "s_script_extensions"sv);
  802. append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases);
  803. append_prop_search("GraphemeBreakProperty"sv, "grapheme_break_property"sv, "s_grapheme_break_properties"sv);
  804. append_prop_search("WordBreakProperty"sv, "word_break_property"sv, "s_word_break_properties"sv);
  805. append_prop_search("SentenceBreakProperty"sv, "sentence_break_property"sv, "s_sentence_break_properties"sv);
  806. generator.append(R"~~~(
  807. }
  808. )~~~");
  809. TRY(file.write(generator.as_string_view().bytes()));
  810. return {};
  811. }
  812. static Vector<u32> flatten_code_point_ranges(Vector<CodePointRange> const& code_points)
  813. {
  814. Vector<u32> flattened;
  815. for (auto const& range : code_points) {
  816. flattened.grow_capacity(range.last - range.first);
  817. for (u32 code_point = range.first; code_point <= range.last; ++code_point)
  818. flattened.append(code_point);
  819. }
  820. return flattened;
  821. }
  822. static Vector<CodePointRange> form_code_point_ranges(Vector<u32> code_points)
  823. {
  824. Vector<CodePointRange> ranges;
  825. u32 range_start = code_points[0];
  826. u32 range_end = range_start;
  827. for (size_t i = 1; i < code_points.size(); ++i) {
  828. u32 code_point = code_points[i];
  829. if ((code_point - range_end) == 1) {
  830. range_end = code_point;
  831. } else {
  832. ranges.append({ range_start, range_end });
  833. range_start = code_point;
  834. range_end = code_point;
  835. }
  836. }
  837. ranges.append({ range_start, range_end });
  838. return ranges;
  839. }
  840. static void sort_and_merge_code_point_ranges(Vector<CodePointRange>& code_points)
  841. {
  842. quick_sort(code_points, [](auto const& range1, auto const& range2) {
  843. return range1.first < range2.first;
  844. });
  845. for (size_t i = 0; i < code_points.size() - 1;) {
  846. if (code_points[i].last >= code_points[i + 1].first) {
  847. code_points[i].last = max(code_points[i].last, code_points[i + 1].last);
  848. code_points.remove(i + 1);
  849. } else {
  850. ++i;
  851. }
  852. }
  853. auto all_code_points = flatten_code_point_ranges(code_points);
  854. code_points = form_code_point_ranges(all_code_points);
  855. }
  856. static void populate_general_category_unions(PropList& general_categories)
  857. {
  858. // The Unicode standard defines General Category values which are not in any UCD file. These
  859. // values are simply unions of other values.
  860. // https://www.unicode.org/reports/tr44/#GC_Values_Table
  861. auto populate_union = [&](auto alias, auto categories) {
  862. auto& code_points = general_categories.ensure(alias);
  863. for (auto const& category : categories)
  864. code_points.extend(general_categories.find(category)->value);
  865. sort_and_merge_code_point_ranges(code_points);
  866. };
  867. populate_union("LC"sv, Array { "Ll"sv, "Lu"sv, "Lt"sv });
  868. populate_union("L"sv, Array { "Lu"sv, "Ll"sv, "Lt"sv, "Lm"sv, "Lo"sv });
  869. populate_union("M"sv, Array { "Mn"sv, "Mc"sv, "Me"sv });
  870. populate_union("N"sv, Array { "Nd"sv, "Nl"sv, "No"sv });
  871. populate_union("P"sv, Array { "Pc"sv, "Pd"sv, "Ps"sv, "Pe"sv, "Pi"sv, "Pf"sv, "Po"sv });
  872. populate_union("S"sv, Array { "Sm"sv, "Sc"sv, "Sk"sv, "So"sv });
  873. populate_union("Z"sv, Array { "Zs"sv, "Zl"sv, "Zp"sv });
  874. populate_union("C"sv, Array { "Cc"sv, "Cf"sv, "Cs"sv, "Co"sv, "Cn"sv });
  875. }
  876. static void normalize_script_extensions(PropList& script_extensions, PropList const& script_list, Vector<Alias> const& script_aliases)
  877. {
  878. // The ScriptExtensions UCD file lays out its code point ranges rather uniquely compared to
  879. // other files. The Script listed on each line may either be a full Script string or an aliased
  880. // abbreviation. Further, the extensions may or may not include the base Script list. Normalize
  881. // the extensions here to be keyed by the full Script name and always include the base list.
  882. auto extensions = move(script_extensions);
  883. script_extensions = script_list;
  884. for (auto const& extension : extensions) {
  885. auto it = find_if(script_aliases.begin(), script_aliases.end(), [&](auto const& alias) { return extension.key == alias.alias; });
  886. auto const& key = (it == script_aliases.end()) ? extension.key : it->name;
  887. auto& code_points = script_extensions.find(key)->value;
  888. code_points.extend(extension.value);
  889. sort_and_merge_code_point_ranges(code_points);
  890. }
  891. // Lastly, the Common and Inherited script extensions are special. They must not contain any
  892. // code points which appear in other script extensions. The ScriptExtensions UCD file does not
  893. // list these extensions, therefore this peculiarity must be handled programmatically.
  894. // https://www.unicode.org/reports/tr24/#Assignment_ScriptX_Values
  895. auto code_point_has_other_extension = [&](StringView key, u32 code_point) {
  896. for (auto const& extension : extensions) {
  897. if (extension.key == key)
  898. continue;
  899. if (any_of(extension.value, [&](auto const& r) { return (r.first <= code_point) && (code_point <= r.last); }))
  900. return true;
  901. }
  902. return false;
  903. };
  904. auto get_code_points_without_other_extensions = [&](StringView key) {
  905. auto code_points = flatten_code_point_ranges(script_list.find(key)->value);
  906. code_points.remove_all_matching([&](u32 c) { return code_point_has_other_extension(key, c); });
  907. return code_points;
  908. };
  909. auto common_code_points = get_code_points_without_other_extensions("Common"sv);
  910. script_extensions.set("Common"sv, form_code_point_ranges(common_code_points));
  911. auto inherited_code_points = get_code_points_without_other_extensions("Inherited"sv);
  912. script_extensions.set("Inherited"sv, form_code_point_ranges(inherited_code_points));
  913. }
  914. ErrorOr<int> serenity_main(Main::Arguments arguments)
  915. {
  916. StringView generated_header_path;
  917. StringView generated_implementation_path;
  918. StringView unicode_data_path;
  919. StringView special_casing_path;
  920. StringView derived_general_category_path;
  921. StringView prop_list_path;
  922. StringView derived_core_prop_path;
  923. StringView derived_binary_prop_path;
  924. StringView prop_alias_path;
  925. StringView prop_value_alias_path;
  926. StringView name_alias_path;
  927. StringView scripts_path;
  928. StringView script_extensions_path;
  929. StringView emoji_data_path;
  930. StringView normalization_path;
  931. StringView grapheme_break_path;
  932. StringView word_break_path;
  933. StringView sentence_break_path;
  934. Core::ArgsParser args_parser;
  935. args_parser.add_option(generated_header_path, "Path to the Unicode Data header file to generate", "generated-header-path", 'h', "generated-header-path");
  936. args_parser.add_option(generated_implementation_path, "Path to the Unicode Data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  937. args_parser.add_option(unicode_data_path, "Path to UnicodeData.txt file", "unicode-data-path", 'u', "unicode-data-path");
  938. args_parser.add_option(special_casing_path, "Path to SpecialCasing.txt file", "special-casing-path", 's', "special-casing-path");
  939. args_parser.add_option(derived_general_category_path, "Path to DerivedGeneralCategory.txt file", "derived-general-category-path", 'g', "derived-general-category-path");
  940. args_parser.add_option(prop_list_path, "Path to PropList.txt file", "prop-list-path", 'p', "prop-list-path");
  941. args_parser.add_option(derived_core_prop_path, "Path to DerivedCoreProperties.txt file", "derived-core-prop-path", 'd', "derived-core-prop-path");
  942. args_parser.add_option(derived_binary_prop_path, "Path to DerivedBinaryProperties.txt file", "derived-binary-prop-path", 'b', "derived-binary-prop-path");
  943. args_parser.add_option(prop_alias_path, "Path to PropertyAliases.txt file", "prop-alias-path", 'a', "prop-alias-path");
  944. args_parser.add_option(prop_value_alias_path, "Path to PropertyValueAliases.txt file", "prop-value-alias-path", 'v', "prop-value-alias-path");
  945. args_parser.add_option(name_alias_path, "Path to NameAliases.txt file", "name-alias-path", 'm', "name-alias-path");
  946. args_parser.add_option(scripts_path, "Path to Scripts.txt file", "scripts-path", 'r', "scripts-path");
  947. args_parser.add_option(script_extensions_path, "Path to ScriptExtensions.txt file", "script-extensions-path", 'x', "script-extensions-path");
  948. args_parser.add_option(emoji_data_path, "Path to emoji-data.txt file", "emoji-data-path", 'e', "emoji-data-path");
  949. args_parser.add_option(normalization_path, "Path to DerivedNormalizationProps.txt file", "normalization-path", 'n', "normalization-path");
  950. args_parser.add_option(grapheme_break_path, "Path to GraphemeBreakProperty.txt file", "grapheme-break-path", 'f', "grapheme-break-path");
  951. args_parser.add_option(word_break_path, "Path to WordBreakProperty.txt file", "word-break-path", 'w', "word-break-path");
  952. args_parser.add_option(sentence_break_path, "Path to SentenceBreakProperty.txt file", "sentence-break-path", 'i', "sentence-break-path");
  953. args_parser.parse(arguments);
  954. auto generated_header_file = TRY(open_file(generated_header_path, Core::Stream::OpenMode::Write));
  955. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::Stream::OpenMode::Write));
  956. auto unicode_data_file = TRY(open_file(unicode_data_path, Core::Stream::OpenMode::Read));
  957. auto derived_general_category_file = TRY(open_file(derived_general_category_path, Core::Stream::OpenMode::Read));
  958. auto special_casing_file = TRY(open_file(special_casing_path, Core::Stream::OpenMode::Read));
  959. auto prop_list_file = TRY(open_file(prop_list_path, Core::Stream::OpenMode::Read));
  960. auto derived_core_prop_file = TRY(open_file(derived_core_prop_path, Core::Stream::OpenMode::Read));
  961. auto derived_binary_prop_file = TRY(open_file(derived_binary_prop_path, Core::Stream::OpenMode::Read));
  962. auto prop_alias_file = TRY(open_file(prop_alias_path, Core::Stream::OpenMode::Read));
  963. auto prop_value_alias_file = TRY(open_file(prop_value_alias_path, Core::Stream::OpenMode::Read));
  964. auto name_alias_file = TRY(open_file(name_alias_path, Core::Stream::OpenMode::Read));
  965. auto scripts_file = TRY(open_file(scripts_path, Core::Stream::OpenMode::Read));
  966. auto script_extensions_file = TRY(open_file(script_extensions_path, Core::Stream::OpenMode::Read));
  967. auto emoji_data_file = TRY(open_file(emoji_data_path, Core::Stream::OpenMode::Read));
  968. auto normalization_file = TRY(open_file(normalization_path, Core::Stream::OpenMode::Read));
  969. auto grapheme_break_file = TRY(open_file(grapheme_break_path, Core::Stream::OpenMode::Read));
  970. auto word_break_file = TRY(open_file(word_break_path, Core::Stream::OpenMode::Read));
  971. auto sentence_break_file = TRY(open_file(sentence_break_path, Core::Stream::OpenMode::Read));
  972. UnicodeData unicode_data {};
  973. TRY(parse_special_casing(*special_casing_file, unicode_data));
  974. TRY(parse_prop_list(*derived_general_category_file, unicode_data.general_categories));
  975. TRY(parse_prop_list(*prop_list_file, unicode_data.prop_list));
  976. TRY(parse_prop_list(*derived_core_prop_file, unicode_data.prop_list));
  977. TRY(parse_prop_list(*derived_binary_prop_file, unicode_data.prop_list));
  978. TRY(parse_prop_list(*emoji_data_file, unicode_data.prop_list));
  979. TRY(parse_normalization_props(*normalization_file, unicode_data));
  980. TRY(parse_alias_list(*prop_alias_file, unicode_data.prop_list, unicode_data.prop_aliases));
  981. TRY(parse_prop_list(*scripts_file, unicode_data.script_list));
  982. TRY(parse_prop_list(*script_extensions_file, unicode_data.script_extensions, true));
  983. TRY(parse_name_aliases(*name_alias_file, unicode_data));
  984. TRY(parse_prop_list(*grapheme_break_file, unicode_data.grapheme_break_props));
  985. TRY(parse_prop_list(*word_break_file, unicode_data.word_break_props));
  986. TRY(parse_prop_list(*sentence_break_file, unicode_data.sentence_break_props));
  987. populate_general_category_unions(unicode_data.general_categories);
  988. TRY(parse_unicode_data(*unicode_data_file, unicode_data));
  989. TRY(parse_value_alias_list(*prop_value_alias_file, "gc"sv, unicode_data.general_categories.keys(), unicode_data.general_category_aliases));
  990. TRY(parse_value_alias_list(*prop_value_alias_file, "sc"sv, unicode_data.script_list.keys(), unicode_data.script_aliases, false));
  991. normalize_script_extensions(unicode_data.script_extensions, unicode_data.script_list, unicode_data.script_aliases);
  992. TRY(generate_unicode_data_header(*generated_header_file, unicode_data));
  993. TRY(generate_unicode_data_implementation(*generated_implementation_file, unicode_data));
  994. return 0;
  995. }