dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
country.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2020-2026 Diego Dasso Migotto
3
4#ifndef DASMIG_CORE_COUNTRY_HPP
5#define DASMIG_CORE_COUNTRY_HPP
6
7#include <stdexcept>
8#include <string>
9#include <string_view>
10
11/// @file country.hpp
12/// @brief Helpers for ISO 3166-1 alpha-2 country codes.
13/// @author Diego Dasso Migotto (diegomigotto at hotmail dot com)
14
15namespace dasmig::core
16{
17
18/// @brief Whether text has the shape of an ISO 3166-1 alpha-2 code: exactly
19/// two capital ASCII letters.
20///
21/// This checks the shape only. It does not know which codes are assigned or
22/// which a library has data for.
23///
24/// @param code Text to check.
25/// @return `true` for `"BR"`, `false` for `"br"`, `"BRA"`, `""` or `"B1"`.
26[[nodiscard]] constexpr bool is_alpha2(std::string_view code) noexcept
27{
28 return code.size() == 2 && code[0] >= 'A' && code[0] <= 'Z' && code[1] >= 'A' && code[1] <= 'Z';
29}
30
31/// @brief The exception libraries throw for a country code they have no data
32/// for. Its message is `Unknown country code: <code>`.
33///
34/// @code
35/// throw core::unknown_country_error(cca2);
36/// @endcode
37///
38/// @param code The code that was not found.
39/// @return A `std::invalid_argument` to throw.
40[[nodiscard]] inline std::invalid_argument unknown_country_error(std::string_view code)
41{
42 return std::invalid_argument{std::string{"Unknown country code: "}.append(code)};
43}
44
45} // namespace dasmig::core
46
47#endif // DASMIG_CORE_COUNTRY_HPP
constexpr bool is_alpha2(std::string_view code) noexcept
Whether text has the shape of an ISO 3166-1 alpha-2 code: exactly two capital ASCII letters.
Definition country.hpp:26
std::invalid_argument unknown_country_error(std::string_view code)
The exception libraries throw for a country code they have no data for. Its message is Unknown countr...
Definition country.hpp:40