diff options
| author | Rutger Broekhoff | 2026-08-28 22:17:10 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 22:17:10 +0200 |
| commit | 70643a9e4821bd11c3f363f1708fe420965467d1 (patch) | |
| tree | 65572b58e247248846628940a2aaca197a15d784 /server/src/locale.cppm | |
| parent | 173cd34bdd0717d3f4beeac76b3296ecd46ada0d (diff) | |
| download | routemon-70643a9e4821bd11c3f363f1708fe420965467d1.tar.gz routemon-70643a9e4821bd11c3f363f1708fe420965467d1.zip | |
Format again
Diffstat (limited to 'server/src/locale.cppm')
| -rw-r--r-- | server/src/locale.cppm | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/server/src/locale.cppm b/server/src/locale.cppm index 8fd867b..06607f0 100644 --- a/server/src/locale.cppm +++ b/server/src/locale.cppm | |||
| @@ -50,11 +50,11 @@ struct icu_locale_hash | |||
| 50 | }; | 50 | }; |
| 51 | 51 | ||
| 52 | using icu_locale_priority_map = | 52 | using icu_locale_priority_map = |
| 53 | std::unordered_map<icu::Locale, locale_priority, icu_locale_hash>; | 53 | std::unordered_map<icu::Locale, locale_priority, icu_locale_hash>; |
| 54 | using icu_priority_locale = std::pair<icu::Locale, locale_priority>; | 54 | using icu_priority_locale = std::pair<icu::Locale, locale_priority>; |
| 55 | 55 | ||
| 56 | auto operator<(icu_priority_locale const& lhs, icu_priority_locale const& rhs) | 56 | auto operator<(icu_priority_locale const& lhs, icu_priority_locale const& rhs) |
| 57 | -> bool | 57 | -> bool |
| 58 | { | 58 | { |
| 59 | return lhs.second < rhs.second; | 59 | return lhs.second < rhs.second; |
| 60 | } | 60 | } |
| @@ -66,7 +66,7 @@ class icu_priority_locale_vec_iterator : public icu::Locale::Iterator | |||
| 66 | 66 | ||
| 67 | public: | 67 | public: |
| 68 | explicit icu_priority_locale_vec_iterator( | 68 | explicit icu_priority_locale_vec_iterator( |
| 69 | std::vector<icu_priority_locale>&& ls) | 69 | std::vector<icu_priority_locale>&& ls) |
| 70 | : ls_(std::move(ls)) | 70 | : ls_(std::move(ls)) |
| 71 | { | 71 | { |
| 72 | } | 72 | } |
| @@ -80,9 +80,8 @@ public: | |||
| 80 | 80 | ||
| 81 | export template <class T> | 81 | export template <class T> |
| 82 | concept locale_input_range = | 82 | concept locale_input_range = |
| 83 | std::ranges::input_range<T> | 83 | std::ranges::input_range<T> |
| 84 | && std::same_as< | 84 | && std::same_as<std::locale const&, std::ranges::range_const_reference_t<T>>; |
| 85 | std::locale const&, std::ranges::range_const_reference_t<T>>; | ||
| 86 | 85 | ||
| 87 | // Helps select a locale based on the Accept-Language header in an | 86 | // Helps select a locale based on the Accept-Language header in an |
| 88 | // HTTP request. | 87 | // HTTP request. |
| @@ -93,23 +92,23 @@ export class selector | |||
| 93 | std::shared_ptr<blocale::generator const> lgen_; | 92 | std::shared_ptr<blocale::generator const> lgen_; |
| 94 | 93 | ||
| 95 | auto make_matcher( | 94 | auto make_matcher( |
| 96 | locale_input_range auto supported_locales, std::locale default_locale) | 95 | locale_input_range auto supported_locales, std::locale default_locale) |
| 97 | { | 96 | { |
| 98 | auto builder = icu::LocaleMatcher::Builder{}; | 97 | auto builder = icu::LocaleMatcher::Builder{}; |
| 99 | for (auto const& supported_locale : supported_locales) | 98 | for (auto const& supported_locale : supported_locales) |
| 100 | { | 99 | { |
| 101 | auto const& supported_locale_info = | 100 | auto const& supported_locale_info = |
| 102 | std::use_facet<blocale::info>(supported_locale); | 101 | std::use_facet<blocale::info>(supported_locale); |
| 103 | auto supported_icu_locale = | 102 | auto supported_icu_locale = |
| 104 | icu::Locale{supported_locale_info.name().c_str()}; | 103 | icu::Locale{supported_locale_info.name().c_str()}; |
| 105 | if (supported_icu_locale.isBogus()) | 104 | if (supported_icu_locale.isBogus()) |
| 106 | throw std::runtime_error{ | 105 | throw std::runtime_error{ |
| 107 | "supported locale gives rise to bogus ICU locale" | 106 | "supported locale gives rise to bogus ICU locale" |
| 108 | }; | 107 | }; |
| 109 | builder.addSupportedLocale(supported_icu_locale); | 108 | builder.addSupportedLocale(supported_icu_locale); |
| 110 | } | 109 | } |
| 111 | auto const& default_locale_info = | 110 | auto const& default_locale_info = |
| 112 | std::use_facet<blocale::info>(default_locale); | 111 | std::use_facet<blocale::info>(default_locale); |
| 113 | auto default_icu_locale = icu::Locale{default_locale_info.name().c_str()}; | 112 | auto default_icu_locale = icu::Locale{default_locale_info.name().c_str()}; |
| 114 | if (default_icu_locale.isBogus()) | 113 | if (default_icu_locale.isBogus()) |
| 115 | throw std::runtime_error{"default locale gives rise to bogus ICU locale"}; | 114 | throw std::runtime_error{"default locale gives rise to bogus ICU locale"}; |
| @@ -171,7 +170,7 @@ export class selector | |||
| 171 | while (kw = keywords->next(&kw_len, ec), !U_FAILURE(ec) && kw) | 170 | while (kw = keywords->next(&kw_len, ec), !U_FAILURE(ec) && kw) |
| 172 | { | 171 | { |
| 173 | auto value = | 172 | auto value = |
| 174 | l.getKeywordValue<std::string>(icu::StringPiece(kw, kw_len), ec); | 173 | l.getKeywordValue<std::string>(icu::StringPiece(kw, kw_len), ec); |
| 175 | if (!added_at) | 174 | if (!added_at) |
| 176 | { | 175 | { |
| 177 | posix_name += "@"; | 176 | posix_name += "@"; |
| @@ -196,8 +195,8 @@ public: | |||
| 196 | // Note: lgen must live at least as long as the selector constructed here! | 195 | // Note: lgen must live at least as long as the selector constructed here! |
| 197 | // It is unfortunately not possible to copy/move a blocale::generator. | 196 | // It is unfortunately not possible to copy/move a blocale::generator. |
| 198 | explicit selector( | 197 | explicit selector( |
| 199 | locale_input_range auto locales, std::locale default_, | 198 | locale_input_range auto locales, std::locale default_, |
| 200 | std::shared_ptr<blocale::generator const> lgen) | 199 | std::shared_ptr<blocale::generator const> lgen) |
| 201 | : default_{default_}, matcher_{make_matcher(locales, default_)}, lgen_{lgen} | 200 | : default_{default_}, matcher_{make_matcher(locales, default_)}, lgen_{lgen} |
| 202 | { | 201 | { |
| 203 | } | 202 | } |
| @@ -212,7 +211,7 @@ public: | |||
| 212 | accept_language | std::views::split(","sv) | std::views::enumerate) | 211 | accept_language | std::views::split(","sv) | std::views::enumerate) |
| 213 | { | 212 | { |
| 214 | auto [lang_range_ut, mweight_ut] = | 213 | auto [lang_range_ut, mweight_ut] = |
| 215 | util::split_on(std::string_view{lang_prio}, ';'); | 214 | util::split_on(std::string_view{lang_prio}, ';'); |
| 216 | auto lang_range_str = trim_ows(lang_range_ut); | 215 | auto lang_range_str = trim_ows(lang_range_ut); |
| 217 | auto mweight_str = mweight_ut.transform(trim_ows); | 216 | auto mweight_str = mweight_ut.transform(trim_ows); |
| 218 | if (lang_range_str == "*") | 217 | if (lang_range_str == "*") |
| @@ -228,7 +227,7 @@ public: | |||
| 228 | { | 227 | { |
| 229 | auto weight_str = mweight_str->substr(2, 4); | 228 | auto weight_str = mweight_str->substr(2, 4); |
| 230 | if (auto mweight = | 229 | if (auto mweight = |
| 231 | util::parse_float(weight_str, std::chars_format::fixed); | 230 | util::parse_float(weight_str, std::chars_format::fixed); |
| 232 | mweight && 0.0f < *mweight && *mweight < 1.0f) | 231 | mweight && 0.0f < *mweight && *mweight < 1.0f) |
| 233 | { | 232 | { |
| 234 | weight = *mweight; | 233 | weight = *mweight; |
| @@ -238,8 +237,8 @@ public: | |||
| 238 | if (weight > 0.0f) | 237 | if (weight > 0.0f) |
| 239 | { | 238 | { |
| 240 | dlpm[icu_locale] = { | 239 | dlpm[icu_locale] = { |
| 241 | .weight = weight, | 240 | .weight = weight, |
| 242 | .original_index = static_cast<std::size_t>(i), | 241 | .original_index = static_cast<std::size_t>(i), |
| 243 | }; | 242 | }; |
| 244 | } | 243 | } |
| 245 | else | 244 | else |
| @@ -249,7 +248,7 @@ public: | |||
| 249 | } | 248 | } |
| 250 | 249 | ||
| 251 | auto desired_locales = | 250 | auto desired_locales = |
| 252 | std::vector<icu_priority_locale>{dlpm.begin(), dlpm.end()}; | 251 | std::vector<icu_priority_locale>{dlpm.begin(), dlpm.end()}; |
| 253 | std::sort(desired_locales.begin(), desired_locales.end()); | 252 | std::sort(desired_locales.begin(), desired_locales.end()); |
| 254 | auto it = icu_priority_locale_vec_iterator{std::move(desired_locales)}; | 253 | auto it = icu_priority_locale_vec_iterator{std::move(desired_locales)}; |
| 255 | auto ec = UErrorCode::U_ZERO_ERROR; | 254 | auto ec = UErrorCode::U_ZERO_ERROR; |
| @@ -268,7 +267,7 @@ export auto to_bcp47_lang_tag(std::locale locale) -> std::optional<std::string> | |||
| 268 | auto const& locale_info = std::use_facet<blocale::info>(locale); | 267 | auto const& locale_info = std::use_facet<blocale::info>(locale); |
| 269 | auto ec = UErrorCode::U_ZERO_ERROR; | 268 | auto ec = UErrorCode::U_ZERO_ERROR; |
| 270 | auto bcp47_lang_tag = | 269 | auto bcp47_lang_tag = |
| 271 | icu::Locale{locale_info.name().c_str()}.toLanguageTag<std::string>(ec); | 270 | icu::Locale{locale_info.name().c_str()}.toLanguageTag<std::string>(ec); |
| 272 | if (U_FAILURE(ec)) | 271 | if (U_FAILURE(ec)) |
| 273 | return std::nullopt; | 272 | return std::nullopt; |
| 274 | return bcp47_lang_tag; | 273 | return bcp47_lang_tag; |