/* * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include U_NAMESPACE_BEGIN class DateTimePatternGenerator; class LocaleDisplayNames; class NumberingSystem; class TimeZone; class TimeZoneNames; U_NAMESPACE_END namespace Unicode { class LocaleData { public: static Optional for_locale(StringView locale); ALWAYS_INLINE icu::Locale& locale() { return m_locale; } String to_string(); icu::LocaleDisplayNames& standard_display_names(); icu::LocaleDisplayNames& dialect_display_names(); icu::NumberingSystem& numbering_system(); icu::DateTimePatternGenerator& date_time_pattern_generator(); icu::TimeZoneNames& time_zone_names(); Optional const& digital_format() { return m_digital_format; } void set_digital_format(DigitalFormat digital_format) { m_digital_format = move(digital_format); } private: explicit LocaleData(icu::Locale locale); icu::Locale m_locale; Optional m_locale_string; OwnPtr m_standard_display_names; OwnPtr m_dialect_display_names; OwnPtr m_numbering_system; OwnPtr m_date_time_pattern_generator; OwnPtr m_time_zone_names; Optional m_digital_format; }; class TimeZoneData { public: static Optional for_time_zone(StringView time_zone); ALWAYS_INLINE icu::TimeZone& time_zone() { return *m_time_zone; } private: explicit TimeZoneData(NonnullOwnPtr); NonnullOwnPtr m_time_zone; }; constexpr bool icu_success(UErrorCode code) { return static_cast(U_SUCCESS(code)); } constexpr bool icu_failure(UErrorCode code) { return static_cast(U_FAILURE(code)); } ALWAYS_INLINE icu::StringPiece icu_string_piece(StringView string) { return { string.characters_without_null_termination(), static_cast(string.length()) }; } ALWAYS_INLINE icu::UnicodeString icu_string(StringView string) { return icu::UnicodeString::fromUTF8(icu_string_piece(string)); } Vector icu_string_list(ReadonlySpan strings); String icu_string_to_string(icu::UnicodeString const& string); String icu_string_to_string(UChar const*, i32 length); template Vector icu_string_enumeration_to_list(OwnPtr enumeration, Filter&& filter) { UErrorCode status = U_ZERO_ERROR; Vector result; if (!enumeration) return {}; while (true) { i32 length = 0; auto const* keyword = enumeration->next(&length, status); if (icu_failure(status) || keyword == nullptr) break; if (!filter(keyword)) continue; result.append(MUST(String::from_utf8({ keyword, static_cast(length) }))); } return result; } ALWAYS_INLINE Vector icu_string_enumeration_to_list(OwnPtr enumeration) { return icu_string_enumeration_to_list(move(enumeration), [](char const*) { return true; }); } }