GenerateUnicodeDateTimeFormat.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GeneratorUtil.h"
  7. #include <AK/AllOf.h>
  8. #include <AK/CharacterTypes.h>
  9. #include <AK/Format.h>
  10. #include <AK/GenericLexer.h>
  11. #include <AK/HashFunctions.h>
  12. #include <AK/HashMap.h>
  13. #include <AK/JsonObject.h>
  14. #include <AK/JsonParser.h>
  15. #include <AK/JsonValue.h>
  16. #include <AK/LexicalPath.h>
  17. #include <AK/SourceGenerator.h>
  18. #include <AK/String.h>
  19. #include <AK/StringBuilder.h>
  20. #include <AK/Traits.h>
  21. #include <AK/Utf8View.h>
  22. #include <LibCore/ArgsParser.h>
  23. #include <LibCore/DirIterator.h>
  24. #include <LibCore/File.h>
  25. #include <LibTimeZone/TimeZone.h>
  26. #include <LibUnicode/DateTimeFormat.h>
  27. using StringIndexType = u16;
  28. constexpr auto s_string_index_type = "u16"sv;
  29. using CalendarPatternIndexType = u16;
  30. constexpr auto s_calendar_pattern_index_type = "u16"sv;
  31. using CalendarPatternListIndexType = u8;
  32. constexpr auto s_calendar_pattern_list_index_type = "u8"sv;
  33. using CalendarRangePatternIndexType = u16;
  34. constexpr auto s_calendar_range_pattern_index_type = "u16"sv;
  35. using CalendarRangePatternListIndexType = u8;
  36. constexpr auto s_calendar_range_pattern_list_index_type = "u8"sv;
  37. using CalendarFormatIndexType = u8;
  38. constexpr auto s_calendar_format_index_type = "u8"sv;
  39. using SymbolListIndexType = u16;
  40. constexpr auto s_symbol_list_index_type = "u16"sv;
  41. using CalendarSymbolsIndexType = u16;
  42. constexpr auto s_calendar_symbols_index_type = "u16"sv;
  43. using CalendarSymbolsListIndexType = u8;
  44. constexpr auto s_calendar_symbols_list_index_type = "u8"sv;
  45. using CalendarIndexType = u8;
  46. constexpr auto s_calendar_index_type = "u8"sv;
  47. using TimeZoneNamesIndexType = u16;
  48. constexpr auto s_time_zone_index_type = "u16"sv;
  49. using TimeZoneNamesListIndexType = u8;
  50. constexpr auto s_time_zone_list_index_type = "u8"sv;
  51. using TimeZoneFormatIndexType = u8;
  52. constexpr auto s_time_zone_format_index_type = "u8"sv;
  53. using DayPeriodIndexType = u8;
  54. constexpr auto s_day_period_index_type = "u8"sv;
  55. using DayPeriodListIndexType = u8;
  56. constexpr auto s_day_period_list_index_type = "u8"sv;
  57. using HourCycleListIndexType = u8;
  58. constexpr auto s_hour_cycle_list_index_type = "u8"sv;
  59. struct CalendarPattern : public Unicode::CalendarPattern {
  60. bool contains_only_date_fields() const
  61. {
  62. return !day_period.has_value() && !hour.has_value() && !minute.has_value() && !second.has_value() && !fractional_second_digits.has_value() && !time_zone_name.has_value();
  63. }
  64. bool contains_only_time_fields() const
  65. {
  66. return !weekday.has_value() && !era.has_value() && !year.has_value() && !month.has_value() && !day.has_value();
  67. }
  68. unsigned hash() const
  69. {
  70. auto hash = pair_int_hash(pattern_index, pattern12_index);
  71. hash = pair_int_hash(hash, skeleton_index);
  72. auto hash_field = [&](auto const& field) {
  73. if (field.has_value())
  74. hash = pair_int_hash(hash, static_cast<u8>(*field));
  75. else
  76. hash = pair_int_hash(hash, -1);
  77. };
  78. hash_field(era);
  79. hash_field(year);
  80. hash_field(month);
  81. hash_field(weekday);
  82. hash_field(day);
  83. hash_field(day_period);
  84. hash_field(hour);
  85. hash_field(minute);
  86. hash_field(second);
  87. hash_field(fractional_second_digits);
  88. hash_field(time_zone_name);
  89. return hash;
  90. }
  91. bool operator==(CalendarPattern const& other) const
  92. {
  93. return (skeleton_index == other.skeleton_index)
  94. && (pattern_index == other.pattern_index)
  95. && (pattern12_index == other.pattern12_index)
  96. && (era == other.era)
  97. && (year == other.year)
  98. && (month == other.month)
  99. && (weekday == other.weekday)
  100. && (day == other.day)
  101. && (day_period == other.day_period)
  102. && (hour == other.hour)
  103. && (minute == other.minute)
  104. && (second == other.second)
  105. && (fractional_second_digits == other.fractional_second_digits)
  106. && (time_zone_name == other.time_zone_name);
  107. }
  108. StringIndexType skeleton_index { 0 };
  109. StringIndexType pattern_index { 0 };
  110. StringIndexType pattern12_index { 0 };
  111. };
  112. template<>
  113. struct AK::Formatter<CalendarPattern> : Formatter<FormatString> {
  114. ErrorOr<void> format(FormatBuilder& builder, CalendarPattern const& pattern)
  115. {
  116. auto field_to_i8 = [](auto const& field) -> i8 {
  117. if (!field.has_value())
  118. return -1;
  119. return static_cast<i8>(*field);
  120. };
  121. return Formatter<FormatString>::format(builder,
  122. "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}",
  123. pattern.skeleton_index,
  124. pattern.pattern_index,
  125. pattern.pattern12_index,
  126. field_to_i8(pattern.era),
  127. field_to_i8(pattern.year),
  128. field_to_i8(pattern.month),
  129. field_to_i8(pattern.weekday),
  130. field_to_i8(pattern.day),
  131. field_to_i8(pattern.day_period),
  132. field_to_i8(pattern.hour),
  133. field_to_i8(pattern.minute),
  134. field_to_i8(pattern.second),
  135. field_to_i8(pattern.fractional_second_digits),
  136. field_to_i8(pattern.time_zone_name));
  137. }
  138. };
  139. template<>
  140. struct AK::Traits<CalendarPattern> : public GenericTraits<CalendarPattern> {
  141. static unsigned hash(CalendarPattern const& c) { return c.hash(); }
  142. };
  143. struct CalendarRangePattern : public CalendarPattern {
  144. unsigned hash() const
  145. {
  146. auto hash = CalendarPattern::hash();
  147. if (field.has_value())
  148. hash = pair_int_hash(hash, static_cast<u8>(*field));
  149. hash = pair_int_hash(hash, start_range);
  150. hash = pair_int_hash(hash, separator);
  151. hash = pair_int_hash(hash, end_range);
  152. return hash;
  153. }
  154. bool operator==(CalendarRangePattern const& other) const
  155. {
  156. if (!CalendarPattern::operator==(other))
  157. return false;
  158. return (field == other.field)
  159. && (start_range == other.start_range)
  160. && (separator == other.separator)
  161. && (end_range == other.end_range);
  162. }
  163. Optional<Unicode::CalendarRangePattern::Field> field {};
  164. StringIndexType start_range { 0 };
  165. StringIndexType separator { 0 };
  166. StringIndexType end_range { 0 };
  167. };
  168. template<>
  169. struct AK::Formatter<CalendarRangePattern> : Formatter<FormatString> {
  170. ErrorOr<void> format(FormatBuilder& builder, CalendarRangePattern const& pattern)
  171. {
  172. auto field_to_i8 = [](auto const& field) -> i8 {
  173. if (!field.has_value())
  174. return -1;
  175. return static_cast<i8>(*field);
  176. };
  177. return Formatter<FormatString>::format(builder,
  178. "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}",
  179. pattern.skeleton_index,
  180. field_to_i8(pattern.field),
  181. pattern.start_range,
  182. pattern.separator,
  183. pattern.end_range,
  184. field_to_i8(pattern.era),
  185. field_to_i8(pattern.year),
  186. field_to_i8(pattern.month),
  187. field_to_i8(pattern.weekday),
  188. field_to_i8(pattern.day),
  189. field_to_i8(pattern.day_period),
  190. field_to_i8(pattern.hour),
  191. field_to_i8(pattern.minute),
  192. field_to_i8(pattern.second),
  193. field_to_i8(pattern.fractional_second_digits),
  194. field_to_i8(pattern.time_zone_name));
  195. }
  196. };
  197. template<>
  198. struct AK::Traits<CalendarRangePattern> : public GenericTraits<CalendarRangePattern> {
  199. static unsigned hash(CalendarRangePattern const& c) { return c.hash(); }
  200. };
  201. struct CalendarFormat {
  202. unsigned hash() const
  203. {
  204. auto hash = pair_int_hash(full_format, long_format);
  205. hash = pair_int_hash(hash, medium_format);
  206. hash = pair_int_hash(hash, short_format);
  207. return hash;
  208. }
  209. bool operator==(CalendarFormat const& other) const
  210. {
  211. return (full_format == other.full_format)
  212. && (long_format == other.long_format)
  213. && (medium_format == other.medium_format)
  214. && (short_format == other.short_format);
  215. }
  216. CalendarPatternIndexType full_format { 0 };
  217. CalendarPatternIndexType long_format { 0 };
  218. CalendarPatternIndexType medium_format { 0 };
  219. CalendarPatternIndexType short_format { 0 };
  220. };
  221. template<>
  222. struct AK::Formatter<CalendarFormat> : Formatter<FormatString> {
  223. ErrorOr<void> format(FormatBuilder& builder, CalendarFormat const& pattern)
  224. {
  225. return Formatter<FormatString>::format(builder,
  226. "{{ {}, {}, {}, {} }}",
  227. pattern.full_format,
  228. pattern.long_format,
  229. pattern.medium_format,
  230. pattern.short_format);
  231. }
  232. };
  233. template<>
  234. struct AK::Traits<CalendarFormat> : public GenericTraits<CalendarFormat> {
  235. static unsigned hash(CalendarFormat const& c) { return c.hash(); }
  236. };
  237. using SymbolList = Vector<StringIndexType>;
  238. struct CalendarSymbols {
  239. unsigned hash() const
  240. {
  241. auto hash = pair_int_hash(narrow_symbols, short_symbols);
  242. hash = pair_int_hash(hash, long_symbols);
  243. return hash;
  244. }
  245. bool operator==(CalendarSymbols const& other) const
  246. {
  247. return (narrow_symbols == other.narrow_symbols)
  248. && (short_symbols == other.short_symbols)
  249. && (long_symbols == other.long_symbols);
  250. }
  251. SymbolListIndexType narrow_symbols { 0 };
  252. SymbolListIndexType short_symbols { 0 };
  253. SymbolListIndexType long_symbols { 0 };
  254. };
  255. template<>
  256. struct AK::Formatter<CalendarSymbols> : Formatter<FormatString> {
  257. ErrorOr<void> format(FormatBuilder& builder, CalendarSymbols const& symbols)
  258. {
  259. return Formatter<FormatString>::format(builder,
  260. "{{ {}, {}, {} }}",
  261. symbols.narrow_symbols,
  262. symbols.short_symbols,
  263. symbols.long_symbols);
  264. }
  265. };
  266. template<>
  267. struct AK::Traits<CalendarSymbols> : public GenericTraits<CalendarSymbols> {
  268. static unsigned hash(CalendarSymbols const& c) { return c.hash(); }
  269. };
  270. using CalendarPatternList = Vector<CalendarPatternIndexType>;
  271. using CalendarRangePatternList = Vector<CalendarRangePatternIndexType>;
  272. using CalendarSymbolsList = Vector<CalendarSymbolsIndexType>;
  273. struct Calendar {
  274. unsigned hash() const
  275. {
  276. auto hash = int_hash(date_formats);
  277. hash = pair_int_hash(hash, time_formats);
  278. hash = pair_int_hash(hash, date_time_formats);
  279. hash = pair_int_hash(hash, available_formats);
  280. hash = pair_int_hash(hash, default_range_format);
  281. hash = pair_int_hash(hash, range_formats);
  282. hash = pair_int_hash(hash, range12_formats);
  283. hash = pair_int_hash(hash, symbols);
  284. return hash;
  285. }
  286. bool operator==(Calendar const& other) const
  287. {
  288. return (date_formats == other.date_formats)
  289. && (time_formats == other.time_formats)
  290. && (date_time_formats == other.date_time_formats)
  291. && (available_formats == other.available_formats)
  292. && (default_range_format == other.default_range_format)
  293. && (range_formats == other.range_formats)
  294. && (range12_formats == other.range12_formats)
  295. && (symbols == other.symbols);
  296. }
  297. CalendarFormatIndexType date_formats { 0 };
  298. CalendarFormatIndexType time_formats { 0 };
  299. CalendarFormatIndexType date_time_formats { 0 };
  300. CalendarPatternListIndexType available_formats { 0 };
  301. CalendarRangePatternIndexType default_range_format { 0 };
  302. CalendarRangePatternListIndexType range_formats { 0 };
  303. CalendarRangePatternListIndexType range12_formats { 0 };
  304. CalendarSymbolsListIndexType symbols { 0 };
  305. };
  306. template<>
  307. struct AK::Formatter<Calendar> : Formatter<FormatString> {
  308. ErrorOr<void> format(FormatBuilder& builder, Calendar const& calendar)
  309. {
  310. return Formatter<FormatString>::format(builder,
  311. "{{ {}, {}, {}, {}, {}, {}, {}, {} }}",
  312. calendar.date_formats,
  313. calendar.time_formats,
  314. calendar.date_time_formats,
  315. calendar.available_formats,
  316. calendar.default_range_format,
  317. calendar.range_formats,
  318. calendar.range12_formats,
  319. calendar.symbols);
  320. }
  321. };
  322. template<>
  323. struct AK::Traits<Calendar> : public GenericTraits<Calendar> {
  324. static unsigned hash(Calendar const& c) { return c.hash(); }
  325. };
  326. struct TimeZoneNames {
  327. unsigned hash() const
  328. {
  329. return pair_int_hash(long_name, short_name);
  330. }
  331. bool operator==(TimeZoneNames const& other) const
  332. {
  333. return (long_name == other.long_name) && (short_name == other.short_name);
  334. }
  335. StringIndexType long_name { 0 };
  336. StringIndexType short_name { 0 };
  337. };
  338. template<>
  339. struct AK::Formatter<TimeZoneNames> : Formatter<FormatString> {
  340. ErrorOr<void> format(FormatBuilder& builder, TimeZoneNames const& time_zone)
  341. {
  342. return Formatter<FormatString>::format(builder,
  343. "{{ {}, {} }}",
  344. time_zone.long_name,
  345. time_zone.short_name);
  346. }
  347. };
  348. template<>
  349. struct AK::Traits<TimeZoneNames> : public GenericTraits<TimeZoneNames> {
  350. static unsigned hash(TimeZoneNames const& t) { return t.hash(); }
  351. };
  352. struct TimeZoneFormat {
  353. unsigned hash() const
  354. {
  355. return int_hash(gmt_zero_format);
  356. }
  357. bool operator==(TimeZoneFormat const& other) const
  358. {
  359. return gmt_zero_format == other.gmt_zero_format;
  360. }
  361. StringIndexType gmt_zero_format { 0 };
  362. };
  363. template<>
  364. struct AK::Formatter<TimeZoneFormat> : Formatter<FormatString> {
  365. ErrorOr<void> format(FormatBuilder& builder, TimeZoneFormat const& time_zone_format)
  366. {
  367. return Formatter<FormatString>::format(builder, "{{ {} }}", time_zone_format.gmt_zero_format);
  368. }
  369. };
  370. template<>
  371. struct AK::Traits<TimeZoneFormat> : public GenericTraits<TimeZoneFormat> {
  372. static unsigned hash(TimeZoneFormat const& t) { return t.hash(); }
  373. };
  374. struct DayPeriod {
  375. unsigned hash() const
  376. {
  377. auto hash = int_hash(static_cast<u8>(day_period));
  378. hash = pair_int_hash(hash, begin);
  379. hash = pair_int_hash(hash, end);
  380. return hash;
  381. }
  382. bool operator==(DayPeriod const& other) const
  383. {
  384. return (day_period == other.day_period)
  385. && (begin == other.begin)
  386. && (end == other.end);
  387. }
  388. Unicode::DayPeriod day_period {};
  389. u8 begin { 0 };
  390. u8 end { 0 };
  391. };
  392. template<>
  393. struct AK::Formatter<DayPeriod> : Formatter<FormatString> {
  394. ErrorOr<void> format(FormatBuilder& builder, DayPeriod const& day_period)
  395. {
  396. return Formatter<FormatString>::format(builder,
  397. "{{ {}, {}, {} }}",
  398. static_cast<u8>(day_period.day_period),
  399. day_period.begin,
  400. day_period.end);
  401. }
  402. };
  403. template<>
  404. struct AK::Traits<DayPeriod> : public GenericTraits<DayPeriod> {
  405. static unsigned hash(DayPeriod const& d) { return d.hash(); }
  406. };
  407. using TimeZoneNamesList = Vector<TimeZoneNamesIndexType>;
  408. using DayPeriodList = Vector<DayPeriodIndexType>;
  409. using HourCycleList = Vector<Unicode::HourCycle>;
  410. template<>
  411. struct AK::Formatter<Unicode::HourCycle> : Formatter<FormatString> {
  412. ErrorOr<void> format(FormatBuilder& builder, Unicode::HourCycle hour_cycle)
  413. {
  414. return Formatter<FormatString>::format(builder, "{}", to_underlying(hour_cycle));
  415. }
  416. };
  417. struct Locale {
  418. HashMap<String, CalendarIndexType> calendars;
  419. TimeZoneNamesListIndexType time_zones { 0 };
  420. TimeZoneFormatIndexType time_zone_formats { 0 };
  421. DayPeriodListIndexType day_periods { 0 };
  422. };
  423. struct UnicodeLocaleData {
  424. UniqueStringStorage<StringIndexType> unique_strings;
  425. UniqueStorage<CalendarPattern, CalendarPatternIndexType> unique_patterns;
  426. UniqueStorage<CalendarPatternList, CalendarPatternListIndexType> unique_pattern_lists;
  427. UniqueStorage<CalendarRangePattern, CalendarRangePatternIndexType> unique_range_patterns;
  428. UniqueStorage<CalendarRangePatternList, CalendarRangePatternListIndexType> unique_range_pattern_lists;
  429. UniqueStorage<CalendarFormat, CalendarFormatIndexType> unique_formats;
  430. UniqueStorage<SymbolList, SymbolListIndexType> unique_symbol_lists;
  431. UniqueStorage<CalendarSymbols, CalendarSymbolsIndexType> unique_calendar_symbols;
  432. UniqueStorage<CalendarSymbolsList, CalendarSymbolsListIndexType> unique_calendar_symbols_lists;
  433. UniqueStorage<Calendar, CalendarIndexType> unique_calendars;
  434. UniqueStorage<TimeZoneNames, TimeZoneNamesIndexType> unique_time_zones;
  435. UniqueStorage<TimeZoneNamesList, TimeZoneNamesListIndexType> unique_time_zone_lists;
  436. UniqueStorage<TimeZoneFormat, TimeZoneFormatIndexType> unique_time_zone_formats;
  437. UniqueStorage<DayPeriod, DayPeriodIndexType> unique_day_periods;
  438. UniqueStorage<DayPeriodList, DayPeriodListIndexType> unique_day_period_lists;
  439. UniqueStorage<HourCycleList, HourCycleListIndexType> unique_hour_cycle_lists;
  440. HashMap<String, Locale> locales;
  441. HashMap<String, HourCycleListIndexType> hour_cycles;
  442. Vector<String> hour_cycle_regions;
  443. HashMap<String, Vector<TimeZone::TimeZone>> meta_zones;
  444. Vector<String> time_zones { "UTC"sv };
  445. Vector<String> calendars;
  446. Vector<Alias> calendar_aliases {
  447. // FIXME: Aliases should come from BCP47. See: https://unicode-org.atlassian.net/browse/CLDR-15158
  448. { "gregorian"sv, "gregory"sv },
  449. };
  450. };
  451. static Optional<Unicode::DayPeriod> day_period_from_string(StringView day_period)
  452. {
  453. if (day_period == "am"sv)
  454. return Unicode::DayPeriod::AM;
  455. if (day_period == "pm"sv)
  456. return Unicode::DayPeriod::PM;
  457. if (day_period == "morning1"sv)
  458. return Unicode::DayPeriod::Morning1;
  459. if (day_period == "morning2"sv)
  460. return Unicode::DayPeriod::Morning2;
  461. if (day_period == "afternoon1"sv)
  462. return Unicode::DayPeriod::Afternoon1;
  463. if (day_period == "afternoon2"sv)
  464. return Unicode::DayPeriod::Afternoon2;
  465. if (day_period == "evening1"sv)
  466. return Unicode::DayPeriod::Evening1;
  467. if (day_period == "evening2"sv)
  468. return Unicode::DayPeriod::Evening2;
  469. if (day_period == "night1"sv)
  470. return Unicode::DayPeriod::Night1;
  471. if (day_period == "night2"sv)
  472. return Unicode::DayPeriod::Night2;
  473. return {};
  474. };
  475. static ErrorOr<void> parse_hour_cycles(String core_path, UnicodeLocaleData& locale_data)
  476. {
  477. // https://unicode.org/reports/tr35/tr35-dates.html#Time_Data
  478. LexicalPath time_data_path(move(core_path));
  479. time_data_path = time_data_path.append("supplemental"sv);
  480. time_data_path = time_data_path.append("timeData.json"sv);
  481. auto time_data_file = TRY(Core::File::open(time_data_path.string(), Core::OpenMode::ReadOnly));
  482. auto time_data = TRY(JsonValue::from_string(time_data_file->read_all()));
  483. auto const& supplemental_object = time_data.as_object().get("supplemental"sv);
  484. auto const& time_data_object = supplemental_object.as_object().get("timeData"sv);
  485. auto parse_hour_cycle = [](StringView hour_cycle) -> Optional<Unicode::HourCycle> {
  486. if (hour_cycle == "h"sv)
  487. return Unicode::HourCycle::H12;
  488. if (hour_cycle == "H"sv)
  489. return Unicode::HourCycle::H23;
  490. if (hour_cycle == "K"sv)
  491. return Unicode::HourCycle::H11;
  492. if (hour_cycle == "k"sv)
  493. return Unicode::HourCycle::H24;
  494. return {};
  495. };
  496. time_data_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
  497. auto allowed_hour_cycles_string = value.as_object().get("_allowed"sv).as_string();
  498. auto allowed_hour_cycles = allowed_hour_cycles_string.split_view(' ');
  499. Vector<Unicode::HourCycle> hour_cycles;
  500. for (auto allowed_hour_cycle : allowed_hour_cycles) {
  501. if (auto hour_cycle = parse_hour_cycle(allowed_hour_cycle); hour_cycle.has_value())
  502. hour_cycles.append(*hour_cycle);
  503. }
  504. auto hour_cycles_index = locale_data.unique_hour_cycle_lists.ensure(move(hour_cycles));
  505. locale_data.hour_cycles.set(key, hour_cycles_index);
  506. if (!locale_data.hour_cycle_regions.contains_slow(key))
  507. locale_data.hour_cycle_regions.append(key);
  508. });
  509. return {};
  510. };
  511. static ErrorOr<void> parse_meta_zones(String core_path, UnicodeLocaleData& locale_data)
  512. {
  513. // https://unicode.org/reports/tr35/tr35-dates.html#Metazones
  514. LexicalPath meta_zone_path(move(core_path));
  515. meta_zone_path = meta_zone_path.append("supplemental"sv);
  516. meta_zone_path = meta_zone_path.append("metaZones.json"sv);
  517. auto meta_zone_file = TRY(Core::File::open(meta_zone_path.string(), Core::OpenMode::ReadOnly));
  518. auto meta_zone = TRY(JsonValue::from_string(meta_zone_file->read_all()));
  519. auto const& supplemental_object = meta_zone.as_object().get("supplemental"sv);
  520. auto const& meta_zone_object = supplemental_object.as_object().get("metaZones"sv);
  521. auto const& meta_zone_array = meta_zone_object.as_object().get("metazones"sv);
  522. meta_zone_array.as_array().for_each([&](JsonValue const& value) {
  523. auto const& mapping = value.as_object().get("mapZone"sv);
  524. auto const& meta_zone = mapping.as_object().get("_other"sv);
  525. auto const& golden_zone = mapping.as_object().get("_type"sv);
  526. if (auto time_zone = TimeZone::time_zone_from_string(golden_zone.as_string()); time_zone.has_value()) {
  527. auto& golden_zones = locale_data.meta_zones.ensure(meta_zone.as_string());
  528. golden_zones.append(*time_zone);
  529. }
  530. });
  531. // UTC does not appear in metaZones.json. Define it for convenience so other parsers don't need to check for its existence.
  532. if (auto time_zone = TimeZone::time_zone_from_string("UTC"sv); time_zone.has_value())
  533. locale_data.meta_zones.set("UTC"sv, { *time_zone });
  534. return {};
  535. };
  536. static constexpr auto is_char(char ch)
  537. {
  538. return [ch](auto c) { return c == ch; };
  539. }
  540. // For patterns that are 12-hour aware, we need to generate two patterns: one with the day period
  541. // (e.g. {ampm}) in the pattern, and one without the day period. We need to take care to remove
  542. // extra spaces around the day period. Some example expected removals:
  543. //
  544. // "{hour}:{minute} {ampm}" becomes "{hour}:{minute}" (remove the space before {ampm})
  545. // "{ampm} {hour}" becomes "{hour}" (remove the space after {ampm})
  546. // "{hour}:{minute} {ampm} {timeZoneName}" becomes "{hour}:{minute} {timeZoneName}" (remove one of the spaces around {ampm})
  547. static String remove_period_from_pattern(String pattern)
  548. {
  549. for (auto remove : AK::Array { "({ampm})"sv, "{ampm}"sv, "({dayPeriod})"sv, "{dayPeriod}"sv }) {
  550. auto index = pattern.find(remove);
  551. if (!index.has_value())
  552. continue;
  553. constexpr u32 space = ' ';
  554. constexpr u32 open = '{';
  555. constexpr u32 close = '}';
  556. Utf8View utf8_pattern { pattern };
  557. Optional<u32> before_removal;
  558. Optional<u32> after_removal;
  559. for (auto it = utf8_pattern.begin(); utf8_pattern.byte_offset_of(it) < *index; ++it)
  560. before_removal = *it;
  561. if (auto it = utf8_pattern.iterator_at_byte_offset(*index + remove.length()); it != utf8_pattern.end())
  562. after_removal = *it;
  563. if ((before_removal == space) && (after_removal != open)) {
  564. pattern = String::formatted("{}{}",
  565. pattern.substring_view(0, *index - 1),
  566. pattern.substring_view(*index + remove.length()));
  567. } else if ((after_removal == space) && (before_removal != close)) {
  568. pattern = String::formatted("{}{}",
  569. pattern.substring_view(0, *index),
  570. pattern.substring_view(*index + remove.length() + 1));
  571. } else {
  572. pattern = String::formatted("{}{}",
  573. pattern.substring_view(0, *index),
  574. pattern.substring_view(*index + remove.length()));
  575. }
  576. }
  577. return pattern;
  578. }
  579. static Optional<CalendarPattern> parse_date_time_pattern_raw(String pattern, String skeleton, UnicodeLocaleData& locale_data)
  580. {
  581. // https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  582. using Unicode::CalendarPatternStyle;
  583. CalendarPattern format {};
  584. if (!skeleton.is_empty())
  585. format.skeleton_index = locale_data.unique_strings.ensure(move(skeleton));
  586. GenericLexer lexer { pattern };
  587. StringBuilder builder;
  588. bool hour12 { false };
  589. while (!lexer.is_eof()) {
  590. // Literal strings enclosed by quotes are to be appended to the pattern as-is without further
  591. // processing (this just avoids conflicts with the patterns below).
  592. if (lexer.next_is(is_quote)) {
  593. builder.append(lexer.consume_quoted_string());
  594. continue;
  595. }
  596. auto starting_char = lexer.peek();
  597. auto segment = lexer.consume_while([&](char ch) { return ch == starting_char; });
  598. // Era
  599. if (all_of(segment, is_char('G'))) {
  600. builder.append("{era}");
  601. if (segment.length() <= 3)
  602. format.era = CalendarPatternStyle::Short;
  603. else if (segment.length() == 4)
  604. format.era = CalendarPatternStyle::Long;
  605. else
  606. format.era = CalendarPatternStyle::Narrow;
  607. }
  608. // Year
  609. else if (all_of(segment, is_any_of("yYuUr"sv))) {
  610. builder.append("{year}");
  611. if (segment.length() == 2)
  612. format.year = CalendarPatternStyle::TwoDigit;
  613. else
  614. format.year = CalendarPatternStyle::Numeric;
  615. }
  616. // Quarter
  617. else if (all_of(segment, is_any_of("qQ"sv))) {
  618. // Intl.DateTimeFormat does not support quarter formatting, so drop these patterns.
  619. return {};
  620. }
  621. // Month
  622. else if (all_of(segment, is_any_of("ML"sv))) {
  623. builder.append("{month}");
  624. if (segment.length() == 1)
  625. format.month = CalendarPatternStyle::Numeric;
  626. else if (segment.length() == 2)
  627. format.month = CalendarPatternStyle::TwoDigit;
  628. else if (segment.length() == 3)
  629. format.month = CalendarPatternStyle::Short;
  630. else if (segment.length() == 4)
  631. format.month = CalendarPatternStyle::Long;
  632. else if (segment.length() == 5)
  633. format.month = CalendarPatternStyle::Narrow;
  634. } else if (all_of(segment, is_char('l'))) {
  635. // Using 'l' for month formatting is deprecated by TR-35, ensure it is not used.
  636. return {};
  637. }
  638. // Week
  639. else if (all_of(segment, is_any_of("wW"sv))) {
  640. // Intl.DateTimeFormat does not support week formatting, so drop these patterns.
  641. return {};
  642. }
  643. // Day
  644. else if (all_of(segment, is_char('d'))) {
  645. builder.append("{day}");
  646. if (segment.length() == 1)
  647. format.day = CalendarPatternStyle::Numeric;
  648. else
  649. format.day = CalendarPatternStyle::TwoDigit;
  650. } else if (all_of(segment, is_any_of("DFg"sv))) {
  651. builder.append("{day}");
  652. format.day = CalendarPatternStyle::Numeric;
  653. }
  654. // Weekday
  655. else if (all_of(segment, is_char('E'))) {
  656. builder.append("{weekday}");
  657. if (segment.length() == 4)
  658. format.weekday = CalendarPatternStyle::Long;
  659. else if (segment.length() == 5)
  660. format.weekday = CalendarPatternStyle::Narrow;
  661. else
  662. format.weekday = CalendarPatternStyle::Short;
  663. } else if (all_of(segment, is_any_of("ec"sv))) {
  664. builder.append("{weekday}");
  665. // TR-35 defines "e", "c", and "cc" as as numeric, and "ee" as 2-digit, but those
  666. // pattern styles are not supported by Intl.DateTimeFormat.
  667. if (segment.length() <= 2)
  668. return {};
  669. if (segment.length() == 4)
  670. format.weekday = CalendarPatternStyle::Long;
  671. else if (segment.length() == 5)
  672. format.weekday = CalendarPatternStyle::Narrow;
  673. else
  674. format.weekday = CalendarPatternStyle::Short;
  675. }
  676. // Period
  677. else if (all_of(segment, is_any_of("ab"sv))) {
  678. builder.append("{ampm}");
  679. hour12 = true;
  680. } else if (all_of(segment, is_char('B'))) {
  681. builder.append("{dayPeriod}");
  682. hour12 = true;
  683. if (segment.length() == 4)
  684. format.day_period = CalendarPatternStyle::Long;
  685. else if (segment.length() == 5)
  686. format.day_period = CalendarPatternStyle::Narrow;
  687. else
  688. format.day_period = CalendarPatternStyle::Short;
  689. }
  690. // Hour
  691. else if (all_of(segment, is_any_of("hHKk"sv))) {
  692. builder.append("{hour}");
  693. if ((segment[0] == 'h') || (segment[0] == 'K'))
  694. hour12 = true;
  695. if (segment.length() == 1)
  696. format.hour = CalendarPatternStyle::Numeric;
  697. else
  698. format.hour = CalendarPatternStyle::TwoDigit;
  699. } else if (all_of(segment, is_any_of("jJC"sv))) {
  700. // TR-35 indicates these should not be used.
  701. return {};
  702. }
  703. // Minute
  704. else if (all_of(segment, is_char('m'))) {
  705. builder.append("{minute}");
  706. if (segment.length() == 1)
  707. format.minute = CalendarPatternStyle::Numeric;
  708. else
  709. format.minute = CalendarPatternStyle::TwoDigit;
  710. }
  711. // Second
  712. else if (all_of(segment, is_char('s'))) {
  713. builder.append("{second}");
  714. if (segment.length() == 1)
  715. format.second = CalendarPatternStyle::Numeric;
  716. else
  717. format.second = CalendarPatternStyle::TwoDigit;
  718. } else if (all_of(segment, is_char('S'))) {
  719. builder.append("{fractionalSecondDigits}");
  720. VERIFY(segment.length() <= 3);
  721. format.fractional_second_digits = static_cast<u8>(segment.length());
  722. } else if (all_of(segment, is_char('A'))) {
  723. // Intl.DateTimeFormat does not support millisecond formatting, so drop these patterns.
  724. return {};
  725. }
  726. // Zone
  727. else if (all_of(segment, is_any_of("zV"sv))) {
  728. builder.append("{timeZoneName}");
  729. if (segment.length() < 4)
  730. format.time_zone_name = CalendarPatternStyle::Short;
  731. else
  732. format.time_zone_name = CalendarPatternStyle::Long;
  733. } else if (all_of(segment, is_any_of("ZOXx"sv))) {
  734. builder.append("{timeZoneName}");
  735. if (segment.length() < 4)
  736. format.time_zone_name = CalendarPatternStyle::ShortOffset;
  737. else
  738. format.time_zone_name = CalendarPatternStyle::LongOffset;
  739. } else if (all_of(segment, is_char('v'))) {
  740. builder.append("{timeZoneName}");
  741. if (segment.length() < 4)
  742. format.time_zone_name = CalendarPatternStyle::ShortGeneric;
  743. else
  744. format.time_zone_name = CalendarPatternStyle::LongGeneric;
  745. }
  746. // Non-patterns
  747. else {
  748. builder.append(segment);
  749. }
  750. }
  751. pattern = builder.build();
  752. if (hour12) {
  753. format.pattern = remove_period_from_pattern(pattern);
  754. format.pattern12 = move(pattern);
  755. } else {
  756. format.pattern = move(pattern);
  757. }
  758. return format;
  759. }
  760. static Optional<CalendarPatternIndexType> parse_date_time_pattern(String pattern, String skeleton, UnicodeLocaleData& locale_data)
  761. {
  762. auto format = parse_date_time_pattern_raw(move(pattern), move(skeleton), locale_data);
  763. if (!format.has_value())
  764. return {};
  765. format->pattern_index = locale_data.unique_strings.ensure(move(format->pattern));
  766. if (format->pattern12.has_value())
  767. format->pattern12_index = locale_data.unique_strings.ensure(format->pattern12.release_value());
  768. return locale_data.unique_patterns.ensure(format.release_value());
  769. }
  770. template<typename... Chars>
  771. static constexpr bool char_is_one_of(char ch, Chars&&... chars)
  772. {
  773. return ((ch == chars) || ...);
  774. }
  775. static void parse_interval_patterns(Calendar& calendar, JsonObject const& interval_formats_object, UnicodeLocaleData& locale_data)
  776. {
  777. // https://unicode.org/reports/tr35/tr35-dates.html#intervalFormats
  778. CalendarRangePatternList range_formats {};
  779. CalendarRangePatternList range12_formats {};
  780. auto name_of_field = [&](char field) {
  781. if (char_is_one_of(field, 'G'))
  782. return Unicode::CalendarRangePattern::Field::Era;
  783. if (char_is_one_of(field, 'y', 'Y', 'u', 'U', 'r'))
  784. return Unicode::CalendarRangePattern::Field::Year;
  785. if (char_is_one_of(field, 'M', 'L'))
  786. return Unicode::CalendarRangePattern::Field::Month;
  787. if (char_is_one_of(field, 'd', 'D', 'F', 'g'))
  788. return Unicode::CalendarRangePattern::Field::Day;
  789. if (char_is_one_of(field, 'a', 'b'))
  790. return Unicode::CalendarRangePattern::Field::AmPm;
  791. if (char_is_one_of(field, 'B'))
  792. return Unicode::CalendarRangePattern::Field::DayPeriod;
  793. if (char_is_one_of(field, 'h', 'H', 'K', 'k'))
  794. return Unicode::CalendarRangePattern::Field::Hour;
  795. if (char_is_one_of(field, 'm'))
  796. return Unicode::CalendarRangePattern::Field::Minute;
  797. if (char_is_one_of(field, 's'))
  798. return Unicode::CalendarRangePattern::Field::Second;
  799. if (char_is_one_of(field, 'S'))
  800. return Unicode::CalendarRangePattern::Field::FractionalSecondDigits;
  801. VERIFY_NOT_REACHED();
  802. };
  803. auto split_default_range_pattern = [&](auto skeleton, auto const& pattern) {
  804. auto start_range_end = pattern.find('}').value() + 1;
  805. auto end_range_begin = pattern.find_last('{').value();
  806. auto start_range = pattern.substring_view(0, start_range_end);
  807. auto separator = pattern.substring_view(start_range_end, end_range_begin - start_range_end);
  808. auto end_range = pattern.substring_view(end_range_begin);
  809. CalendarRangePattern format {};
  810. format.skeleton_index = locale_data.unique_strings.ensure(skeleton);
  811. format.start_range = locale_data.unique_strings.ensure(start_range);
  812. format.separator = locale_data.unique_strings.ensure(separator);
  813. format.end_range = locale_data.unique_strings.ensure(end_range);
  814. return format;
  815. };
  816. auto split_range_pattern = [&](auto skeleton, auto field, auto const& pattern, auto const& parsed_fields) {
  817. HashMap<StringView, size_t> partitions;
  818. StringView last_partition;
  819. auto begin_index = pattern.find('{');
  820. size_t end_index = 0;
  821. while (begin_index.has_value()) {
  822. end_index = pattern.find('}', *begin_index).value();
  823. auto partition = pattern.substring_view(*begin_index, end_index - *begin_index);
  824. if (partitions.contains(partition))
  825. break;
  826. partitions.set(partition, *begin_index);
  827. last_partition = partition;
  828. begin_index = pattern.find('{', end_index + 1);
  829. }
  830. VERIFY(begin_index.has_value() && !last_partition.is_empty());
  831. auto start_range_end = partitions.get(last_partition).value() + last_partition.length() + 1;
  832. auto start_range = pattern.substring_view(0, start_range_end);
  833. auto separator = pattern.substring_view(start_range_end, *begin_index - start_range_end);
  834. auto end_range = pattern.substring_view(*begin_index);
  835. CalendarRangePattern format {};
  836. format.skeleton_index = locale_data.unique_strings.ensure(skeleton);
  837. format.field = field;
  838. format.start_range = locale_data.unique_strings.ensure(start_range);
  839. format.separator = locale_data.unique_strings.ensure(separator);
  840. format.end_range = locale_data.unique_strings.ensure(end_range);
  841. format.for_each_calendar_field_zipped_with(parsed_fields, [](auto& format_field, auto const& parsed_field, auto) {
  842. format_field = parsed_field;
  843. });
  844. return format;
  845. };
  846. interval_formats_object.for_each_member([&](auto const& skeleton, auto const& value) {
  847. if (skeleton == "intervalFormatFallback"sv) {
  848. auto range_format = split_default_range_pattern(skeleton, value.as_string());
  849. calendar.default_range_format = locale_data.unique_range_patterns.ensure(move(range_format));
  850. return;
  851. }
  852. value.as_object().for_each_member([&](auto const& field, auto const& pattern) {
  853. if (field.ends_with("alt-variant"sv))
  854. return;
  855. VERIFY(field.length() == 1);
  856. auto name = name_of_field(field[0]);
  857. auto format = parse_date_time_pattern_raw(pattern.as_string(), skeleton, locale_data).release_value();
  858. auto range_format = split_range_pattern(skeleton, name, format.pattern, format);
  859. range_formats.append(locale_data.unique_range_patterns.ensure(move(range_format)));
  860. if (format.pattern12.has_value()) {
  861. auto range12_pattern = split_range_pattern(skeleton, name, *format.pattern12, format);
  862. range12_formats.append(locale_data.unique_range_patterns.ensure(move(range12_pattern)));
  863. } else {
  864. range12_formats.append(range_formats.last());
  865. }
  866. });
  867. });
  868. calendar.range_formats = locale_data.unique_range_pattern_lists.ensure(move(range_formats));
  869. calendar.range12_formats = locale_data.unique_range_pattern_lists.ensure(move(range12_formats));
  870. }
  871. static void generate_missing_patterns(Calendar& calendar, CalendarPatternList& formats, Vector<CalendarPattern> date_formats, Vector<CalendarPattern> time_formats, UnicodeLocaleData& locale_data)
  872. {
  873. // https://unicode.org/reports/tr35/tr35-dates.html#Missing_Skeleton_Fields
  874. auto replace_pattern = [&](auto format, auto time_format, auto date_format) {
  875. auto pattern = locale_data.unique_strings.get(format);
  876. auto time_pattern = locale_data.unique_strings.get(time_format);
  877. auto date_pattern = locale_data.unique_strings.get(date_format);
  878. auto new_pattern = pattern.replace("{0}", time_pattern).replace("{1}", date_pattern);
  879. return locale_data.unique_strings.ensure(move(new_pattern));
  880. };
  881. auto inject_fractional_second_digits = [&](auto format) {
  882. auto pattern = locale_data.unique_strings.get(format);
  883. auto new_pattern = pattern.replace("{second}"sv, "{second}{decimal}{fractionalSecondDigits}"sv);
  884. return locale_data.unique_strings.ensure(move(new_pattern));
  885. };
  886. auto append_if_unique = [&](auto format) {
  887. auto format_index = locale_data.unique_patterns.ensure(move(format));
  888. if (!formats.contains_slow(format_index))
  889. formats.append(format_index);
  890. };
  891. Vector<CalendarPattern> time_formats_with_fractional_second_digits;
  892. for (auto const& format : date_formats)
  893. append_if_unique(format);
  894. for (auto const& format : time_formats) {
  895. append_if_unique(format);
  896. if (format.second.has_value() && !format.fractional_second_digits.has_value()) {
  897. auto new_format = format;
  898. new_format.fractional_second_digits = 2;
  899. new_format.pattern_index = inject_fractional_second_digits(new_format.pattern_index);
  900. if (new_format.pattern12_index != 0)
  901. new_format.pattern12_index = inject_fractional_second_digits(new_format.pattern12_index);
  902. time_formats_with_fractional_second_digits.append(new_format);
  903. append_if_unique(move(new_format));
  904. }
  905. }
  906. time_formats.extend(move(time_formats_with_fractional_second_digits));
  907. for (auto const& date_format : date_formats) {
  908. auto const& date_time_formats = locale_data.unique_formats.get(calendar.date_time_formats);
  909. CalendarPatternIndexType date_time_format_index = 0;
  910. if (date_format.month == Unicode::CalendarPatternStyle::Long) {
  911. if (date_format.weekday.has_value())
  912. date_time_format_index = date_time_formats.full_format;
  913. else
  914. date_time_format_index = date_time_formats.long_format;
  915. } else if (date_format.month == Unicode::CalendarPatternStyle::Short) {
  916. date_time_format_index = date_time_formats.medium_format;
  917. } else {
  918. date_time_format_index = date_time_formats.short_format;
  919. }
  920. for (auto const& time_format : time_formats) {
  921. auto format = locale_data.unique_patterns.get(date_time_format_index);
  922. if (time_format.pattern12_index != 0)
  923. format.pattern12_index = replace_pattern(format.pattern_index, time_format.pattern12_index, date_format.pattern_index);
  924. format.pattern_index = replace_pattern(format.pattern_index, time_format.pattern_index, date_format.pattern_index);
  925. format.for_each_calendar_field_zipped_with(date_format, [](auto& field, auto const& date_field, auto) {
  926. if (date_field.has_value())
  927. field = date_field;
  928. });
  929. format.for_each_calendar_field_zipped_with(time_format, [](auto& field, auto const& time_field, auto) {
  930. if (time_field.has_value())
  931. field = time_field;
  932. });
  933. append_if_unique(move(format));
  934. }
  935. }
  936. }
  937. static void parse_calendar_symbols(Calendar& calendar, JsonObject const& calendar_object, UnicodeLocaleData& locale_data)
  938. {
  939. auto create_symbol_lists = [](size_t size) {
  940. SymbolList narrow_symbol_list;
  941. SymbolList short_symbol_list;
  942. SymbolList long_symbol_list;
  943. narrow_symbol_list.resize(size);
  944. short_symbol_list.resize(size);
  945. long_symbol_list.resize(size);
  946. return Array<SymbolList, 3> { {
  947. move(narrow_symbol_list),
  948. move(short_symbol_list),
  949. move(long_symbol_list),
  950. } };
  951. };
  952. CalendarSymbolsList symbols_list {};
  953. auto store_symbol_lists = [&](auto symbol, auto symbol_lists) {
  954. auto symbol_index = to_underlying(symbol);
  955. if (symbol_index >= symbols_list.size())
  956. symbols_list.resize(symbol_index + 1);
  957. CalendarSymbols symbols {};
  958. symbols.narrow_symbols = locale_data.unique_symbol_lists.ensure(move(symbol_lists[0]));
  959. symbols.short_symbols = locale_data.unique_symbol_lists.ensure(move(symbol_lists[1]));
  960. symbols.long_symbols = locale_data.unique_symbol_lists.ensure(move(symbol_lists[2]));
  961. auto calendar_symbols_index = locale_data.unique_calendar_symbols.ensure(move(symbols));
  962. symbols_list[symbol_index] = calendar_symbols_index;
  963. };
  964. auto parse_era_symbols = [&](auto const& symbols_object) {
  965. auto const& narrow_symbols = symbols_object.get("eraNarrow"sv).as_object();
  966. auto const& short_symbols = symbols_object.get("eraAbbr"sv).as_object();
  967. auto const& long_symbols = symbols_object.get("eraNames"sv).as_object();
  968. auto symbol_lists = create_symbol_lists(2);
  969. auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
  970. if (auto key_index = key.to_uint(); key_index.has_value())
  971. symbols[*key_index] = locale_data.unique_strings.ensure(move(symbol));
  972. };
  973. narrow_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  974. append_symbol(symbol_lists[0], key, value.as_string());
  975. });
  976. short_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  977. append_symbol(symbol_lists[1], key, value.as_string());
  978. });
  979. long_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  980. append_symbol(symbol_lists[2], key, value.as_string());
  981. });
  982. store_symbol_lists(Unicode::CalendarSymbol::Era, move(symbol_lists));
  983. };
  984. auto parse_month_symbols = [&](auto const& symbols_object) {
  985. auto const& narrow_symbols = symbols_object.get("narrow"sv).as_object();
  986. auto const& short_symbols = symbols_object.get("abbreviated"sv).as_object();
  987. auto const& long_symbols = symbols_object.get("wide"sv).as_object();
  988. auto symbol_lists = create_symbol_lists(12);
  989. auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
  990. auto key_index = key.to_uint().value() - 1;
  991. symbols[key_index] = locale_data.unique_strings.ensure(move(symbol));
  992. };
  993. narrow_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  994. append_symbol(symbol_lists[0], key, value.as_string());
  995. });
  996. short_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  997. append_symbol(symbol_lists[1], key, value.as_string());
  998. });
  999. long_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1000. append_symbol(symbol_lists[2], key, value.as_string());
  1001. });
  1002. store_symbol_lists(Unicode::CalendarSymbol::Month, move(symbol_lists));
  1003. };
  1004. auto parse_weekday_symbols = [&](auto const& symbols_object) {
  1005. auto const& narrow_symbols = symbols_object.get("narrow"sv).as_object();
  1006. auto const& short_symbols = symbols_object.get("abbreviated"sv).as_object();
  1007. auto const& long_symbols = symbols_object.get("wide"sv).as_object();
  1008. auto symbol_lists = create_symbol_lists(7);
  1009. auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
  1010. if (key == "sun"sv)
  1011. symbols[to_underlying(Unicode::Weekday::Sunday)] = locale_data.unique_strings.ensure(move(symbol));
  1012. else if (key == "mon"sv)
  1013. symbols[to_underlying(Unicode::Weekday::Monday)] = locale_data.unique_strings.ensure(move(symbol));
  1014. else if (key == "tue"sv)
  1015. symbols[to_underlying(Unicode::Weekday::Tuesday)] = locale_data.unique_strings.ensure(move(symbol));
  1016. else if (key == "wed"sv)
  1017. symbols[to_underlying(Unicode::Weekday::Wednesday)] = locale_data.unique_strings.ensure(move(symbol));
  1018. else if (key == "thu"sv)
  1019. symbols[to_underlying(Unicode::Weekday::Thursday)] = locale_data.unique_strings.ensure(move(symbol));
  1020. else if (key == "fri"sv)
  1021. symbols[to_underlying(Unicode::Weekday::Friday)] = locale_data.unique_strings.ensure(move(symbol));
  1022. else if (key == "sat"sv)
  1023. symbols[to_underlying(Unicode::Weekday::Saturday)] = locale_data.unique_strings.ensure(move(symbol));
  1024. };
  1025. narrow_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1026. append_symbol(symbol_lists[0], key, value.as_string());
  1027. });
  1028. short_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1029. append_symbol(symbol_lists[1], key, value.as_string());
  1030. });
  1031. long_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1032. append_symbol(symbol_lists[2], key, value.as_string());
  1033. });
  1034. store_symbol_lists(Unicode::CalendarSymbol::Weekday, move(symbol_lists));
  1035. };
  1036. auto parse_day_period_symbols = [&](auto const& symbols_object) {
  1037. auto const& narrow_symbols = symbols_object.get("narrow"sv).as_object();
  1038. auto const& short_symbols = symbols_object.get("abbreviated"sv).as_object();
  1039. auto const& long_symbols = symbols_object.get("wide"sv).as_object();
  1040. auto symbol_lists = create_symbol_lists(10);
  1041. auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
  1042. if (auto day_period = day_period_from_string(key); day_period.has_value())
  1043. symbols[to_underlying(*day_period)] = locale_data.unique_strings.ensure(move(symbol));
  1044. };
  1045. narrow_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1046. append_symbol(symbol_lists[0], key, value.as_string());
  1047. });
  1048. short_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1049. append_symbol(symbol_lists[1], key, value.as_string());
  1050. });
  1051. long_symbols.for_each_member([&](auto const& key, JsonValue const& value) {
  1052. append_symbol(symbol_lists[2], key, value.as_string());
  1053. });
  1054. store_symbol_lists(Unicode::CalendarSymbol::DayPeriod, move(symbol_lists));
  1055. };
  1056. parse_era_symbols(calendar_object.get("eras"sv).as_object());
  1057. parse_month_symbols(calendar_object.get("months"sv).as_object().get("format"sv).as_object());
  1058. parse_weekday_symbols(calendar_object.get("days"sv).as_object().get("format"sv).as_object());
  1059. parse_day_period_symbols(calendar_object.get("dayPeriods"sv).as_object().get("format"sv).as_object());
  1060. calendar.symbols = locale_data.unique_calendar_symbols_lists.ensure(move(symbols_list));
  1061. }
  1062. static ErrorOr<void> parse_calendars(String locale_calendars_path, UnicodeLocaleData& locale_data, Locale& locale)
  1063. {
  1064. LexicalPath calendars_path(move(locale_calendars_path));
  1065. if (!calendars_path.basename().starts_with("ca-"sv))
  1066. return {};
  1067. auto calendars_file = TRY(Core::File::open(calendars_path.string(), Core::OpenMode::ReadOnly));
  1068. auto calendars = TRY(JsonValue::from_string(calendars_file->read_all()));
  1069. auto const& main_object = calendars.as_object().get("main"sv);
  1070. auto const& locale_object = main_object.as_object().get(calendars_path.parent().basename());
  1071. auto const& dates_object = locale_object.as_object().get("dates"sv);
  1072. auto const& calendars_object = dates_object.as_object().get("calendars"sv);
  1073. auto parse_patterns = [&](auto const& patterns_object, auto const& skeletons_object, Vector<CalendarPattern>* patterns) {
  1074. auto parse_pattern = [&](auto name) {
  1075. auto format = patterns_object.get(name);
  1076. auto skeleton = skeletons_object.get(name);
  1077. auto format_index = parse_date_time_pattern(format.as_string(), skeleton.as_string_or(String::empty()), locale_data).value();
  1078. if (patterns)
  1079. patterns->append(locale_data.unique_patterns.get(format_index));
  1080. return format_index;
  1081. };
  1082. CalendarFormat formats {};
  1083. formats.full_format = parse_pattern("full"sv);
  1084. formats.long_format = parse_pattern("long"sv);
  1085. formats.medium_format = parse_pattern("medium"sv);
  1086. formats.short_format = parse_pattern("short"sv);
  1087. return locale_data.unique_formats.ensure(move(formats));
  1088. };
  1089. calendars_object.as_object().for_each_member([&](auto const& calendar_name, JsonValue const& value) {
  1090. // The generic calendar is not a supported Unicode calendar key, so skip it:
  1091. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar#unicode_calendar_keys
  1092. if (calendar_name == "generic"sv)
  1093. return;
  1094. Calendar calendar {};
  1095. CalendarPatternList available_formats {};
  1096. if (!locale_data.calendars.contains_slow(calendar_name))
  1097. locale_data.calendars.append(calendar_name);
  1098. Vector<CalendarPattern> date_formats;
  1099. Vector<CalendarPattern> time_formats;
  1100. auto const& date_formats_object = value.as_object().get("dateFormats"sv);
  1101. auto const& date_skeletons_object = value.as_object().get("dateSkeletons"sv);
  1102. calendar.date_formats = parse_patterns(date_formats_object.as_object(), date_skeletons_object.as_object(), &date_formats);
  1103. auto const& time_formats_object = value.as_object().get("timeFormats"sv);
  1104. auto const& time_skeletons_object = value.as_object().get("timeSkeletons"sv);
  1105. calendar.time_formats = parse_patterns(time_formats_object.as_object(), time_skeletons_object.as_object(), &time_formats);
  1106. auto const& date_time_formats_object = value.as_object().get("dateTimeFormats"sv);
  1107. calendar.date_time_formats = parse_patterns(date_time_formats_object.as_object(), JsonObject {}, nullptr);
  1108. auto const& available_formats_object = date_time_formats_object.as_object().get("availableFormats"sv);
  1109. available_formats_object.as_object().for_each_member([&](auto const& skeleton, JsonValue const& pattern) {
  1110. auto pattern_index = parse_date_time_pattern(pattern.as_string(), skeleton, locale_data);
  1111. if (!pattern_index.has_value())
  1112. return;
  1113. auto const& format = locale_data.unique_patterns.get(*pattern_index);
  1114. if (format.contains_only_date_fields())
  1115. date_formats.append(format);
  1116. else if (format.contains_only_time_fields())
  1117. time_formats.append(format);
  1118. if (!available_formats.contains_slow(*pattern_index))
  1119. available_formats.append(*pattern_index);
  1120. });
  1121. auto const& interval_formats_object = date_time_formats_object.as_object().get("intervalFormats"sv);
  1122. parse_interval_patterns(calendar, interval_formats_object.as_object(), locale_data);
  1123. generate_missing_patterns(calendar, available_formats, move(date_formats), move(time_formats), locale_data);
  1124. parse_calendar_symbols(calendar, value.as_object(), locale_data);
  1125. calendar.available_formats = locale_data.unique_pattern_lists.ensure(move(available_formats));
  1126. locale.calendars.set(calendar_name, locale_data.unique_calendars.ensure(move(calendar)));
  1127. });
  1128. return {};
  1129. }
  1130. static ErrorOr<void> parse_time_zone_names(String locale_time_zone_names_path, UnicodeLocaleData& locale_data, Locale& locale)
  1131. {
  1132. LexicalPath time_zone_names_path(move(locale_time_zone_names_path));
  1133. time_zone_names_path = time_zone_names_path.append("timeZoneNames.json"sv);
  1134. auto time_zone_names_file = TRY(Core::File::open(time_zone_names_path.string(), Core::OpenMode::ReadOnly));
  1135. auto time_zone_names = TRY(JsonValue::from_string(time_zone_names_file->read_all()));
  1136. auto const& main_object = time_zone_names.as_object().get("main"sv);
  1137. auto const& locale_object = main_object.as_object().get(time_zone_names_path.parent().basename());
  1138. auto const& dates_object = locale_object.as_object().get("dates"sv);
  1139. auto const& time_zone_names_object = dates_object.as_object().get("timeZoneNames"sv);
  1140. auto const& meta_zone_object = time_zone_names_object.as_object().get("metazone"sv);
  1141. auto const& gmt_zero_format_string = time_zone_names_object.as_object().get("gmtZeroFormat"sv);
  1142. if (meta_zone_object.is_null())
  1143. return {};
  1144. auto parse_name = [&](StringView type, JsonObject const& meta_zone_object) -> Optional<StringIndexType> {
  1145. auto const& names = meta_zone_object.get(type);
  1146. if (!names.is_object())
  1147. return {};
  1148. auto const& daylight = names.as_object().get("daylight"sv);
  1149. if (daylight.is_string())
  1150. return locale_data.unique_strings.ensure(daylight.as_string());
  1151. auto const& standard = names.as_object().get("standard"sv);
  1152. if (standard.is_string())
  1153. return locale_data.unique_strings.ensure(standard.as_string());
  1154. return {};
  1155. };
  1156. TimeZoneNamesList time_zones;
  1157. TimeZoneFormat time_zone_formats {};
  1158. time_zone_formats.gmt_zero_format = locale_data.unique_strings.ensure(gmt_zero_format_string.as_string());
  1159. auto parse_time_zone = [&](StringView meta_zone, JsonObject const& meta_zone_object) {
  1160. auto golden_zones = locale_data.meta_zones.find(meta_zone);
  1161. if (golden_zones == locale_data.meta_zones.end())
  1162. return;
  1163. TimeZoneNames time_zone_names {};
  1164. if (auto long_name = parse_name("long"sv, meta_zone_object); long_name.has_value())
  1165. time_zone_names.long_name = long_name.value();
  1166. if (auto short_name = parse_name("short"sv, meta_zone_object); short_name.has_value())
  1167. time_zone_names.short_name = short_name.value();
  1168. auto time_zone_index = locale_data.unique_time_zones.ensure(move(time_zone_names));
  1169. for (auto golden_zone : golden_zones->value) {
  1170. auto time_zone = to_underlying(golden_zone);
  1171. if (time_zone >= time_zones.size())
  1172. time_zones.resize(time_zone + 1);
  1173. time_zones[time_zone] = time_zone_index;
  1174. }
  1175. };
  1176. meta_zone_object.as_object().for_each_member([&](auto const& meta_zone, JsonValue const& value) {
  1177. parse_time_zone(meta_zone, value.as_object());
  1178. });
  1179. // The long and short names for UTC are not under the "timeZoneNames/metazone" object, but are under "timeZoneNames/zone/Etc".
  1180. auto const& zone_object = time_zone_names_object.as_object().get("zone"sv);
  1181. auto const& etc_object = zone_object.as_object().get("Etc"sv);
  1182. auto const& utc_object = etc_object.as_object().get("UTC"sv);
  1183. parse_time_zone("UTC"sv, utc_object.as_object());
  1184. locale.time_zones = locale_data.unique_time_zone_lists.ensure(move(time_zones));
  1185. locale.time_zone_formats = locale_data.unique_time_zone_formats.ensure(move(time_zone_formats));
  1186. return {};
  1187. }
  1188. static ErrorOr<void> parse_day_periods(String core_path, UnicodeLocaleData& locale_data)
  1189. {
  1190. // https://unicode.org/reports/tr35/tr35-dates.html#Day_Period_Rule_Sets
  1191. LexicalPath day_periods_path(move(core_path));
  1192. day_periods_path = day_periods_path.append("supplemental"sv);
  1193. day_periods_path = day_periods_path.append("dayPeriods.json"sv);
  1194. auto day_periods_file = TRY(Core::File::open(day_periods_path.string(), Core::OpenMode::ReadOnly));
  1195. auto locale_day_periods = TRY(JsonValue::from_string(day_periods_file->read_all()));
  1196. auto const& supplemental_object = locale_day_periods.as_object().get("supplemental"sv);
  1197. auto const& day_periods_object = supplemental_object.as_object().get("dayPeriodRuleSet"sv);
  1198. auto parse_hour = [](auto const& time) {
  1199. auto hour_end_index = time.find(':').value();
  1200. // The times are in the form "hours:minutes", but we only need the hour segment.
  1201. // TR-35 explicitly states that minutes other than :00 must not be used.
  1202. VERIFY(time.substring_view(hour_end_index) == ":00"sv);
  1203. auto hour = time.substring_view(0, hour_end_index);
  1204. return hour.template to_uint<u8>().value();
  1205. };
  1206. auto parse_day_period = [&](auto const& symbol, auto const& ranges) -> Optional<DayPeriod> {
  1207. auto day_period = day_period_from_string(symbol);
  1208. if (!day_period.has_value())
  1209. return {};
  1210. auto begin = parse_hour(ranges.get("_from"sv).as_string());
  1211. auto end = parse_hour(ranges.get("_before"sv).as_string());
  1212. return DayPeriod { *day_period, begin, end };
  1213. };
  1214. day_periods_object.as_object().for_each_member([&](auto const& language, JsonValue const& value) {
  1215. auto locale = locale_data.locales.find(language);
  1216. if (locale == locale_data.locales.end())
  1217. return;
  1218. DayPeriodList day_periods;
  1219. value.as_object().for_each_member([&](auto const& symbol, JsonValue const& ranges) {
  1220. if (auto day_period = parse_day_period(symbol, ranges.as_object()); day_period.has_value()) {
  1221. auto day_period_index = locale_data.unique_day_periods.ensure(day_period.release_value());
  1222. day_periods.append(day_period_index);
  1223. }
  1224. });
  1225. locale->value.day_periods = locale_data.unique_day_period_lists.ensure(move(day_periods));
  1226. });
  1227. return {};
  1228. };
  1229. static ErrorOr<void> parse_all_locales(String core_path, String dates_path, UnicodeLocaleData& locale_data)
  1230. {
  1231. TRY(parse_hour_cycles(core_path, locale_data));
  1232. TRY(parse_meta_zones(core_path, locale_data));
  1233. auto dates_iterator = TRY(path_to_dir_iterator(move(dates_path)));
  1234. auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
  1235. auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(locale_data.unique_strings, LexicalPath::basename(path)));
  1236. StringBuilder builder;
  1237. builder.append(locale_data.unique_strings.get(parsed_locale.language));
  1238. if (auto script = locale_data.unique_strings.get(parsed_locale.script); !script.is_empty())
  1239. builder.appendff("-{}", script);
  1240. if (auto region = locale_data.unique_strings.get(parsed_locale.region); !region.is_empty())
  1241. builder.appendff("-{}", region);
  1242. return builder.build();
  1243. };
  1244. while (dates_iterator.has_next()) {
  1245. auto dates_path = TRY(next_path_from_dir_iterator(dates_iterator));
  1246. auto calendars_iterator = TRY(path_to_dir_iterator(dates_path, {}));
  1247. auto language = TRY(remove_variants_from_path(dates_path));
  1248. auto& locale = locale_data.locales.ensure(language);
  1249. while (calendars_iterator.has_next()) {
  1250. auto calendars_path = TRY(next_path_from_dir_iterator(calendars_iterator));
  1251. TRY(parse_calendars(move(calendars_path), locale_data, locale));
  1252. }
  1253. TRY(parse_time_zone_names(move(dates_path), locale_data, locale));
  1254. }
  1255. TRY(parse_day_periods(move(core_path), locale_data));
  1256. return {};
  1257. }
  1258. static String format_identifier(StringView owner, String identifier)
  1259. {
  1260. identifier = identifier.replace("-"sv, "_"sv, true);
  1261. identifier = identifier.replace("/"sv, "_"sv, true);
  1262. if (all_of(identifier, is_ascii_digit))
  1263. return String::formatted("{}_{}", owner[0], identifier);
  1264. if (is_ascii_lower_alpha(identifier[0]))
  1265. return String::formatted("{:c}{}", to_ascii_uppercase(identifier[0]), identifier.substring_view(1));
  1266. return identifier;
  1267. }
  1268. static void generate_unicode_locale_header(Core::File& file, UnicodeLocaleData& locale_data)
  1269. {
  1270. StringBuilder builder;
  1271. SourceGenerator generator { builder };
  1272. generator.append(R"~~~(
  1273. #pragma once
  1274. #include <AK/Types.h>
  1275. namespace Unicode {
  1276. )~~~");
  1277. generate_enum(generator, format_identifier, "Calendar"sv, {}, locale_data.calendars, locale_data.calendar_aliases);
  1278. generate_enum(generator, format_identifier, "HourCycleRegion"sv, {}, locale_data.hour_cycle_regions);
  1279. generator.append(R"~~~(
  1280. }
  1281. )~~~");
  1282. VERIFY(file.write(generator.as_string_view()));
  1283. }
  1284. static void generate_unicode_locale_implementation(Core::File& file, UnicodeLocaleData& locale_data)
  1285. {
  1286. StringBuilder builder;
  1287. SourceGenerator generator { builder };
  1288. generator.set("string_index_type"sv, s_string_index_type);
  1289. generator.set("calendar_pattern_index_type"sv, s_calendar_pattern_index_type);
  1290. generator.set("calendar_pattern_list_index_type"sv, s_calendar_pattern_list_index_type);
  1291. generator.set("calendar_range_pattern_index_type"sv, s_calendar_range_pattern_index_type);
  1292. generator.set("calendar_range_pattern_list_index_type"sv, s_calendar_range_pattern_list_index_type);
  1293. generator.set("calendar_format_index_type"sv, s_calendar_format_index_type);
  1294. generator.set("symbol_list_index_type"sv, s_symbol_list_index_type);
  1295. generator.set("calendar_symbols_index_type"sv, s_calendar_symbols_index_type);
  1296. generator.set("calendar_symbols_list_index_type"sv, s_calendar_symbols_list_index_type);
  1297. generator.set("calendar_index_type"sv, s_calendar_index_type);
  1298. generator.set("time_zone_index_type"sv, s_time_zone_index_type);
  1299. generator.set("time_zone_list_index_type"sv, s_time_zone_list_index_type);
  1300. generator.set("day_period_index_type"sv, s_day_period_index_type);
  1301. generator.set("day_period_list_index_type"sv, s_day_period_list_index_type);
  1302. generator.append(R"~~~(
  1303. #include <AK/Array.h>
  1304. #include <AK/BinarySearch.h>
  1305. #include <AK/Optional.h>
  1306. #include <AK/StringView.h>
  1307. #include <LibTimeZone/TimeZone.h>
  1308. #include <LibUnicode/DateTimeFormat.h>
  1309. #include <LibUnicode/Locale.h>
  1310. #include <LibUnicode/UnicodeDateTimeFormat.h>
  1311. namespace Unicode {
  1312. )~~~");
  1313. locale_data.unique_strings.generate(generator);
  1314. generator.append(R"~~~(
  1315. template <typename SourceType, typename TargetType>
  1316. static void convert_calendar_fields(SourceType const& source, TargetType& target)
  1317. {
  1318. if (source.era != -1)
  1319. target.era = static_cast<CalendarPatternStyle>(source.era);
  1320. if (source.year != -1)
  1321. target.year = static_cast<CalendarPatternStyle>(source.year);
  1322. if (source.month != -1)
  1323. target.month = static_cast<CalendarPatternStyle>(source.month);
  1324. if (source.weekday != -1)
  1325. target.weekday = static_cast<CalendarPatternStyle>(source.weekday);
  1326. if (source.day != -1)
  1327. target.day = static_cast<CalendarPatternStyle>(source.day);
  1328. if (source.day_period != -1)
  1329. target.day_period = static_cast<CalendarPatternStyle>(source.day_period);
  1330. if (source.hour != -1)
  1331. target.hour = static_cast<CalendarPatternStyle>(source.hour);
  1332. if (source.minute != -1)
  1333. target.minute = static_cast<CalendarPatternStyle>(source.minute);
  1334. if (source.second != -1)
  1335. target.second = static_cast<CalendarPatternStyle>(source.second);
  1336. if (source.fractional_second_digits != -1)
  1337. target.fractional_second_digits = static_cast<u8>(source.fractional_second_digits);
  1338. if (source.time_zone_name != -1)
  1339. target.time_zone_name = static_cast<CalendarPatternStyle>(source.time_zone_name);
  1340. }
  1341. struct CalendarPatternImpl {
  1342. CalendarPattern to_unicode_calendar_pattern() const {
  1343. CalendarPattern calendar_pattern {};
  1344. calendar_pattern.skeleton = s_string_list[skeleton];
  1345. calendar_pattern.pattern = s_string_list[pattern];
  1346. if (pattern12 != 0)
  1347. calendar_pattern.pattern12 = s_string_list[pattern12];
  1348. convert_calendar_fields(*this, calendar_pattern);
  1349. return calendar_pattern;
  1350. }
  1351. @string_index_type@ skeleton { 0 };
  1352. @string_index_type@ pattern { 0 };
  1353. @string_index_type@ pattern12 { 0 };
  1354. i8 era { -1 };
  1355. i8 year { -1 };
  1356. i8 month { -1 };
  1357. i8 weekday { -1 };
  1358. i8 day { -1 };
  1359. i8 day_period { -1 };
  1360. i8 hour { -1 };
  1361. i8 minute { -1 };
  1362. i8 second { -1 };
  1363. i8 fractional_second_digits { -1 };
  1364. i8 time_zone_name { -1 };
  1365. };
  1366. struct CalendarRangePatternImpl {
  1367. CalendarRangePattern to_unicode_calendar_range_pattern() const {
  1368. CalendarRangePattern calendar_range_pattern {};
  1369. if (field != -1)
  1370. calendar_range_pattern.field = static_cast<CalendarRangePattern::Field>(field);
  1371. calendar_range_pattern.start_range = s_string_list[start_range];
  1372. calendar_range_pattern.separator = s_string_list[separator];
  1373. calendar_range_pattern.end_range = s_string_list[end_range];
  1374. convert_calendar_fields(*this, calendar_range_pattern);
  1375. return calendar_range_pattern;
  1376. }
  1377. @string_index_type@ skeleton { 0 };
  1378. i8 field { -1 };
  1379. @string_index_type@ start_range { 0 };
  1380. @string_index_type@ separator { 0 };
  1381. @string_index_type@ end_range { 0 };
  1382. i8 era { -1 };
  1383. i8 year { -1 };
  1384. i8 month { -1 };
  1385. i8 weekday { -1 };
  1386. i8 day { -1 };
  1387. i8 day_period { -1 };
  1388. i8 hour { -1 };
  1389. i8 minute { -1 };
  1390. i8 second { -1 };
  1391. i8 fractional_second_digits { -1 };
  1392. i8 time_zone_name { -1 };
  1393. };
  1394. )~~~");
  1395. locale_data.unique_patterns.generate(generator, "CalendarPatternImpl"sv, "s_calendar_patterns"sv, 10);
  1396. locale_data.unique_pattern_lists.generate(generator, s_calendar_pattern_index_type, "s_calendar_pattern_lists"sv);
  1397. locale_data.unique_range_patterns.generate(generator, "CalendarRangePatternImpl"sv, "s_calendar_range_patterns"sv, 10);
  1398. locale_data.unique_range_pattern_lists.generate(generator, s_calendar_range_pattern_index_type, "s_calendar_range_pattern_lists"sv);
  1399. generator.append(R"~~~(
  1400. struct CalendarFormatImpl {
  1401. CalendarFormat to_unicode_calendar_format() const {
  1402. CalendarFormat calendar_format {};
  1403. calendar_format.full_format = s_calendar_patterns[full_format].to_unicode_calendar_pattern();
  1404. calendar_format.long_format = s_calendar_patterns[long_format].to_unicode_calendar_pattern();
  1405. calendar_format.medium_format = s_calendar_patterns[medium_format].to_unicode_calendar_pattern();
  1406. calendar_format.short_format = s_calendar_patterns[short_format].to_unicode_calendar_pattern();
  1407. return calendar_format;
  1408. }
  1409. @calendar_pattern_index_type@ full_format { 0 };
  1410. @calendar_pattern_index_type@ long_format { 0 };
  1411. @calendar_pattern_index_type@ medium_format { 0 };
  1412. @calendar_pattern_index_type@ short_format { 0 };
  1413. };
  1414. struct CalendarSymbols {
  1415. @symbol_list_index_type@ narrow_symbols { 0 };
  1416. @symbol_list_index_type@ short_symbols { 0 };
  1417. @symbol_list_index_type@ long_symbols { 0 };
  1418. };
  1419. struct CalendarData {
  1420. @calendar_format_index_type@ date_formats { 0 };
  1421. @calendar_format_index_type@ time_formats { 0 };
  1422. @calendar_format_index_type@ date_time_formats { 0 };
  1423. @calendar_pattern_list_index_type@ available_formats { 0 };
  1424. @calendar_range_pattern_index_type@ default_range_format { 0 };
  1425. @calendar_range_pattern_list_index_type@ range_formats { 0 };
  1426. @calendar_range_pattern_list_index_type@ range12_formats { 0 };
  1427. @calendar_symbols_list_index_type@ symbols { 0 };
  1428. };
  1429. struct TimeZoneNames {
  1430. @string_index_type@ long_name { 0 };
  1431. @string_index_type@ short_name { 0 };
  1432. };
  1433. struct TimeZoneFormat {
  1434. @string_index_type@ gmt_zero_format { 0 };
  1435. };
  1436. struct DayPeriodData {
  1437. u8 day_period { 0 };
  1438. u8 begin { 0 };
  1439. u8 end { 0 };
  1440. };
  1441. )~~~");
  1442. locale_data.unique_formats.generate(generator, "CalendarFormatImpl"sv, "s_calendar_formats"sv, 10);
  1443. locale_data.unique_symbol_lists.generate(generator, s_string_index_type, "s_symbol_lists"sv);
  1444. locale_data.unique_calendar_symbols.generate(generator, "CalendarSymbols"sv, "s_calendar_symbols"sv, 10);
  1445. locale_data.unique_calendar_symbols_lists.generate(generator, s_calendar_symbols_index_type, "s_calendar_symbol_lists"sv);
  1446. locale_data.unique_calendars.generate(generator, "CalendarData"sv, "s_calendars"sv, 10);
  1447. locale_data.unique_time_zones.generate(generator, "TimeZoneNames"sv, "s_time_zones"sv, 30);
  1448. locale_data.unique_time_zone_lists.generate(generator, s_time_zone_index_type, "s_time_zone_lists"sv);
  1449. locale_data.unique_time_zone_formats.generate(generator, "TimeZoneFormat"sv, "s_time_zone_formats"sv, 30);
  1450. locale_data.unique_day_periods.generate(generator, "DayPeriodData"sv, "s_day_periods"sv, 30);
  1451. locale_data.unique_day_period_lists.generate(generator, s_day_period_index_type, "s_day_period_lists"sv);
  1452. locale_data.unique_hour_cycle_lists.generate(generator, "u8"sv, "s_hour_cycle_lists"sv);
  1453. auto append_calendars = [&](String name, auto const& calendars) {
  1454. generator.set("name", name);
  1455. generator.set("size", String::number(calendars.size()));
  1456. generator.append(R"~~~(
  1457. static constexpr Array<@calendar_index_type@, @size@> @name@ { {)~~~");
  1458. bool first = true;
  1459. for (auto const& calendar_key : locale_data.calendars) {
  1460. auto calendar = calendars.find(calendar_key)->value;
  1461. generator.append(first ? " " : ", ");
  1462. generator.append(String::number(calendar));
  1463. first = false;
  1464. }
  1465. generator.append(" } };");
  1466. };
  1467. auto append_mapping = [&](auto const& keys, auto const& map, auto type, auto name, auto mapping_getter) {
  1468. generator.set("type", type);
  1469. generator.set("name", name);
  1470. generator.set("size", String::number(keys.size()));
  1471. generator.append(R"~~~(
  1472. static constexpr Array<@type@, @size@> @name@ { {)~~~");
  1473. bool first = true;
  1474. for (auto const& key : keys) {
  1475. auto const& value = map.find(key)->value;
  1476. auto mapping = mapping_getter(value);
  1477. generator.append(first ? " " : ", ");
  1478. generator.append(String::number(mapping));
  1479. first = false;
  1480. }
  1481. generator.append(" } };");
  1482. };
  1483. auto locales = locale_data.locales.keys();
  1484. quick_sort(locales);
  1485. generate_mapping(generator, locale_data.locales, s_calendar_index_type, "s_locale_calendars"sv, "s_calendars_{}", format_identifier, [&](auto const& name, auto const& value) { append_calendars(name, value.calendars); });
  1486. append_mapping(locales, locale_data.locales, s_time_zone_index_type, "s_locale_time_zones"sv, [](auto const& locale) { return locale.time_zones; });
  1487. append_mapping(locales, locale_data.locales, s_time_zone_format_index_type, "s_locale_time_zone_formats"sv, [](auto const& locale) { return locale.time_zone_formats; });
  1488. append_mapping(locales, locale_data.locales, s_day_period_index_type, "s_locale_day_periods"sv, [](auto const& locale) { return locale.day_periods; });
  1489. append_mapping(locale_data.hour_cycle_regions, locale_data.hour_cycles, s_hour_cycle_list_index_type, "s_hour_cycles"sv, [](auto const& hour_cycles) { return hour_cycles; });
  1490. generator.append("\n");
  1491. auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) {
  1492. HashValueMap<String> hashes;
  1493. hashes.ensure_capacity(values.size());
  1494. for (auto const& value : values)
  1495. hashes.set(value.hash(), format_identifier(enum_title, value));
  1496. for (auto const& alias : aliases)
  1497. hashes.set(alias.alias.hash(), format_identifier(enum_title, alias.alias));
  1498. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
  1499. };
  1500. append_from_string("Calendar"sv, "calendar"sv, locale_data.calendars, locale_data.calendar_aliases);
  1501. append_from_string("HourCycleRegion"sv, "hour_cycle_region"sv, locale_data.hour_cycle_regions);
  1502. generator.append(R"~~~(
  1503. Vector<HourCycle> get_regional_hour_cycles(StringView region)
  1504. {
  1505. auto region_value = hour_cycle_region_from_string(region);
  1506. if (!region_value.has_value())
  1507. return {};
  1508. auto region_index = to_underlying(*region_value);
  1509. auto regional_hour_cycles_index = s_hour_cycles.at(region_index);
  1510. auto const& regional_hour_cycles = s_hour_cycle_lists.at(regional_hour_cycles_index);
  1511. Vector<HourCycle> hour_cycles;
  1512. hour_cycles.ensure_capacity(regional_hour_cycles.size());
  1513. for (auto hour_cycle : regional_hour_cycles)
  1514. hour_cycles.unchecked_append(static_cast<HourCycle>(hour_cycle));
  1515. return hour_cycles;
  1516. }
  1517. static CalendarData const* find_calendar_data(StringView locale, StringView calendar)
  1518. {
  1519. auto locale_value = locale_from_string(locale);
  1520. if (!locale_value.has_value())
  1521. return nullptr;
  1522. auto calendar_value = calendar_from_string(calendar);
  1523. if (!calendar_value.has_value())
  1524. return nullptr;
  1525. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  1526. size_t calendar_index = to_underlying(*calendar_value);
  1527. auto const& calendar_indices = s_locale_calendars.at(locale_index);
  1528. calendar_index = calendar_indices[calendar_index];
  1529. return &s_calendars[calendar_index];
  1530. }
  1531. Optional<CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar)
  1532. {
  1533. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1534. auto const& formats = s_calendar_formats.at(data->date_formats);
  1535. return formats.to_unicode_calendar_format();
  1536. }
  1537. return {};
  1538. }
  1539. Optional<CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar)
  1540. {
  1541. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1542. auto const& formats = s_calendar_formats.at(data->time_formats);
  1543. return formats.to_unicode_calendar_format();
  1544. }
  1545. return {};
  1546. }
  1547. Optional<CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar)
  1548. {
  1549. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1550. auto const& formats = s_calendar_formats.at(data->date_time_formats);
  1551. return formats.to_unicode_calendar_format();
  1552. }
  1553. return {};
  1554. }
  1555. Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar)
  1556. {
  1557. Vector<CalendarPattern> result {};
  1558. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1559. auto const& available_formats = s_calendar_pattern_lists.at(data->available_formats);
  1560. result.ensure_capacity(available_formats.size());
  1561. for (auto const& format : available_formats)
  1562. result.unchecked_append(s_calendar_patterns[format].to_unicode_calendar_pattern());
  1563. }
  1564. return result;
  1565. }
  1566. Optional<CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar)
  1567. {
  1568. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1569. auto const& pattern = s_calendar_range_patterns[data->default_range_format];
  1570. return pattern.to_unicode_calendar_range_pattern();
  1571. }
  1572. return {};
  1573. }
  1574. Vector<CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton)
  1575. {
  1576. Vector<CalendarRangePattern> result {};
  1577. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1578. auto const& range_formats = s_calendar_range_pattern_lists.at(data->range_formats);
  1579. for (auto format : range_formats) {
  1580. auto const& pattern = s_calendar_range_patterns[format];
  1581. if (skeleton == s_string_list[pattern.skeleton])
  1582. result.append(pattern.to_unicode_calendar_range_pattern());
  1583. }
  1584. }
  1585. return result;
  1586. }
  1587. Vector<CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton)
  1588. {
  1589. Vector<CalendarRangePattern> result {};
  1590. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1591. auto const& range12_formats = s_calendar_range_pattern_lists.at(data->range12_formats);
  1592. for (auto format : range12_formats) {
  1593. auto const& pattern = s_calendar_range_patterns[format];
  1594. if (skeleton == s_string_list[pattern.skeleton])
  1595. result.append(pattern.to_unicode_calendar_range_pattern());
  1596. }
  1597. }
  1598. return result;
  1599. }
  1600. static Span<@string_index_type@ const> find_calendar_symbols(StringView locale, StringView calendar, CalendarSymbol symbol, CalendarPatternStyle style)
  1601. {
  1602. if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
  1603. auto const& symbols_list = s_calendar_symbol_lists[data->symbols];
  1604. auto symbol_index = to_underlying(symbol);
  1605. auto calendar_symbols_index = symbols_list.at(symbol_index);
  1606. auto const& symbols = s_calendar_symbols.at(calendar_symbols_index);
  1607. @symbol_list_index_type@ symbol_list_index = 0;
  1608. switch (style) {
  1609. case CalendarPatternStyle::Narrow:
  1610. symbol_list_index = symbols.narrow_symbols;
  1611. break;
  1612. case CalendarPatternStyle::Short:
  1613. symbol_list_index = symbols.short_symbols;
  1614. break;
  1615. case CalendarPatternStyle::Long:
  1616. symbol_list_index = symbols.long_symbols;
  1617. break;
  1618. default:
  1619. VERIFY_NOT_REACHED();
  1620. }
  1621. return s_symbol_lists.at(symbol_list_index);
  1622. }
  1623. return {};
  1624. }
  1625. Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value)
  1626. {
  1627. auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Era, style);
  1628. if (auto value_index = to_underlying(value); value_index < symbols.size())
  1629. return s_string_list[symbols.at(value_index)];
  1630. return {};
  1631. }
  1632. Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Month value)
  1633. {
  1634. auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Month, style);
  1635. if (auto value_index = to_underlying(value); value_index < symbols.size())
  1636. return s_string_list[symbols.at(value_index)];
  1637. return {};
  1638. }
  1639. Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Weekday value)
  1640. {
  1641. auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Weekday, style);
  1642. if (auto value_index = to_underlying(value); value_index < symbols.size())
  1643. return s_string_list[symbols.at(value_index)];
  1644. return {};
  1645. }
  1646. Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, DayPeriod value)
  1647. {
  1648. auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::DayPeriod, style);
  1649. if (auto value_index = to_underlying(value); value_index < symbols.size())
  1650. return s_string_list[symbols.at(value_index)];
  1651. return {};
  1652. }
  1653. Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour)
  1654. {
  1655. auto locale_value = locale_from_string(locale);
  1656. if (!locale_value.has_value())
  1657. return {};
  1658. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  1659. auto day_periods_index = s_locale_day_periods[locale_index];
  1660. auto day_periods = s_day_period_lists[day_periods_index];
  1661. for (auto day_period_index : day_periods) {
  1662. auto day_period = s_day_periods[day_period_index];
  1663. auto h = hour;
  1664. if (day_period.begin > day_period.end) {
  1665. day_period.end += 24;
  1666. h += 24;
  1667. }
  1668. if ((day_period.begin <= h) && (h < day_period.end)) {
  1669. auto period = static_cast<DayPeriod>(day_period.day_period);
  1670. return get_calendar_day_period_symbol(locale, calendar, style, period);
  1671. }
  1672. }
  1673. // Fallback to fixed periods if the locale does not have flexible day periods.
  1674. // TR-35 states that the meaning of AM and PM does not change with locale.
  1675. if (hour < 12)
  1676. return get_calendar_day_period_symbol(locale, calendar, style, DayPeriod::AM);
  1677. return get_calendar_day_period_symbol(locale, calendar, style, DayPeriod::PM);
  1678. }
  1679. static TimeZoneFormat const* find_time_zone_formats(StringView locale)
  1680. {
  1681. auto locale_value = locale_from_string(locale);
  1682. if (!locale_value.has_value())
  1683. return nullptr;
  1684. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  1685. auto time_zone_format_index = s_locale_time_zone_formats.at(locale_index);
  1686. return &s_time_zone_formats.at(time_zone_format_index);
  1687. }
  1688. static TimeZoneNames const* find_time_zone_names(StringView locale, StringView time_zone)
  1689. {
  1690. auto locale_value = locale_from_string(locale);
  1691. if (!locale_value.has_value())
  1692. return nullptr;
  1693. auto time_zone_value = ::TimeZone::time_zone_from_string(time_zone);
  1694. if (!time_zone_value.has_value())
  1695. return nullptr;
  1696. auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
  1697. size_t time_zone_index = to_underlying(*time_zone_value);
  1698. auto time_zone_list_index = s_locale_time_zones.at(locale_index);
  1699. auto const& time_zone_list = s_time_zone_lists.at(time_zone_list_index);
  1700. if (time_zone_list.size() <= time_zone_index)
  1701. return nullptr;
  1702. time_zone_index = time_zone_list.at(time_zone_index);
  1703. return &s_time_zones[time_zone_index];
  1704. }
  1705. Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style)
  1706. {
  1707. if ((style == CalendarPatternStyle::Long) || (style == CalendarPatternStyle::Short)) {
  1708. if (auto const* data = find_time_zone_names(locale, time_zone); data != nullptr) {
  1709. auto time_zone_index = style == CalendarPatternStyle::Long ? data->long_name : data->short_name;
  1710. if (time_zone_index != 0)
  1711. return s_string_list[time_zone_index];
  1712. }
  1713. } else {
  1714. // FIXME: This becomes more complicated when time zones other than UTC are supported. We will need to know the GMT offset
  1715. // of each time zone (which must be parsed from the time zone database, not the CLDR). For now, assuming UTC means
  1716. // we can assume a GMT offset of 0, for which the CLDR has a specific format string for the offset styles. Further,
  1717. // we will need to parse the "generic" time zone names from timeZoneNames.json.
  1718. VERIFY(time_zone == "UTC"sv);
  1719. if (auto const* formats = find_time_zone_formats(locale); formats != nullptr)
  1720. return s_string_list[formats->gmt_zero_format];
  1721. }
  1722. return {};
  1723. }
  1724. }
  1725. )~~~");
  1726. VERIFY(file.write(generator.as_string_view()));
  1727. }
  1728. ErrorOr<int> serenity_main(Main::Arguments arguments)
  1729. {
  1730. StringView generated_header_path;
  1731. StringView generated_implementation_path;
  1732. StringView core_path;
  1733. StringView dates_path;
  1734. Core::ArgsParser args_parser;
  1735. args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path");
  1736. args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  1737. args_parser.add_option(core_path, "Path to cldr-core directory", "core-path", 'r', "core-path");
  1738. args_parser.add_option(dates_path, "Path to cldr-dates directory", "dates-path", 'd', "dates-path");
  1739. args_parser.parse(arguments);
  1740. auto open_file = [&](StringView path) -> ErrorOr<NonnullRefPtr<Core::File>> {
  1741. if (path.is_empty()) {
  1742. args_parser.print_usage(stderr, arguments.argv[0]);
  1743. return Error::from_string_literal("Must provide all command line options"sv);
  1744. }
  1745. return Core::File::open(path, Core::OpenMode::ReadWrite);
  1746. };
  1747. auto generated_header_file = TRY(open_file(generated_header_path));
  1748. auto generated_implementation_file = TRY(open_file(generated_implementation_path));
  1749. UnicodeLocaleData locale_data;
  1750. TRY(parse_all_locales(core_path, dates_path, locale_data));
  1751. generate_unicode_locale_header(generated_header_file, locale_data);
  1752. generate_unicode_locale_implementation(generated_implementation_file, locale_data);
  1753. return 0;
  1754. }