swift
Loading...
Searching...
No Matches
countrylist.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/countrylist.h"
5
6#include <QJsonValue>
7#include <QRegularExpression>
8#include <Qt>
9
10SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc, CCountry, CCountryList)
11
12namespace swift::misc
13{
15
17 {
18 const QString iso(isoCode.trimmed().toUpper());
20 {
21 return {};
22 }
23 return IDatastoreObjectList::findByKey(isoCode);
24 }
25
27 {
28 if (candidate.isEmpty())
29 {
30 return {};
31 }
32
33 thread_local const QRegularExpression reg("^[a-z]+", QRegularExpression::CaseInsensitiveOption);
34 const QRegularExpressionMatch match = reg.match(candidate);
35 const QString cn(match.hasMatch() ? match.captured(0) : candidate);
36 const CCountryList countries =
37 this->findBy([&](const CCountry &country) { return country.matchesCountryName(cn); });
38
39 if (countries.isEmpty())
40 {
41 return this->findFirstByAlias(cn);
42 }
43 if (countries.size() < 2)
44 {
45 return countries.frontOrDefault();
46 }
47
48 // find best match by further reducing
49 for (const CCountry &c : countries)
50 {
51 if (c.getName() == cn)
52 {
53 return c;
54 }
55 if (c.getName().startsWith(cn, Qt::CaseInsensitive))
56 {
57 return c;
58 }
59 if (cn.startsWith(c.getName(), Qt::CaseInsensitive))
60 {
61 return c;
62 }
63 }
64 return countries.front();
65 }
66
68 {
69 if (alias.isEmpty())
70 {
71 return {};
72 }
73 const QString a(alias.toUpper().trimmed());
74 for (const CCountry &country : (*this))
75 {
76 if (country.matchesAlias(a))
77 {
78 return country;
79 }
80 }
81 return {};
82 }
83
85 {
86 QStringList sl;
87 for (const CCountry &country : (*this))
88 {
89 const QString s = country.getCombinedStringIsoName();
90 if (s.isEmpty())
91 {
92 continue;
93 }
94 sl.append(s);
95 }
96 if (sorted)
97 {
98 sl.sort();
99 }
100 return sl;
101 }
102
104 {
105 QStringList sl;
106 for (const CCountry &country : (*this))
107 {
108 const QString s = country.getCombinedStringNameIso();
109 if (s.isEmpty())
110 {
111 continue;
112 }
113 sl.append(s);
114 }
115 if (sorted)
116 {
117 sl.sort();
118 }
119 return sl;
120 }
121
123 {
124 QStringList sl;
125 for (const CCountry &country : (*this))
126 {
127 const QString s = country.getName();
128 if (s.isEmpty())
129 {
130 continue;
131 }
132 sl.append(s);
133 }
134 if (sorted)
135 {
136 sl.sort();
137 }
138 return sl;
139 }
140
142 {
143 QStringList sl;
144 for (const CCountry &country : (*this))
145 {
146 const QString s = country.getIsoCode();
147 if (s.length() != 2)
148 {
149 continue;
150 }
151 sl.append(s);
152 }
153 if (sorted)
154 {
155 sl.sort();
156 }
157 return sl;
158 }
159
161 {
162 QStringList sl;
163 for (const CCountry &country : (*this))
164 {
165 const QString s = country.getIso3Code();
166 if (s.length() != 3)
167 {
168 continue;
169 }
170 sl.append(s);
171 }
172 if (sorted)
173 {
174 sl.sort();
175 }
176 return sl;
177 }
178
180 {
181 CCountryList countries;
182 for (const QJsonValue &value : array)
183 {
184 CCountry country(CCountry::fromDatabaseJson(value.toObject()));
185 countries.push_back(country);
186 }
187 return countries;
188 }
189} // namespace swift::misc
bool matchesCountryName(const QString &name) const
Matches country name.
Definition country.cpp:86
static bool isValidIsoCode(const QString &isoCode)
Valid country iso code.
Definition country.cpp:217
static CCountry fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
From our database JSON format.
Definition country.cpp:194
static CCountryList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
CCountry findFirstByAlias(const QString &alias) const
Find first by alias.
CSequence()=default
Default constructor.
CCountry findBestMatchByCountryName(const QString &candidate) const
Find "best match" by country.
QStringList toIso3List(bool sorted=false) const
All ISO 3 codes.
CCountry findByIsoCode(const QString &isoCode) const
Find by ISO code.
QStringList toIsoNameList(bool sorted=false) const
ISO/name string list.
QStringList toNameList(bool sorted=false) const
Name string list.
QStringList toIsoList(bool sorted=false) const
All ISO codes.
QStringList toNameIsoList(bool sorted=false) const
Name/ISO string list.
CCountryList()=default
Default constructor.
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
const_reference frontOrDefault() const
Access the first element, or a default-initialized value if the sequence is empty.
Definition sequence.h:239
CSequence findBy(Predicate p) const
Definition sequence.h:407
Q_REQUIRED_RESULT CSequence sorted(Predicate p) const
Definition sequence.h:610
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
reference front()
Access the first element.
Definition sequence.h:225
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Free functions in swift::misc.
void append(QList< T > &&value)
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(QAnyStringView name) const const
bool hasMatch() const const
bool isEmpty() const const
qsizetype length() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toUpper() const const
QString trimmed() const const
void sort(Qt::CaseSensitivity cs)
CaseInsensitive
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition sequence.h:63