GenerateNumberFormatData.cpp 45 KB

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