summaryrefslogtreecommitdiffstats
path: root/server/src/locale.cppm
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/locale.cppm')
-rw-r--r--server/src/locale.cppm33
1 files changed, 17 insertions, 16 deletions
diff --git a/server/src/locale.cppm b/server/src/locale.cppm
index 06607f0..da80f76 100644
--- a/server/src/locale.cppm
+++ b/server/src/locale.cppm
@@ -50,11 +50,11 @@ struct icu_locale_hash
50}; 50};
51 51
52using icu_locale_priority_map = 52using 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>;
54using icu_priority_locale = std::pair<icu::Locale, locale_priority>; 54using icu_priority_locale = std::pair<icu::Locale, locale_priority>;
55 55
56auto operator<(icu_priority_locale const& lhs, icu_priority_locale const& rhs) 56auto 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
67public: 67public:
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,8 +80,9 @@ public:
80 80
81export template <class T> 81export template <class T>
82concept locale_input_range = 82concept locale_input_range =
83 std::ranges::input_range<T> 83 std::ranges::input_range<T>
84 && std::same_as<std::locale const&, std::ranges::range_const_reference_t<T>>; 84 && std::same_as<
85 std::locale const&, std::ranges::range_const_reference_t<T>>;
85 86
86// Helps select a locale based on the Accept-Language header in an 87// Helps select a locale based on the Accept-Language header in an
87// HTTP request. 88// HTTP request.
@@ -92,15 +93,15 @@ export class selector
92 std::shared_ptr<blocale::generator const> lgen_; 93 std::shared_ptr<blocale::generator const> lgen_;
93 94
94 auto make_matcher( 95 auto make_matcher(
95 locale_input_range auto supported_locales, std::locale default_locale) 96 locale_input_range auto supported_locales, std::locale default_locale)
96 { 97 {
97 auto builder = icu::LocaleMatcher::Builder{}; 98 auto builder = icu::LocaleMatcher::Builder{};
98 for (auto const& supported_locale : supported_locales) 99 for (auto const& supported_locale : supported_locales)
99 { 100 {
100 auto const& supported_locale_info = 101 auto const& supported_locale_info =
101 std::use_facet<blocale::info>(supported_locale); 102 std::use_facet<blocale::info>(supported_locale);
102 auto supported_icu_locale = 103 auto supported_icu_locale =
103 icu::Locale{supported_locale_info.name().c_str()}; 104 icu::Locale{supported_locale_info.name().c_str()};
104 if (supported_icu_locale.isBogus()) 105 if (supported_icu_locale.isBogus())
105 throw std::runtime_error{ 106 throw std::runtime_error{
106 "supported locale gives rise to bogus ICU locale" 107 "supported locale gives rise to bogus ICU locale"
@@ -108,7 +109,7 @@ export class selector
108 builder.addSupportedLocale(supported_icu_locale); 109 builder.addSupportedLocale(supported_icu_locale);
109 } 110 }
110 auto const& default_locale_info = 111 auto const& default_locale_info =
111 std::use_facet<blocale::info>(default_locale); 112 std::use_facet<blocale::info>(default_locale);
112 auto default_icu_locale = icu::Locale{default_locale_info.name().c_str()}; 113 auto default_icu_locale = icu::Locale{default_locale_info.name().c_str()};
113 if (default_icu_locale.isBogus()) 114 if (default_icu_locale.isBogus())
114 throw std::runtime_error{"default locale gives rise to bogus ICU locale"}; 115 throw std::runtime_error{"default locale gives rise to bogus ICU locale"};
@@ -170,7 +171,7 @@ export class selector
170 while (kw = keywords->next(&kw_len, ec), !U_FAILURE(ec) && kw) 171 while (kw = keywords->next(&kw_len, ec), !U_FAILURE(ec) && kw)
171 { 172 {
172 auto value = 173 auto value =
173 l.getKeywordValue<std::string>(icu::StringPiece(kw, kw_len), ec); 174 l.getKeywordValue<std::string>(icu::StringPiece(kw, kw_len), ec);
174 if (!added_at) 175 if (!added_at)
175 { 176 {
176 posix_name += "@"; 177 posix_name += "@";
@@ -195,8 +196,8 @@ public:
195 // Note: lgen must live at least as long as the selector constructed here! 196 // Note: lgen must live at least as long as the selector constructed here!
196 // It is unfortunately not possible to copy/move a blocale::generator. 197 // It is unfortunately not possible to copy/move a blocale::generator.
197 explicit selector( 198 explicit selector(
198 locale_input_range auto locales, std::locale default_, 199 locale_input_range auto locales, std::locale default_,
199 std::shared_ptr<blocale::generator const> lgen) 200 std::shared_ptr<blocale::generator const> lgen)
200 : default_{default_}, matcher_{make_matcher(locales, default_)}, lgen_{lgen} 201 : default_{default_}, matcher_{make_matcher(locales, default_)}, lgen_{lgen}
201 { 202 {
202 } 203 }
@@ -211,7 +212,7 @@ public:
211 accept_language | std::views::split(","sv) | std::views::enumerate) 212 accept_language | std::views::split(","sv) | std::views::enumerate)
212 { 213 {
213 auto [lang_range_ut, mweight_ut] = 214 auto [lang_range_ut, mweight_ut] =
214 util::split_on(std::string_view{lang_prio}, ';'); 215 util::split_on(std::string_view{lang_prio}, ';');
215 auto lang_range_str = trim_ows(lang_range_ut); 216 auto lang_range_str = trim_ows(lang_range_ut);
216 auto mweight_str = mweight_ut.transform(trim_ows); 217 auto mweight_str = mweight_ut.transform(trim_ows);
217 if (lang_range_str == "*") 218 if (lang_range_str == "*")
@@ -227,7 +228,7 @@ public:
227 { 228 {
228 auto weight_str = mweight_str->substr(2, 4); 229 auto weight_str = mweight_str->substr(2, 4);
229 if (auto mweight = 230 if (auto mweight =
230 util::parse_float(weight_str, std::chars_format::fixed); 231 util::parse_float(weight_str, std::chars_format::fixed);
231 mweight && 0.0f < *mweight && *mweight < 1.0f) 232 mweight && 0.0f < *mweight && *mweight < 1.0f)
232 { 233 {
233 weight = *mweight; 234 weight = *mweight;
@@ -248,7 +249,7 @@ public:
248 } 249 }
249 250
250 auto desired_locales = 251 auto desired_locales =
251 std::vector<icu_priority_locale>{dlpm.begin(), dlpm.end()}; 252 std::vector<icu_priority_locale>{dlpm.begin(), dlpm.end()};
252 std::sort(desired_locales.begin(), desired_locales.end()); 253 std::sort(desired_locales.begin(), desired_locales.end());
253 auto it = icu_priority_locale_vec_iterator{std::move(desired_locales)}; 254 auto it = icu_priority_locale_vec_iterator{std::move(desired_locales)};
254 auto ec = UErrorCode::U_ZERO_ERROR; 255 auto ec = UErrorCode::U_ZERO_ERROR;
@@ -267,7 +268,7 @@ export auto to_bcp47_lang_tag(std::locale locale) -> std::optional<std::string>
267 auto const& locale_info = std::use_facet<blocale::info>(locale); 268 auto const& locale_info = std::use_facet<blocale::info>(locale);
268 auto ec = UErrorCode::U_ZERO_ERROR; 269 auto ec = UErrorCode::U_ZERO_ERROR;
269 auto bcp47_lang_tag = 270 auto bcp47_lang_tag =
270 icu::Locale{locale_info.name().c_str()}.toLanguageTag<std::string>(ec); 271 icu::Locale{locale_info.name().c_str()}.toLanguageTag<std::string>(ec);
271 if (U_FAILURE(ec)) 272 if (U_FAILURE(ec))
272 return std::nullopt; 273 return std::nullopt;
273 return bcp47_lang_tag; 274 return bcp47_lang_tag;