7#include "iso_3166_intf.hpp"
8#include "../utility/utility.hpp"
9#include "../algorithm/module.hpp"
10#include "../macros.hpp"
13hi_export_module(hikogui.i18n.iso_3166 : impl);
15namespace hi::inline
v1 {
19 fixed_string<2> code2;
20 fixed_string<3> code3;
28[[nodiscard]]
consteval auto iso_3166_infos_init() noexcept
285 constexpr auto data_size =
sizeof(data) /
sizeof(data[0]);
287 auto r = std::array<iso_3166_info, data_size>{};
289 for (
auto i = 0_uz; i != data_size; ++i) {
296constexpr auto iso_3166_infos = iso_3166_infos_init();
298[[nodiscard]]
consteval auto iso_3166_code2_by_number_init() noexcept
300 auto r = std::array<fixed_string<2>, 1000>{};
301 for (
auto i = 0_uz; i != r.size(); ++i) {
305 for (hilet& info : iso_3166_infos) {
306 r[info.number] = info.code2;
311[[nodiscard]]
consteval auto iso_3166_code3_by_number_init() noexcept
313 auto r = std::array<fixed_string<3>, 1000>{};
314 for (
auto i = 0_uz; i != r.size(); ++i) {
318 for (hilet& info : iso_3166_infos) {
319 r[info.number] = info.code3;
324[[nodiscard]]
consteval auto iso_3166_number_by_code2_init() noexcept
326 constexpr auto size = std::tuple_size_v<
decltype(iso_3166_infos)>;
327 using type = std::pair<fixed_string<2>, uint16_t>;
329 auto r = std::array<type, size>{};
330 for (
auto i = 0_uz; i != iso_3166_infos.size(); ++i) {
331 hilet& info = iso_3166_infos[i];
332 r[i] = {info.code2, info.number};
335 std::sort(r.begin(), r.end(), [](hilet& a, hilet& b) {
336 return a.first < b.first;
341[[nodiscard]]
consteval auto iso_3166_number_by_code3_init() noexcept
343 constexpr auto size = std::tuple_size_v<
decltype(iso_3166_infos)>;
344 using type = std::pair<fixed_string<3>, uint16_t>;
346 auto r = std::array<type, size>{};
347 for (
auto i = 0_uz; i != iso_3166_infos.size(); ++i) {
348 hilet& info = iso_3166_infos[i];
349 r[i] = {info.code3, info.number};
352 std::sort(r.begin(), r.end(), [](hilet& a, hilet& b) {
353 return a.first < b.first;
358constexpr auto iso_3166_code2_by_number = iso_3166_code2_by_number_init();
359constexpr auto iso_3166_code3_by_number = iso_3166_code3_by_number_init();
360constexpr auto iso_3166_number_by_code2 = iso_3166_number_by_code2_init();
361constexpr auto iso_3166_number_by_code3 = iso_3166_number_by_code3_init();
365constexpr iso_3166::iso_3166(std::string_view str)
368 _v = from_string<uint16_t>(str);
369 hi_check(_v < 1000,
"ISO-3166 number must be between 000 and 999, got '{}'", _v);
371 }
else if (str.size() == 2) {
372 auto str_up = to_upper(str);
375 detail::iso_3166_number_by_code2.
begin(),
376 detail::iso_3166_number_by_code2.
end(),
378 [](hilet& item, hilet& value) {
379 return item.first < value;
383 it != detail::iso_3166_number_by_code2.
end() and it->first == str_up,
384 "Could not find ISO-3166 2 letter language code '{}'",
389 }
else if (str.size() == 3) {
390 auto str_up = to_upper(str);
393 detail::iso_3166_number_by_code3.
begin(),
394 detail::iso_3166_number_by_code3.
end(),
396 [](hilet& item, hilet& value) {
397 return item.first < value;
401 it != detail::iso_3166_number_by_code3.
end() and it->first == str_up,
402 "Could not find ISO-3166 3 letter language code '{}'",
408 throw parse_error(std::format(
"Could not parse ISO-3166 code '{}'", str));
412[[nodiscard]]
constexpr std::string iso_3166::code2() const noexcept
414 hi_assert(_v < 1000);
415 return detail::iso_3166_code2_by_number[_v];
418[[nodiscard]]
constexpr std::string iso_3166::code3() const noexcept
420 hi_assert(_v < 1000);
421 return detail::iso_3166_code3_by_number[_v];
DOXYGEN BUG.
Definition algorithm.hpp:16
Definition iso_3166_impl.hpp:18