9#include "isocodescache_p.h"
10#include "kcatalog_p.h"
13#include "spatial_index_p.h"
14#include "timezonedata_p.h"
28KCountry::~KCountry() =
default;
32bool KCountry::operator==(
const KCountry &other)
const
37bool KCountry::operator!=(
const KCountry &other)
const
61 const auto cache = IsoCodesCache::instance();
62 const auto it = std::find_if(cache->countryAlpha3MapBegin(), cache->countryAlpha3MapEnd(), [
this](
auto entry) {
63 return entry.value == d;
65 if (it != cache->countryAlpha3MapEnd()) {
66 uint16_t alpha3Key = (*it).key;
68 code[2] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
69 alpha3Key /= IsoCodes::AlphaNumKeyFactor;
70 code[1] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
71 alpha3Key /= IsoCodes::AlphaNumKeyFactor;
72 code[0] =
QLatin1Char(IsoCodes::mapFromAlphaNumKey(alpha3Key));
84 auto cache = IsoCodesCache::instance();
85 cache->loadIso3166_1();
86 const auto it = std::lower_bound(cache->countryNameMapBegin(), cache->countryNameMapEnd(), d);
87 if (it != cache->countryNameMapEnd() && (*it).key == d) {
88 return i18nd(
"iso_3166-1", cache->countryStringTableLookup((*it).value));
100 char flagA[] =
"\xF0\x9F\x87\xA6";
101 flagA[3] = 0xA6 + ((d >> 8) -
'A');
103 flagA[3] = 0xA6 + ((d & 0xff) -
'A');
124 const auto countryIt = std::lower_bound(TimezoneData::countryTimezoneMapBegin(), TimezoneData::countryTimezoneMapEnd(), d);
125 if (countryIt != TimezoneData::countryTimezoneMapEnd() && (*countryIt).key == d) {
126 tzs.
push_back(TimezoneData::ianaIdLookup((*countryIt).value));
130 const auto [subdivBegin, subdivEnd] =
131 std::equal_range(TimezoneData::subdivisionTimezoneMapBegin(), TimezoneData::subdivisionTimezoneMapEnd(), d, [](
auto lhs,
auto rhs) {
132 if constexpr (std::is_same_v<
decltype(lhs), uint16_t>)
133 return lhs < (rhs.key >> 16);
135 return (lhs.key >> 16) < rhs;
137 for (
auto it = subdivBegin; it != subdivEnd; ++it) {
138 const auto tzId = TimezoneData::ianaIdLookup((*it).value);
155 for (
const auto &l : ls) {
159 qCDebug(KI18NLD) <<
"conflicting currency information in QLocale for" << alpha2();
173 auto cache = IsoCodesCache::instance();
174 cache->loadIso3166_2();
178 auto it = std::lower_bound(cache->subdivisionNameMapBegin(), cache->subdivisionNameMapEnd(), d, [](
auto lhs,
auto rhs) {
179 return (lhs.key >> 16) < rhs;
182 auto [parentBegin, parentEnd] = std::equal_range(cache->subdivisionParentMapBegin(), cache->subdivisionParentMapEnd(), d, [](
auto lhs,
auto rhs) {
183 if constexpr (std::is_same_v<decltype(lhs), uint16_t>)
184 return lhs < (rhs.key >> 16);
186 return (lhs.key >> 16) < rhs;
189 for (; it != cache->subdivisionNameMapEnd() && ((*it).key >> 16) == d; ++it) {
190 if (!std::binary_search(parentBegin, parentEnd, (*it).key)) {
200static uint16_t validatedAlpha2Key(uint16_t alpha2Key)
206 auto cache = IsoCodesCache::instance();
207 cache->loadIso3166_1();
208 const auto it = std::lower_bound(cache->countryNameMapBegin(), cache->countryNameMapEnd(), alpha2Key);
209 if (it != cache->countryNameMapEnd() && (*it).key == alpha2Key) {
218 c.d = validatedAlpha2Key(IsoCodes::alpha2CodeToKey(alpha2Code));
228 c.d = validatedAlpha2Key(IsoCodes::alpha2CodeToKey(alpha2Code, std::strlen(alpha2Code)));
232static uint16_t alpha3Lookup(uint16_t alpha3Key)
238 auto cache = IsoCodesCache::instance();
239 cache->loadIso3166_1();
240 const auto it = std::lower_bound(cache->countryAlpha3MapBegin(), cache->countryAlpha3MapEnd(), alpha3Key);
241 if (it != cache->countryAlpha3MapEnd() && (*it).key == alpha3Key) {
250 c.d = alpha3Lookup(IsoCodes::alpha3CodeToKey(alpha3Code));
260 c.d = alpha3Lookup(IsoCodes::alpha3CodeToKey(alpha3Code, std::strlen(alpha3Code)));
266 const auto entry = SpatialIndex::lookup(latitude, longitude);
268 c.d = entry.m_subdiv >> 16;
281 for (
const auto c : name) {
287 switch (c.category()) {
311 res.
push_back(c.decomposition().at(0).toCaseFolded());
323 auto idx = haystack.
indexOf(needle);
327 if (idx > 0 && !haystack[idx - 1].isSpace()) {
330 idx += needle.
size();
331 return idx >= haystack.
size() || haystack[idx].isSpace();
336 if (result == std::numeric_limits<uint16_t>::max() || result == code || rhs.
isEmpty()) {
339 const auto matches = isSeparatedSubstring(lhs, rhs) || isSeparatedSubstring(rhs, lhs);
344 result = result == 0 ? code : std::numeric_limits<uint16_t>::max();
352 const auto normalizedName = normalizeCountryName(name);
354 auto cache = IsoCodesCache::instance();
355 cache->loadIso3166_1();
357 uint16_t substrMatch = 0;
360 for (
auto it = cache->countryNameMapBegin(); it != cache->countryNameMapEnd(); ++it) {
361 const auto normalizedCountry = normalizeCountryName(
QString::fromUtf8(cache->countryStringTableLookup((*it).value)));
362 if (normalizedName == normalizedCountry) {
367 checkSubstringMatch(normalizedName, normalizedCountry, (*it).key, substrMatch);
371 const auto langs = KCatalog::availableCatalogLanguages(
"iso_3166-1");
372 for (
const auto &lang : langs) {
373 const auto catalog = KCatalog(
"iso_3166-1", lang);
374 for (
auto it = cache->countryNameMapBegin(); it != cache->countryNameMapEnd(); ++it) {
375 const auto normalizedCountry = normalizeCountryName(catalog.translate(cache->countryStringTableLookup((*it).value)));
376 if (normalizedName == normalizedCountry) {
381 checkSubstringMatch(normalizedName, normalizedCountry, (*it).key, substrMatch);
386 if (substrMatch != std::numeric_limits<uint16_t>::max() && substrMatch != 0) {
393 if (normalizedName.size() == 3) {
396 if (normalizedName.size() == 2) {
406 auto cache = IsoCodesCache::instance();
407 cache->loadIso3166_1();
408 l.
reserve(cache->countryCount());
409 std::transform(cache->countryNameMapBegin(), cache->countryNameMapEnd(), std::back_inserter(l), [](
auto entry) {
419 const auto tzIds = timeZoneIds();
422 std::transform(tzIds.begin(), tzIds.end(), std::back_inserter(l), [](
const char *tzId) {
423 return QString::fromUtf8(tzId);
428#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 isSpace(char32_t ucs4)
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
QString trimmed() const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype size() const const