9#include "isocodescache_p.h"
10#include "kcatalog_p.h"
13#include "spatial_index_p.h"
14#include "timezonedata_p.h"
26KCountry::~KCountry() =
default;
30bool KCountry::operator==(
const KCountry &other)
const
35bool KCountry::operator!=(
const KCountry &other)
const
59 const auto cache = IsoCodesCache::instance();
60 const auto it = std::find_if(cache->countryAlpha3MapBegin(), cache->countryAlpha3MapEnd(), [
this](
auto entry) {
61 return entry.value == d;
63 if (it != cache->countryAlpha3MapEnd()) {
64 uint16_t alpha3Key = (*it).key;
66 code[2] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
67 alpha3Key /= IsoCodes::AlphaNumKeyFactor;
68 code[1] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
69 alpha3Key /= IsoCodes::AlphaNumKeyFactor;
70 code[0] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
82 auto cache = IsoCodesCache::instance();
83 cache->loadIso3166_1();
84 const auto it = std::lower_bound(cache->countryNameMapBegin(), cache->countryNameMapEnd(), d);
85 if (it != cache->countryNameMapEnd() && (*it).key == d) {
86 return i18nd(
"iso_3166-1", cache->countryStringTableLookup((*it).value));
98 char flagA[] =
"\xF0\x9F\x87\xA6";
99 flagA[3] = 0xA6 + ((d >> 8) -
'A');
101 flagA[3] = 0xA6 + ((d & 0xff) -
'A');
122 const auto countryIt = std::lower_bound(TimezoneData::countryTimezoneMapBegin(), TimezoneData::countryTimezoneMapEnd(), d);
123 if (countryIt != TimezoneData::countryTimezoneMapEnd() && (*countryIt).key == d) {
124 tzs.
push_back(TimezoneData::ianaIdLookup((*countryIt).value));
128 const auto [subdivBegin, subdivEnd] =
129 std::equal_range(TimezoneData::subdivisionTimezoneMapBegin(), TimezoneData::subdivisionTimezoneMapEnd(), d, [](
auto lhs,
auto rhs) {
130 if constexpr (std::is_same_v<
decltype(lhs), uint16_t>)
131 return lhs < (rhs.key >> 16);
133 return (lhs.key >> 16) < rhs;
135 for (
auto it = subdivBegin; it != subdivEnd; ++it) {
136 const auto tzId = TimezoneData::ianaIdLookup((*it).value);
153 for (
const auto &l : ls) {
157 qCDebug(KI18NLD) <<
"conflicting currency information in QLocale for" << alpha2();
171 auto cache = IsoCodesCache::instance();
172 cache->loadIso3166_2();
176 auto it = std::lower_bound(cache->subdivisionNameMapBegin(), cache->subdivisionNameMapEnd(), d, [](
auto lhs,
auto rhs) {
177 return (lhs.key >> 16) < rhs;
180 auto [parentBegin, parentEnd] = std::equal_range(cache->subdivisionParentMapBegin(), cache->subdivisionParentMapEnd(), d, [](
auto lhs,
auto rhs) {
181 if constexpr (std::is_same_v<decltype(lhs), uint16_t>)
182 return lhs < (rhs.key >> 16);
184 return (lhs.key >> 16) < rhs;
187 for (; it != cache->subdivisionNameMapEnd() && ((*it).key >> 16) == d; ++it) {
188 if (!std::binary_search(parentBegin, parentEnd, (*it).key)) {
198static uint16_t validatedAlpha2Key(uint16_t alpha2Key)
204 auto cache = IsoCodesCache::instance();
205 cache->loadIso3166_1();
206 const auto it = std::lower_bound(cache->countryNameMapBegin(), cache->countryNameMapEnd(), alpha2Key);
207 if (it != cache->countryNameMapEnd() && (*it).key == alpha2Key) {
216 c.d = validatedAlpha2Key(IsoCodes::alpha2CodeToKey(alpha2Code));
226 c.d = validatedAlpha2Key(IsoCodes::alpha2CodeToKey(alpha2Code, std::strlen(alpha2Code)));
230static uint16_t alpha3Lookup(uint16_t alpha3Key)
236 auto cache = IsoCodesCache::instance();
237 cache->loadIso3166_1();
238 const auto it = std::lower_bound(cache->countryAlpha3MapBegin(), cache->countryAlpha3MapEnd(), alpha3Key);
239 if (it != cache->countryAlpha3MapEnd() && (*it).key == alpha3Key) {
248 c.d = alpha3Lookup(IsoCodes::alpha3CodeToKey(alpha3Code));
258 c.d = alpha3Lookup(IsoCodes::alpha3CodeToKey(alpha3Code, std::strlen(alpha3Code)));
264 const auto entry = SpatialIndex::lookup(latitude, longitude);
266 c.d = entry.m_subdiv >> 16;
279 for (
const auto c : name) {
285 switch (c.category()) {
306 res.
push_back(c.decomposition().at(0).toCaseFolded());
317 if (result == std::numeric_limits<uint16_t>::max() || result == code || rhs.
isEmpty()) {
324 result = result == 0 ? code : std::numeric_limits<uint16_t>::max();
332 const auto normalizedName = normalizeCountryName(name);
334 auto cache = IsoCodesCache::instance();
335 cache->loadIso3166_1();
337 uint16_t substrMatch = 0;
340 for (
auto it = cache->countryNameMapBegin(); it != cache->countryNameMapEnd(); ++it) {
341 const auto normalizedCountry = normalizeCountryName(
QString::fromUtf8(cache->countryStringTableLookup((*it).value)));
342 if (normalizedName == normalizedCountry) {
347 checkSubstringMatch(normalizedName, normalizedCountry, (*it).key, substrMatch);
351 const auto langs = KCatalog::availableCatalogLanguages(
"iso_3166-1");
352 for (
const auto &lang : langs) {
353 const auto catalog = KCatalog(
"iso_3166-1", lang);
354 for (
auto it = cache->countryNameMapBegin(); it != cache->countryNameMapEnd(); ++it) {
355 const auto normalizedCountry = normalizeCountryName(catalog.translate(cache->countryStringTableLookup((*it).value)));
356 if (normalizedName == normalizedCountry) {
361 checkSubstringMatch(normalizedName, normalizedCountry, (*it).key, substrMatch);
366 if (substrMatch != std::numeric_limits<uint16_t>::max() && substrMatch != 0) {
373 if (normalizedName.size() == 3) {
376 if (normalizedName.size() == 2) {
386 auto cache = IsoCodesCache::instance();
387 cache->loadIso3166_1();
388 l.
reserve(cache->countryCount());
389 std::transform(cache->countryNameMapBegin(), cache->countryNameMapEnd(), std::back_inserter(l), [](
auto entry) {
399 const auto tzIds = timeZoneIds();
402 std::transform(tzIds.begin(), tzIds.end(), std::back_inserter(l), [](
const char *tzId) {
403 return QString::fromUtf8(tzId);
408#include "moc_kcountry.cpp"
Information about an ISO 3166-2 country subdivision.
Information about an ISO 3166-1 country.
static KCountry fromLocation(float latitude, float longitude)
Looks up the country at the given geographic coordinate.
KCountry()
Creates an invalid/empty KCountry instance.
static QList< KCountry > allCountries()
List all countries.
bool isValid() const
Returns false if this is an empty/invalid/default constructed instance, true otherwise.
static KCountry fromName(QStringView name)
Attempts to identify the country from the given name.
static KCountry fromQLocale(QLocale::Country country)
Returns a KCountry instance matching the given QLocale::Country code.
QLocale::Country country() const
Returns the QLocale::Country value matching this country, or QLocale::AnyCountry if there is none.
static KCountry fromAlpha2(QStringView alpha2Code)
Create a KCountry instance from an ISO 3166-1 alpha 2 code.
static KCountry fromAlpha3(QStringView alpha3Code)
Create a KCountry instance from an ISO 3166-1 alpha 3 code.
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
Translate a string from domain and substitute any arguments.
bool contains(const AT &value) const const
void push_back(parameter_type value)
void reserve(qsizetype size)
Territory codeToTerritory(QStringView territoryCode)
QList< QLocale > matchingLocales(QLocale::Language language, QLocale::Script script, QLocale::Territory territory)
QString territoryToCode(Territory territory)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
void reserve(qsizetype size)
qsizetype size() const const
bool endsWith(QChar ch) const const
bool isEmpty() const const
bool startsWith(QChar ch) const const