GenerateUnicodeNumberFormat.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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/Format.h>
  12. #include <AK/HashFunctions.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/JsonObject.h>
  15. #include <AK/JsonParser.h>
  16. #include <AK/JsonValue.h>
  17. #include <AK/LexicalPath.h>
  18. #include <AK/QuickSort.h>
  19. #include <AK/SourceGenerator.h>
  20. #include <AK/String.h>
  21. #include <AK/StringBuilder.h>
  22. #include <AK/Traits.h>
  23. #include <AK/Utf8View.h>
  24. #include <LibCore/ArgsParser.h>
  25. #include <LibCore/DirIterator.h>
  26. #include <LibCore/File.h>
  27. #include <LibCore/Stream.h>
  28. #include <LibUnicode/Locale.h>
  29. #include <LibUnicode/NumberFormat.h>
  30. #include <math.h>
  31. using StringIndexType = u16;
  32. constexpr auto s_string_index_type = "u16"sv;
  33. using NumberFormatIndexType = u16;
  34. constexpr auto s_number_format_index_type = "u16"sv;
  35. using NumberFormatListIndexType = u16;
  36. constexpr auto s_number_format_list_index_type = "u16"sv;
  37. using NumericSymbolListIndexType = u8;
  38. constexpr auto s_numeric_symbol_list_index_type = "u8"sv;
  39. using NumberSystemIndexType = u8;
  40. constexpr auto s_number_system_index_type = "u8"sv;
  41. using UnitIndexType = u16;
  42. constexpr auto s_unit_index_type = "u16"sv;
  43. enum class NumberFormatType {
  44. Standard,
  45. Compact,
  46. };
  47. struct NumberFormat : public Unicode::NumberFormat {
  48. using Base = Unicode::NumberFormat;
  49. static Base::Plurality plurality_from_string(StringView plurality)
  50. {
  51. if (plurality == "other"sv)
  52. return Base::Plurality::Other;
  53. if (plurality == "1"sv)
  54. return Base::Plurality::Single;
  55. if (plurality == "zero"sv)
  56. return Base::Plurality::Zero;
  57. if (plurality == "one"sv)
  58. return Base::Plurality::One;
  59. if (plurality == "two"sv)
  60. return Base::Plurality::Two;
  61. if (plurality == "few"sv)
  62. return Base::Plurality::Few;
  63. if (plurality == "many"sv)
  64. return Base::Plurality::Many;
  65. VERIFY_NOT_REACHED();
  66. }
  67. unsigned hash() const
  68. {
  69. auto hash = pair_int_hash(magnitude, exponent);
  70. hash = pair_int_hash(hash, static_cast<u8>(plurality));
  71. hash = pair_int_hash(hash, zero_format_index);
  72. hash = pair_int_hash(hash, positive_format_index);
  73. hash = pair_int_hash(hash, negative_format_index);
  74. for (auto index : identifier_indices)
  75. hash = pair_int_hash(hash, index);
  76. return hash;
  77. }
  78. bool operator==(NumberFormat const& other) const
  79. {
  80. return (magnitude == other.magnitude)
  81. && (exponent == other.exponent)
  82. && (plurality == other.plurality)
  83. && (zero_format_index == other.zero_format_index)
  84. && (positive_format_index == other.positive_format_index)
  85. && (negative_format_index == other.negative_format_index)
  86. && (identifier_indices == other.identifier_indices);
  87. }
  88. StringIndexType zero_format_index { 0 };
  89. StringIndexType positive_format_index { 0 };
  90. StringIndexType negative_format_index { 0 };
  91. Vector<StringIndexType> identifier_indices {};
  92. };
  93. template<>
  94. struct AK::Formatter<NumberFormat> : Formatter<FormatString> {
  95. ErrorOr<void> format(FormatBuilder& builder, NumberFormat const& format)
  96. {
  97. StringBuilder identifier_indices;
  98. identifier_indices.join(", "sv, format.identifier_indices);
  99. return Formatter<FormatString>::format(builder,
  100. "{{ {}, {}, {}, {}, {}, {}, {{ {} }} }}",
  101. format.magnitude,
  102. format.exponent,
  103. static_cast<u8>(format.plurality),
  104. format.zero_format_index,
  105. format.positive_format_index,
  106. format.negative_format_index,
  107. identifier_indices.build());
  108. }
  109. };
  110. template<>
  111. struct AK::Traits<NumberFormat> : public GenericTraits<NumberFormat> {
  112. static unsigned hash(NumberFormat const& f) { return f.hash(); }
  113. };
  114. using NumberFormatList = Vector<NumberFormatIndexType>;
  115. using NumericSymbolList = Vector<StringIndexType>;
  116. struct NumberSystem {
  117. unsigned hash() const
  118. {
  119. auto hash = int_hash(symbols);
  120. hash = pair_int_hash(hash, primary_grouping_size);
  121. hash = pair_int_hash(hash, secondary_grouping_size);
  122. hash = pair_int_hash(hash, decimal_format);
  123. hash = pair_int_hash(hash, decimal_long_formats);
  124. hash = pair_int_hash(hash, decimal_short_formats);
  125. hash = pair_int_hash(hash, currency_format);
  126. hash = pair_int_hash(hash, accounting_format);
  127. hash = pair_int_hash(hash, currency_unit_formats);
  128. hash = pair_int_hash(hash, currency_short_formats);
  129. hash = pair_int_hash(hash, percent_format);
  130. hash = pair_int_hash(hash, scientific_format);
  131. return hash;
  132. }
  133. bool operator==(NumberSystem const& other) const
  134. {
  135. return (symbols == other.symbols)
  136. && (primary_grouping_size == other.primary_grouping_size)
  137. && (secondary_grouping_size == other.secondary_grouping_size)
  138. && (decimal_format == other.decimal_format)
  139. && (decimal_long_formats == other.decimal_long_formats)
  140. && (decimal_short_formats == other.decimal_short_formats)
  141. && (currency_format == other.currency_format)
  142. && (accounting_format == other.accounting_format)
  143. && (currency_unit_formats == other.currency_unit_formats)
  144. && (currency_short_formats == other.currency_short_formats)
  145. && (percent_format == other.percent_format)
  146. && (scientific_format == other.scientific_format);
  147. }
  148. NumericSymbolListIndexType symbols { 0 };
  149. u8 primary_grouping_size { 0 };
  150. u8 secondary_grouping_size { 0 };
  151. NumberFormatIndexType decimal_format { 0 };
  152. NumberFormatListIndexType decimal_long_formats { 0 };
  153. NumberFormatListIndexType decimal_short_formats { 0 };
  154. NumberFormatIndexType currency_format { 0 };
  155. NumberFormatIndexType accounting_format { 0 };
  156. NumberFormatListIndexType currency_unit_formats { 0 };
  157. NumberFormatListIndexType currency_short_formats { 0 };
  158. NumberFormatIndexType percent_format { 0 };
  159. NumberFormatIndexType scientific_format { 0 };
  160. };
  161. template<>
  162. struct AK::Formatter<NumberSystem> : Formatter<FormatString> {
  163. ErrorOr<void> format(FormatBuilder& builder, NumberSystem const& system)
  164. {
  165. return Formatter<FormatString>::format(builder,
  166. "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}",
  167. system.symbols,
  168. system.primary_grouping_size,
  169. system.secondary_grouping_size,
  170. system.decimal_format,
  171. system.decimal_long_formats,
  172. system.decimal_short_formats,
  173. system.currency_format,
  174. system.accounting_format,
  175. system.currency_unit_formats,
  176. system.currency_short_formats,
  177. system.percent_format,
  178. system.scientific_format);
  179. }
  180. };
  181. template<>
  182. struct AK::Traits<NumberSystem> : public GenericTraits<NumberSystem> {
  183. static unsigned hash(NumberSystem const& s) { return s.hash(); }
  184. };
  185. struct Unit {
  186. unsigned hash() const
  187. {
  188. auto hash = int_hash(unit);
  189. hash = pair_int_hash(hash, long_formats);
  190. hash = pair_int_hash(hash, short_formats);
  191. hash = pair_int_hash(hash, narrow_formats);
  192. return hash;
  193. }
  194. bool operator==(Unit const& other) const
  195. {
  196. return (unit == other.unit)
  197. && (long_formats == other.long_formats)
  198. && (short_formats == other.short_formats)
  199. && (narrow_formats == other.narrow_formats);
  200. }
  201. StringIndexType unit { 0 };
  202. NumberFormatListIndexType long_formats { 0 };
  203. NumberFormatListIndexType short_formats { 0 };
  204. NumberFormatListIndexType narrow_formats { 0 };
  205. };
  206. template<>
  207. struct AK::Formatter<Unit> : Formatter<FormatString> {
  208. ErrorOr<void> format(FormatBuilder& builder, Unit const& system)
  209. {
  210. return Formatter<FormatString>::format(builder,
  211. "{{ {}, {}, {}, {} }}",
  212. system.unit,
  213. system.long_formats,
  214. system.short_formats,
  215. system.narrow_formats);
  216. }
  217. };
  218. template<>
  219. struct AK::Traits<Unit> : public GenericTraits<Unit> {
  220. static unsigned hash(Unit const& u) { return u.hash(); }
  221. };
  222. struct Locale {
  223. Vector<NumberSystemIndexType> number_systems;
  224. HashMap<String, UnitIndexType> units {};
  225. u8 minimum_grouping_digits { 0 };
  226. };
  227. struct UnicodeLocaleData {
  228. UniqueStringStorage<StringIndexType> unique_strings;
  229. UniqueStorage<NumberFormat, NumberFormatIndexType> unique_formats;
  230. UniqueStorage<NumberFormatList, NumberFormatListIndexType> unique_format_lists;
  231. UniqueStorage<NumericSymbolList, NumericSymbolListIndexType> unique_symbols;
  232. UniqueStorage<NumberSystem, NumberSystemIndexType> unique_systems;
  233. UniqueStorage<Unit, UnitIndexType> unique_units;
  234. HashMap<String, Array<u32, 10>> number_system_digits;
  235. Vector<String> number_systems;
  236. HashMap<String, Locale> locales;
  237. size_t max_identifier_count { 0 };
  238. };
  239. static ErrorOr<void> parse_number_system_digits(String core_supplemental_path, UnicodeLocaleData& locale_data)
  240. {
  241. LexicalPath number_systems_path(move(core_supplemental_path));
  242. number_systems_path = number_systems_path.append("numberingSystems.json"sv);
  243. auto number_systems = TRY(read_json_file(number_systems_path.string()));
  244. auto const& supplemental_object = number_systems.as_object().get("supplemental"sv);
  245. auto const& number_systems_object = supplemental_object.as_object().get("numberingSystems"sv);
  246. number_systems_object.as_object().for_each_member([&](auto const& number_system, auto const& digits_object) {
  247. auto type = digits_object.as_object().get("_type"sv).as_string();
  248. if (type != "numeric"sv)
  249. return;
  250. auto digits = digits_object.as_object().get("_digits"sv).as_string();
  251. Utf8View utf8_digits { digits };
  252. VERIFY(utf8_digits.length() == 10);
  253. auto& number_system_digits = locale_data.number_system_digits.ensure(number_system);
  254. size_t index = 0;
  255. for (u32 digit : utf8_digits)
  256. number_system_digits[index++] = digit;
  257. if (!locale_data.number_systems.contains_slow(number_system))
  258. locale_data.number_systems.append(number_system);
  259. });
  260. return {};
  261. }
  262. static String parse_identifiers(String pattern, StringView replacement, UnicodeLocaleData& locale_data, NumberFormat& format)
  263. {
  264. static constexpr Utf8View whitespace { "\u0020\u00a0\u200f"sv };
  265. while (true) {
  266. Utf8View utf8_pattern { pattern };
  267. Optional<size_t> start_index;
  268. Optional<size_t> end_index;
  269. bool inside_replacement = false;
  270. for (auto it = utf8_pattern.begin(); it != utf8_pattern.end(); ++it) {
  271. if (*it == '{') {
  272. if (start_index.has_value()) {
  273. end_index = utf8_pattern.byte_offset_of(it);
  274. break;
  275. }
  276. inside_replacement = true;
  277. } else if (*it == '}') {
  278. inside_replacement = false;
  279. } else if (!inside_replacement && !start_index.has_value() && !whitespace.contains(*it)) {
  280. start_index = utf8_pattern.byte_offset_of(it);
  281. }
  282. }
  283. if (!start_index.has_value())
  284. return pattern;
  285. end_index = end_index.value_or(pattern.length());
  286. utf8_pattern = utf8_pattern.substring_view(*start_index, *end_index - *start_index);
  287. utf8_pattern = utf8_pattern.trim(whitespace);
  288. auto identifier = utf8_pattern.as_string().replace("'.'"sv, "."sv);
  289. auto identifier_index = locale_data.unique_strings.ensure(move(identifier));
  290. size_t replacement_index = 0;
  291. if (auto index = format.identifier_indices.find_first_index(identifier_index); index.has_value()) {
  292. replacement_index = *index;
  293. } else {
  294. replacement_index = format.identifier_indices.size();
  295. format.identifier_indices.append(identifier_index);
  296. locale_data.max_identifier_count = max(locale_data.max_identifier_count, format.identifier_indices.size());
  297. }
  298. pattern = String::formatted("{}{{{}:{}}}{}",
  299. *start_index > 0 ? pattern.substring_view(0, *start_index) : ""sv,
  300. replacement,
  301. replacement_index,
  302. pattern.substring_view(*start_index + utf8_pattern.byte_length()));
  303. }
  304. }
  305. static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& locale_data, NumberFormatType type, NumberFormat& format, NumberSystem* number_system_for_groupings = nullptr)
  306. {
  307. // https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns
  308. // https://cldr.unicode.org/translation/number-currency-formats/number-and-currency-patterns
  309. VERIFY((patterns.size() == 1) || (patterns.size() == 2));
  310. auto replace_patterns = [&](String pattern) {
  311. static HashMap<StringView, StringView> replacements = {
  312. { "{0}"sv, "{number}"sv },
  313. { "{1}"sv, "{currency}"sv },
  314. { "%"sv, "{percentSign}"sv },
  315. { "+"sv, "{plusSign}"sv },
  316. { "-"sv, "{minusSign}"sv },
  317. { "¤"sv, "{currency}"sv }, // U+00A4 Currency Sign
  318. { "E"sv, "{scientificSeparator}"sv },
  319. };
  320. for (auto const& replacement : replacements)
  321. pattern = pattern.replace(replacement.key, replacement.value, true);
  322. if (auto start_number_index = pattern.find_any_of("#0"sv, String::SearchDirection::Forward); start_number_index.has_value()) {
  323. auto end_number_index = *start_number_index + 1;
  324. for (; end_number_index < pattern.length(); ++end_number_index) {
  325. auto ch = pattern[end_number_index];
  326. if ((ch != '#') && (ch != '0') && (ch != ',') && (ch != '.'))
  327. break;
  328. }
  329. if (number_system_for_groupings) {
  330. auto number_pattern = pattern.substring_view(*start_number_index, end_number_index - *start_number_index);
  331. auto group_separators = number_pattern.find_all(","sv);
  332. VERIFY((group_separators.size() == 1) || (group_separators.size() == 2));
  333. auto decimal = number_pattern.find('.');
  334. VERIFY(decimal.has_value());
  335. if (group_separators.size() == 1) {
  336. number_system_for_groupings->primary_grouping_size = *decimal - group_separators[0] - 1;
  337. number_system_for_groupings->secondary_grouping_size = number_system_for_groupings->primary_grouping_size;
  338. } else {
  339. number_system_for_groupings->primary_grouping_size = *decimal - group_separators[1] - 1;
  340. number_system_for_groupings->secondary_grouping_size = group_separators[1] - group_separators[0] - 1;
  341. }
  342. }
  343. pattern = String::formatted("{}{{number}}{}",
  344. *start_number_index > 0 ? pattern.substring_view(0, *start_number_index) : ""sv,
  345. pattern.substring_view(end_number_index));
  346. // This is specifically handled here rather than in the replacements HashMap above so
  347. // that we do not errantly replace zeroes in number patterns.
  348. if (pattern.contains(*replacements.get("E"sv)))
  349. pattern = pattern.replace("0"sv, "{scientificExponent}"sv);
  350. }
  351. if (type == NumberFormatType::Compact)
  352. return parse_identifiers(move(pattern), "compactIdentifier"sv, locale_data, format);
  353. return pattern;
  354. };
  355. auto zero_format = replace_patterns(move(patterns[0]));
  356. format.positive_format_index = locale_data.unique_strings.ensure(String::formatted("{{plusSign}}{}", zero_format));
  357. if (patterns.size() == 2) {
  358. auto negative_format = replace_patterns(move(patterns[1]));
  359. format.negative_format_index = locale_data.unique_strings.ensure(move(negative_format));
  360. } else {
  361. format.negative_format_index = locale_data.unique_strings.ensure(String::formatted("{{minusSign}}{}", zero_format));
  362. }
  363. format.zero_format_index = locale_data.unique_strings.ensure(move(zero_format));
  364. }
  365. static void parse_number_pattern(Vector<String> patterns, UnicodeLocaleData& locale_data, NumberFormatType type, NumberFormatIndexType& format_index, NumberSystem* number_system_for_groupings = nullptr)
  366. {
  367. NumberFormat format {};
  368. parse_number_pattern(move(patterns), locale_data, type, format, number_system_for_groupings);
  369. format_index = locale_data.unique_formats.ensure(move(format));
  370. }
  371. static ErrorOr<void> parse_number_systems(String locale_numbers_path, UnicodeLocaleData& locale_data, Locale& locale)
  372. {
  373. LexicalPath numbers_path(move(locale_numbers_path));
  374. numbers_path = numbers_path.append("numbers.json"sv);
  375. auto numbers = TRY(read_json_file(numbers_path.string()));
  376. auto const& main_object = numbers.as_object().get("main"sv);
  377. auto const& locale_object = main_object.as_object().get(numbers_path.parent().basename());
  378. auto const& locale_numbers_object = locale_object.as_object().get("numbers"sv);
  379. auto const& minimum_grouping_digits = locale_numbers_object.as_object().get("minimumGroupingDigits"sv);
  380. Vector<Optional<NumberSystem>> number_systems;
  381. number_systems.resize(locale_data.number_systems.size());
  382. auto ensure_number_system = [&](auto const& system) -> NumberSystem& {
  383. auto system_index = locale_data.number_systems.find_first_index(system).value();
  384. VERIFY(system_index < number_systems.size());
  385. auto& number_system = number_systems.at(system_index);
  386. if (!number_system.has_value())
  387. number_system = NumberSystem {};
  388. return number_system.value();
  389. };
  390. auto parse_number_format = [&](auto const& format_object) {
  391. Vector<NumberFormatIndexType> result;
  392. result.ensure_capacity(format_object.size());
  393. format_object.for_each_member([&](auto const& key, JsonValue const& value) {
  394. auto split_key = key.split_view('-');
  395. if (split_key.size() != 3)
  396. return;
  397. auto patterns = value.as_string().split(';');
  398. NumberFormat format {};
  399. if (auto type = split_key[0].template to_uint<u64>(); type.has_value()) {
  400. VERIFY(*type % 10 == 0);
  401. format.magnitude = static_cast<u8>(log10(*type));
  402. if (patterns[0] != "0"sv) {
  403. auto number_of_zeroes_in_pattern = patterns[0].count("0"sv);
  404. VERIFY(format.magnitude >= number_of_zeroes_in_pattern);
  405. format.exponent = format.magnitude + 1 - number_of_zeroes_in_pattern;
  406. }
  407. } else {
  408. VERIFY(split_key[0] == "unitPattern"sv);
  409. }
  410. format.plurality = NumberFormat::plurality_from_string(split_key[2]);
  411. parse_number_pattern(move(patterns), locale_data, NumberFormatType::Compact, format);
  412. auto format_index = locale_data.unique_formats.ensure(move(format));
  413. result.append(format_index);
  414. });
  415. return locale_data.unique_format_lists.ensure(move(result));
  416. };
  417. auto numeric_symbol_from_string = [&](StringView numeric_symbol) -> Optional<Unicode::NumericSymbol> {
  418. if (numeric_symbol == "decimal"sv)
  419. return Unicode::NumericSymbol::Decimal;
  420. if (numeric_symbol == "exponential"sv)
  421. return Unicode::NumericSymbol::Exponential;
  422. if (numeric_symbol == "group"sv)
  423. return Unicode::NumericSymbol::Group;
  424. if (numeric_symbol == "infinity"sv)
  425. return Unicode::NumericSymbol::Infinity;
  426. if (numeric_symbol == "minusSign"sv)
  427. return Unicode::NumericSymbol::MinusSign;
  428. if (numeric_symbol == "nan"sv)
  429. return Unicode::NumericSymbol::NaN;
  430. if (numeric_symbol == "percentSign"sv)
  431. return Unicode::NumericSymbol::PercentSign;
  432. if (numeric_symbol == "plusSign"sv)
  433. return Unicode::NumericSymbol::PlusSign;
  434. return {};
  435. };
  436. locale_numbers_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
  437. constexpr auto symbols_prefix = "symbols-numberSystem-"sv;
  438. constexpr auto decimal_formats_prefix = "decimalFormats-numberSystem-"sv;
  439. constexpr auto currency_formats_prefix = "currencyFormats-numberSystem-"sv;
  440. constexpr auto percent_formats_prefix = "percentFormats-numberSystem-"sv;
  441. constexpr auto scientific_formats_prefix = "scientificFormats-numberSystem-"sv;
  442. if (key.starts_with(symbols_prefix)) {
  443. auto system = key.substring(symbols_prefix.length());
  444. auto& number_system = ensure_number_system(system);
  445. NumericSymbolList symbols;
  446. value.as_object().for_each_member([&](auto const& symbol, JsonValue const& localization) {
  447. auto numeric_symbol = numeric_symbol_from_string(symbol);
  448. if (!numeric_symbol.has_value())
  449. return;
  450. if (to_underlying(*numeric_symbol) >= symbols.size())
  451. symbols.resize(to_underlying(*numeric_symbol) + 1);
  452. auto symbol_index = locale_data.unique_strings.ensure(localization.as_string());
  453. symbols[to_underlying(*numeric_symbol)] = symbol_index;
  454. });
  455. number_system.symbols = locale_data.unique_symbols.ensure(move(symbols));
  456. } else if (key.starts_with(decimal_formats_prefix)) {
  457. auto system = key.substring(decimal_formats_prefix.length());
  458. auto& number_system = ensure_number_system(system);
  459. auto format_object = value.as_object().get("standard"sv);
  460. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.decimal_format, &number_system);
  461. auto const& long_format = value.as_object().get("long"sv).as_object().get("decimalFormat"sv);
  462. number_system.decimal_long_formats = parse_number_format(long_format.as_object());
  463. auto const& short_format = value.as_object().get("short"sv).as_object().get("decimalFormat"sv);
  464. number_system.decimal_short_formats = parse_number_format(short_format.as_object());
  465. } else if (key.starts_with(currency_formats_prefix)) {
  466. auto system = key.substring(currency_formats_prefix.length());
  467. auto& number_system = ensure_number_system(system);
  468. auto format_object = value.as_object().get("standard"sv);
  469. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.currency_format);
  470. format_object = value.as_object().get("accounting"sv);
  471. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.accounting_format);
  472. number_system.currency_unit_formats = parse_number_format(value.as_object());
  473. if (value.as_object().has("short"sv)) {
  474. auto const& short_format = value.as_object().get("short"sv).as_object().get("standard"sv);
  475. number_system.currency_short_formats = parse_number_format(short_format.as_object());
  476. }
  477. } else if (key.starts_with(percent_formats_prefix)) {
  478. auto system = key.substring(percent_formats_prefix.length());
  479. auto& number_system = ensure_number_system(system);
  480. auto format_object = value.as_object().get("standard"sv);
  481. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.percent_format);
  482. } else if (key.starts_with(scientific_formats_prefix)) {
  483. auto system = key.substring(scientific_formats_prefix.length());
  484. auto& number_system = ensure_number_system(system);
  485. auto format_object = value.as_object().get("standard"sv);
  486. parse_number_pattern(format_object.as_string().split(';'), locale_data, NumberFormatType::Standard, number_system.scientific_format);
  487. }
  488. });
  489. locale.number_systems.ensure_capacity(number_systems.size());
  490. for (auto& number_system : number_systems) {
  491. NumberSystemIndexType system_index = 0;
  492. if (number_system.has_value())
  493. system_index = locale_data.unique_systems.ensure(number_system.release_value());
  494. locale.number_systems.append(system_index);
  495. }
  496. locale.minimum_grouping_digits = minimum_grouping_digits.as_string().template to_uint<u8>().value();
  497. return {};
  498. }
  499. static ErrorOr<void> parse_units(String locale_units_path, UnicodeLocaleData& locale_data, Locale& locale)
  500. {
  501. LexicalPath units_path(move(locale_units_path));
  502. units_path = units_path.append("units.json"sv);
  503. auto locale_units = TRY(read_json_file(units_path.string()));
  504. auto const& main_object = locale_units.as_object().get("main"sv);
  505. auto const& locale_object = main_object.as_object().get(units_path.parent().basename());
  506. auto const& locale_units_object = locale_object.as_object().get("units"sv);
  507. auto const& long_object = locale_units_object.as_object().get("long"sv);
  508. auto const& short_object = locale_units_object.as_object().get("short"sv);
  509. auto const& narrow_object = locale_units_object.as_object().get("narrow"sv);
  510. HashMap<String, Unit> units;
  511. auto ensure_unit = [&](auto const& unit) -> Unit& {
  512. return units.ensure(unit, [&]() {
  513. auto unit_index = locale_data.unique_strings.ensure(unit);
  514. return Unit { .unit = unit_index };
  515. });
  516. };
  517. auto is_sanctioned_unit = [](StringView unit_name) {
  518. // This is a copy of the units sanctioned for use within ECMA-402. LibUnicode generally tries to
  519. // avoid being directly dependent on ECMA-402, but this rather significantly reduces the amount
  520. // of data generated here, and ECMA-402 is currently the only consumer of this data.
  521. // https://tc39.es/ecma402/#table-sanctioned-simple-unit-identifiers
  522. constexpr auto sanctioned_units = AK::Array { "acre"sv, "bit"sv, "byte"sv, "celsius"sv, "centimeter"sv, "day"sv, "degree"sv, "fahrenheit"sv, "fluid-ounce"sv, "foot"sv, "gallon"sv, "gigabit"sv, "gigabyte"sv, "gram"sv, "hectare"sv, "hour"sv, "inch"sv, "kilobit"sv, "kilobyte"sv, "kilogram"sv, "kilometer"sv, "liter"sv, "megabit"sv, "megabyte"sv, "meter"sv, "mile"sv, "mile-scandinavian"sv, "milliliter"sv, "millimeter"sv, "millisecond"sv, "minute"sv, "month"sv, "ounce"sv, "percent"sv, "petabyte"sv, "pound"sv, "second"sv, "stone"sv, "terabit"sv, "terabyte"sv, "week"sv, "yard"sv, "year"sv };
  523. return find(sanctioned_units.begin(), sanctioned_units.end(), unit_name) != sanctioned_units.end();
  524. };
  525. auto parse_units_object = [&](auto const& units_object, Unicode::Style style) {
  526. constexpr auto unit_pattern_prefix = "unitPattern-count-"sv;
  527. constexpr auto combined_unit_separator = "-per-"sv;
  528. units_object.for_each_member([&](auto const& key, JsonValue const& value) {
  529. auto end_of_category = key.find('-');
  530. if (!end_of_category.has_value())
  531. return;
  532. auto unit_name = key.substring(*end_of_category + 1);
  533. if (!is_sanctioned_unit(unit_name)) {
  534. auto indices = unit_name.find_all(combined_unit_separator);
  535. if (indices.size() != 1)
  536. return;
  537. auto numerator = unit_name.substring_view(0, indices[0]);
  538. auto denominator = unit_name.substring_view(indices[0] + combined_unit_separator.length());
  539. if (!is_sanctioned_unit(numerator) || !is_sanctioned_unit(denominator))
  540. return;
  541. }
  542. auto& unit = ensure_unit(unit_name);
  543. NumberFormatList formats;
  544. value.as_object().for_each_member([&](auto const& unit_key, JsonValue const& pattern_value) {
  545. if (!unit_key.starts_with(unit_pattern_prefix))
  546. return;
  547. NumberFormat format {};
  548. auto plurality = unit_key.substring_view(unit_pattern_prefix.length());
  549. format.plurality = NumberFormat::plurality_from_string(plurality);
  550. auto zero_format = pattern_value.as_string().replace("{0}"sv, "{number}"sv);
  551. zero_format = parse_identifiers(zero_format, "unitIdentifier"sv, locale_data, format);
  552. format.positive_format_index = locale_data.unique_strings.ensure(zero_format.replace("{number}"sv, "{plusSign}{number}"sv));
  553. format.negative_format_index = locale_data.unique_strings.ensure(zero_format.replace("{number}"sv, "{minusSign}{number}"sv));
  554. format.zero_format_index = locale_data.unique_strings.ensure(move(zero_format));
  555. formats.append(locale_data.unique_formats.ensure(move(format)));
  556. });
  557. auto number_format_list_index = locale_data.unique_format_lists.ensure(move(formats));
  558. switch (style) {
  559. case Unicode::Style::Long:
  560. unit.long_formats = number_format_list_index;
  561. break;
  562. case Unicode::Style::Short:
  563. unit.short_formats = number_format_list_index;
  564. break;
  565. case Unicode::Style::Narrow:
  566. unit.narrow_formats = number_format_list_index;
  567. break;
  568. default:
  569. VERIFY_NOT_REACHED();
  570. }
  571. });
  572. };
  573. parse_units_object(long_object.as_object(), Unicode::Style::Long);
  574. parse_units_object(short_object.as_object(), Unicode::Style::Short);
  575. parse_units_object(narrow_object.as_object(), Unicode::Style::Narrow);
  576. for (auto& unit : units) {
  577. auto unit_index = locale_data.unique_units.ensure(move(unit.value));
  578. locale.units.set(unit.key, unit_index);
  579. }
  580. return {};
  581. }
  582. static ErrorOr<void> parse_all_locales(String core_path, String numbers_path, String units_path, UnicodeLocaleData& locale_data)
  583. {
  584. auto numbers_iterator = TRY(path_to_dir_iterator(move(numbers_path)));
  585. auto units_iterator = TRY(path_to_dir_iterator(move(units_path)));
  586. LexicalPath core_supplemental_path(move(core_path));
  587. core_supplemental_path = core_supplemental_path.append("supplemental"sv);
  588. VERIFY(Core::File::is_directory(core_supplemental_path.string()));
  589. TRY(parse_number_system_digits(core_supplemental_path.string(), locale_data));
  590. auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
  591. auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(locale_data.unique_strings, LexicalPath::basename(path)));
  592. StringBuilder builder;
  593. builder.append(locale_data.unique_strings.get(parsed_locale.language));
  594. if (auto script = locale_data.unique_strings.get(parsed_locale.script); !script.is_empty())
  595. builder.appendff("-{}", script);
  596. if (auto region = locale_data.unique_strings.get(parsed_locale.region); !region.is_empty())
  597. builder.appendff("-{}", region);
  598. return builder.build();
  599. };
  600. while (numbers_iterator.has_next()) {
  601. auto numbers_path = TRY(next_path_from_dir_iterator(numbers_iterator));
  602. auto language = TRY(remove_variants_from_path(numbers_path));
  603. auto& locale = locale_data.locales.ensure(language);
  604. TRY(parse_number_systems(numbers_path, locale_data, locale));
  605. }
  606. while (units_iterator.has_next()) {
  607. auto units_path = TRY(next_path_from_dir_iterator(units_iterator));
  608. auto language = TRY(remove_variants_from_path(units_path));
  609. auto& locale = locale_data.locales.ensure(language);
  610. TRY(parse_units(units_path, locale_data, locale));
  611. }
  612. return {};
  613. }
  614. static String format_identifier(StringView, String identifier)
  615. {
  616. return identifier.to_titlecase();
  617. }
  618. static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile& file, UnicodeLocaleData& locale_data)
  619. {
  620. StringBuilder builder;
  621. SourceGenerator generator { builder };
  622. generator.append(R"~~~(
  623. #include <AK/Types.h>
  624. #pragma once
  625. namespace Unicode {
  626. )~~~");
  627. generate_enum(generator, format_identifier, "NumberSystem"sv, {}, locale_data.number_systems);
  628. generator.append(R"~~~(
  629. }
  630. )~~~");
  631. TRY(file.write(generator.as_string_view().bytes()));
  632. return {};
  633. }
  634. static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::BufferedFile& file, UnicodeLocaleData& locale_data)
  635. {
  636. StringBuilder builder;
  637. SourceGenerator generator { builder };
  638. generator.set("string_index_type"sv, s_string_index_type);
  639. generator.set("number_format_index_type"sv, s_number_format_index_type);
  640. generator.set("number_format_list_index_type"sv, s_number_format_list_index_type);
  641. generator.set("numeric_symbol_list_index_type"sv, s_numeric_symbol_list_index_type);
  642. generator.set("identifier_count", String::number(locale_data.max_identifier_count));
  643. generator.append(R"~~~(
  644. #include <AK/Array.h>
  645. #include <AK/BinarySearch.h>
  646. #include <AK/Optional.h>
  647. #include <AK/Span.h>
  648. #include <AK/StringView.h>
  649. #include <AK/Vector.h>
  650. #include <LibUnicode/Locale.h>
  651. #include <LibUnicode/NumberFormat.h>
  652. #include <LibUnicode/UnicodeNumberFormat.h>
  653. namespace Unicode {
  654. )~~~");
  655. locale_data.unique_strings.generate(generator);
  656. generator.append(R"~~~(
  657. struct NumberFormatImpl {
  658. NumberFormat to_unicode_number_format() const {
  659. NumberFormat number_format {};
  660. number_format.magnitude = magnitude;
  661. number_format.exponent = exponent;
  662. number_format.plurality = static_cast<NumberFormat::Plurality>(plurality);
  663. number_format.zero_format = s_string_list[zero_format];
  664. number_format.positive_format = s_string_list[positive_format];
  665. number_format.negative_format = s_string_list[negative_format];
  666. number_format.identifiers.ensure_capacity(identifiers.size());
  667. for (@string_index_type@ identifier : identifiers)
  668. number_format.identifiers.append(s_string_list[identifier]);
  669. return number_format;
  670. }
  671. u8 magnitude { 0 };
  672. u8 exponent { 0 };
  673. u8 plurality { 0 };
  674. @string_index_type@ zero_format { 0 };
  675. @string_index_type@ positive_format { 0 };
  676. @string_index_type@ negative_format { 0 };
  677. Array<@string_index_type@, @identifier_count@> identifiers {};
  678. };
  679. struct NumberSystemData {
  680. @numeric_symbol_list_index_type@ symbols { 0 };
  681. u8 primary_grouping_size { 0 };
  682. u8 secondary_grouping_size { 0 };
  683. @number_format_index_type@ decimal_format { 0 };
  684. @number_format_list_index_type@ decimal_long_formats { 0 };
  685. @number_format_list_index_type@ decimal_short_formats { 0 };
  686. @number_format_index_type@ currency_format { 0 };
  687. @number_format_index_type@ accounting_format { 0 };
  688. @number_format_list_index_type@ currency_unit_formats { 0 };
  689. @number_format_list_index_type@ currency_short_formats { 0 };
  690. @number_format_index_type@ percent_format { 0 };
  691. @number_format_index_type@ scientific_format { 0 };
  692. };
  693. struct Unit {
  694. @string_index_type@ unit { 0 };
  695. @number_format_list_index_type@ long_formats { 0 };
  696. @number_format_list_index_type@ short_formats { 0 };
  697. @number_format_list_index_type@ narrow_formats { 0 };
  698. };
  699. )~~~");
  700. locale_data.unique_formats.generate(generator, "NumberFormatImpl"sv, "s_number_formats"sv, 10);
  701. locale_data.unique_format_lists.generate(generator, s_number_format_index_type, "s_number_format_lists"sv);
  702. locale_data.unique_symbols.generate(generator, s_string_index_type, "s_numeric_symbol_lists"sv);
  703. locale_data.unique_systems.generate(generator, "NumberSystemData"sv, "s_number_systems"sv, 10);
  704. locale_data.unique_units.generate(generator, "Unit"sv, "s_units"sv, 10);
  705. auto locales = locale_data.locales.keys();
  706. quick_sort(locales);
  707. generator.set("size", String::number(locales.size()));
  708. generator.append(R"~~~(
  709. static constexpr Array<u8, @size@> s_minimum_grouping_digits { { )~~~");
  710. bool first = true;
  711. for (auto const& locale : locales) {
  712. generator.append(first ? " " : ", ");
  713. generator.append(String::number(locale_data.locales.find(locale)->value.minimum_grouping_digits));
  714. first = false;
  715. }
  716. generator.append(" } };\n");
  717. auto append_map = [&](String name, auto type, auto const& map) {
  718. generator.set("name", name);
  719. generator.set("type", type);
  720. generator.set("size", String::number(map.size()));
  721. generator.append(R"~~~(
  722. static constexpr Array<@type@, @size@> @name@ { {)~~~");
  723. bool first = true;
  724. for (auto const& item : map) {
  725. generator.append(first ? " " : ", ");
  726. if constexpr (requires { item.value; })
  727. generator.append(String::number(item.value));
  728. else
  729. generator.append(String::number(item));
  730. first = false;
  731. }
  732. generator.append(" } };");
  733. };
  734. generate_mapping(generator, locale_data.number_system_digits, "u32"sv, "s_number_systems_digits"sv, "s_number_systems_digits_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, "u32"sv, value); });
  735. generate_mapping(generator, locale_data.locales, s_number_system_index_type, "s_locale_number_systems"sv, "s_number_systems_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, s_number_system_index_type, value.number_systems); });
  736. generate_mapping(generator, locale_data.locales, s_unit_index_type, "s_locale_units"sv, "s_units_{}", nullptr, [&](auto const& name, auto const& value) { append_map(name, s_unit_index_type, value.units); });
  737. auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values) {
  738. HashValueMap<String> hashes;
  739. hashes.ensure_capacity(values.size());
  740. for (auto const& value : values)
  741. hashes.set(value.hash(), format_identifier(enum_title, value));
  742. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
  743. };
  744. append_from_string("NumberSystem"sv, "number_system"sv, locale_data.number_systems);
  745. generator.append(R"~~~(
  746. Optional<Span<u32 const>> get_digits_for_number_system(StringView system)
  747. {
  748. auto number_system_value = number_system_from_string(system);
  749. if (!number_system_value.has_value())
  750. return {};
  751. auto number_system_index = to_underlying(*number_system_value);
  752. return s_number_systems_digits[number_system_index];
  753. }
  754. static NumberSystemData const* find_number_system(StringView locale, StringView system)
  755. {
  756. auto locale_value = locale_from_string(locale);
  757. if (!locale_value.has_value())
  758. return nullptr;
  759. auto number_system_value = number_system_from_string(system);
  760. if (!number_system_value.has_value())
  761. return nullptr;
  762. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  763. auto number_system_index = to_underlying(*number_system_value);
  764. auto const& number_systems = s_locale_number_systems.at(locale_index);
  765. number_system_index = number_systems.at(number_system_index);
  766. if (number_system_index == 0)
  767. return nullptr;
  768. return &s_number_systems.at(number_system_index);
  769. }
  770. Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol)
  771. {
  772. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  773. auto symbols = s_numeric_symbol_lists.at(number_system->symbols);
  774. auto symbol_index = to_underlying(symbol);
  775. if (symbol_index >= symbols.size())
  776. return {};
  777. return s_string_list[symbols[symbol_index]];
  778. }
  779. return {};
  780. }
  781. Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system)
  782. {
  783. auto locale_value = locale_from_string(locale);
  784. if (!locale_value.has_value())
  785. return {};
  786. u8 minimum_grouping_digits = s_minimum_grouping_digits[to_underlying(*locale_value) - 1];
  787. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr)
  788. return NumberGroupings { minimum_grouping_digits, number_system->primary_grouping_size, number_system->secondary_grouping_size };
  789. return {};
  790. }
  791. Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type)
  792. {
  793. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  794. @number_format_index_type@ format_index = 0;
  795. switch (type) {
  796. case StandardNumberFormatType::Decimal:
  797. format_index = number_system->decimal_format;
  798. break;
  799. case StandardNumberFormatType::Currency:
  800. format_index = number_system->currency_format;
  801. break;
  802. case StandardNumberFormatType::Accounting:
  803. format_index = number_system->accounting_format;
  804. break;
  805. case StandardNumberFormatType::Percent:
  806. format_index = number_system->percent_format;
  807. break;
  808. case StandardNumberFormatType::Scientific:
  809. format_index = number_system->scientific_format;
  810. break;
  811. }
  812. return s_number_formats[format_index].to_unicode_number_format();
  813. }
  814. return {};
  815. }
  816. Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type)
  817. {
  818. Vector<NumberFormat> formats;
  819. if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
  820. @number_format_list_index_type@ number_format_list_index { 0 };
  821. switch (type) {
  822. case CompactNumberFormatType::DecimalLong:
  823. number_format_list_index = number_system->decimal_long_formats;
  824. break;
  825. case CompactNumberFormatType::DecimalShort:
  826. number_format_list_index = number_system->decimal_short_formats;
  827. break;
  828. case CompactNumberFormatType::CurrencyUnit:
  829. number_format_list_index = number_system->currency_unit_formats;
  830. break;
  831. case CompactNumberFormatType::CurrencyShort:
  832. number_format_list_index = number_system->currency_short_formats;
  833. break;
  834. }
  835. auto number_formats = s_number_format_lists.at(number_format_list_index);
  836. formats.ensure_capacity(number_formats.size());
  837. for (auto number_format : number_formats)
  838. formats.append(s_number_formats[number_format].to_unicode_number_format());
  839. }
  840. return formats;
  841. }
  842. static Unit const* find_units(StringView locale, StringView unit)
  843. {
  844. auto locale_value = locale_from_string(locale);
  845. if (!locale_value.has_value())
  846. return nullptr;
  847. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  848. auto const& locale_units = s_locale_units.at(locale_index);
  849. for (auto unit_index : locale_units) {
  850. auto const& units = s_units.at(unit_index);
  851. if (unit == s_string_list[units.unit])
  852. return &units;
  853. };
  854. return nullptr;
  855. }
  856. Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style)
  857. {
  858. Vector<NumberFormat> formats;
  859. if (auto const* units = find_units(locale, unit); units != nullptr) {
  860. @number_format_list_index_type@ number_format_list_index { 0 };
  861. switch (style) {
  862. case Style::Long:
  863. number_format_list_index = units->long_formats;
  864. break;
  865. case Style::Short:
  866. number_format_list_index = units->short_formats;
  867. break;
  868. case Style::Narrow:
  869. number_format_list_index = units->narrow_formats;
  870. break;
  871. default:
  872. VERIFY_NOT_REACHED();
  873. }
  874. auto number_formats = s_number_format_lists.at(number_format_list_index);
  875. formats.ensure_capacity(number_formats.size());
  876. for (auto number_format : number_formats)
  877. formats.append(s_number_formats[number_format].to_unicode_number_format());
  878. }
  879. return formats;
  880. }
  881. }
  882. )~~~");
  883. TRY(file.write(generator.as_string_view().bytes()));
  884. return {};
  885. }
  886. ErrorOr<int> serenity_main(Main::Arguments arguments)
  887. {
  888. StringView generated_header_path;
  889. StringView generated_implementation_path;
  890. StringView core_path;
  891. StringView numbers_path;
  892. StringView units_path;
  893. Core::ArgsParser args_parser;
  894. args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path");
  895. args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  896. args_parser.add_option(core_path, "Path to cldr-core directory", "core-path", 'r', "core-path");
  897. args_parser.add_option(numbers_path, "Path to cldr-numbers directory", "numbers-path", 'n', "numbers-path");
  898. args_parser.add_option(units_path, "Path to cldr-units directory", "units-path", 'u', "units-path");
  899. args_parser.parse(arguments);
  900. auto generated_header_file = TRY(open_file(generated_header_path, Core::Stream::OpenMode::Write));
  901. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::Stream::OpenMode::Write));
  902. UnicodeLocaleData locale_data;
  903. TRY(parse_all_locales(core_path, numbers_path, units_path, locale_data));
  904. TRY(generate_unicode_locale_header(*generated_header_file, locale_data));
  905. TRY(generate_unicode_locale_implementation(*generated_implementation_file, locale_data));
  906. return 0;
  907. }