GenerateUnicodeDateTimeFormat.cpp 84 KB

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