Skip to content

Commit 2024d9e

Browse files
trflynn89linusg
authored andcommitted
LibUnicode: Add method to combine two format pattern skeletons
The fields of the generated elements must be in the same order as the table here: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table Further, only one field from each group of fields is allowed.
1 parent 9d4c430 commit 2024d9e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Userland/Libraries/LibUnicode/DateTimeFormat.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7+
#include <AK/Array.h>
8+
#include <AK/StringBuilder.h>
79
#include <LibUnicode/DateTimeFormat.h>
810
#include <LibUnicode/Locale.h>
911

@@ -113,6 +115,47 @@ Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
113115
return {};
114116
}
115117

118+
String combine_skeletons(StringView first, StringView second)
119+
{
120+
// https://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
121+
constexpr auto field_order = Array {
122+
"G"sv, // Era
123+
"yYuUr"sv, // Year
124+
"ML"sv, // Month
125+
"dDFg"sv, // Day
126+
"Eec"sv, // Weekday
127+
"abB"sv, // Period
128+
"hHKk"sv, // Hour
129+
"m"sv, // Minute
130+
"sSA"sv, // Second
131+
"zZOvVXx"sv, // Zone
132+
};
133+
134+
StringBuilder builder;
135+
136+
auto append_from_skeleton = [&](auto skeleton, auto ch) {
137+
auto first_index = skeleton.find(ch);
138+
if (!first_index.has_value())
139+
return false;
140+
141+
auto last_index = skeleton.find_last(ch);
142+
143+
builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
144+
return true;
145+
};
146+
147+
for (auto fields : field_order) {
148+
for (auto ch : fields) {
149+
if (append_from_skeleton(first, ch))
150+
break;
151+
if (append_from_skeleton(second, ch))
152+
break;
153+
}
154+
}
155+
156+
return builder.build();
157+
}
158+
116159
Optional<CalendarFormat> get_calendar_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarFormatType type)
117160
{
118161
#if ENABLE_UNICODE_DATA

Userland/Libraries/LibUnicode/DateTimeFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
157157
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
158158
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale);
159159
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
160+
String combine_skeletons(StringView first, StringView second);
160161
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
161162
Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
162163
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar);

0 commit comments

Comments
 (0)