7#include "kcountrysubdivision.h"
9#include "isocodescache_p.h"
11#include "ki18n_logging.h"
13#include "spatial_index_p.h"
14#include "timezonedata_p.h"
26KCountrySubdivision::~KCountrySubdivision() =
default;
52 code += country().alpha2();
55 auto key = d & 0xffff;
57 const auto c = IsoCodes::mapFromAlphaNumKey(key);
61 key /= IsoCodes::AlphaNumKeyFactor;
73 auto cache = IsoCodesCache::instance();
74 cache->loadIso3166_2();
75 const auto it = std::lower_bound(cache->subdivisionNameMapBegin(), cache->subdivisionNameMapEnd(), d);
76 if (it != cache->subdivisionNameMapEnd() && (*it).key == d) {
77 return i18nd(
"iso_3166-2", cache->subdivisionStringTableLookup((*it).value));
96 auto cache = IsoCodesCache::instance();
97 cache->loadIso3166_2();
99 const auto it = std::lower_bound(cache->subdivisionParentMapBegin(), cache->subdivisionParentMapEnd(), d);
100 if (it != cache->subdivisionParentMapEnd() && (*it).key == d) {
101 s.d = (d & 0xffff0000) | (uint32_t)(*it).value;
114 const auto [subdivBegin, subdivEnd] = std::equal_range(TimezoneData::subdivisionTimezoneMapBegin(), TimezoneData::subdivisionTimezoneMapEnd(), d);
115 if (subdivBegin != subdivEnd) {
116 tzs.
reserve(std::distance(subdivBegin, subdivEnd));
117 for (
auto it = subdivBegin; it != subdivEnd; ++it) {
118 tzs.
push_back(TimezoneData::ianaIdLookup((*it).value));
123 const auto countryIt = std::lower_bound(TimezoneData::countryTimezoneMapBegin(), TimezoneData::countryTimezoneMapEnd(), uint16_t(d >> 16));
124 if (countryIt != TimezoneData::countryTimezoneMapEnd() && (*countryIt).key == (d >> 16)) {
125 tzs.
push_back(TimezoneData::ianaIdLookup((*countryIt).value));
138 auto cache = IsoCodesCache::instance();
139 cache->loadIso3166_2();
143 auto it = std::lower_bound(cache->subdivisionParentMapBegin(), cache->subdivisionParentMapEnd(), d >> 16, [](
auto lhs,
auto rhs) {
144 return (lhs.key >> 16) < rhs;
146 for (; it != cache->subdivisionParentMapEnd() && ((*it).key >> 16) == (d >> 16); ++it) {
147 if ((*it).value == (d & 0xffff)) {
157static uint32_t validatedSubdivisionKey(uint32_t key)
163 auto cache = IsoCodesCache::instance();
164 cache->loadIso3166_2();
166 const auto it = std::lower_bound(cache->subdivisionNameMapBegin(), cache->subdivisionNameMapEnd(), key);
167 if (it != cache->subdivisionNameMapEnd() && (*it).key == key) {
176 s.d = validatedSubdivisionKey(IsoCodes::subdivisionCodeToKey(code));
184 s.d = validatedSubdivisionKey(IsoCodes::subdivisionCodeToKey(code, std::strlen(code)));
191 const auto entry = SpatialIndex::lookup(latitude, longitude);
193 if (entry.m_subdiv & 0xffff) {
194 s.d = entry.m_subdiv;
199QStringList KCountrySubdivision::timeZoneIdsStringList()
const
201 const auto tzIds = timeZoneIds();
204 std::transform(tzIds.begin(), tzIds.end(), std::back_inserter(l), [](
const char *tzId) {
205 return QString::fromUtf8(tzId);
210#include "moc_kcountrysubdivision.cpp"
Information about an ISO 3166-2 country subdivision.
static KCountrySubdivision fromCode(QStringView code)
Create a KCountrySubdivision instance from an ISO 3166-2 code.
KCountrySubdivision()
Creates an invalid/empty KCountrySubdivision instance.
bool isValid() const
Returns false if this is an empty/invalid/default constructed instance, true otherwise.
static KCountrySubdivision fromLocation(float latitude, float longitude)
Looks up the country subdivision at the given geographic coordinate.
Information about an ISO 3166-1 country.
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
Translate a string from domain and substitute any arguments.
void push_back(parameter_type value)
void reserve(qsizetype size)
QString & insert(qsizetype position, QChar ch)
void reserve(qsizetype size)