swift
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 
10 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc, CCountry, CCountryList)
11 
12 namespace swift::misc
13 {
15 
17  {
18  const QString iso(isoCode.trimmed().toUpper());
19  if (!CCountry::isValidIsoCode(iso)) { return {}; }
20  return IDatastoreObjectList::findByKey(isoCode);
21  }
22 
24  {
25  if (candidate.isEmpty()) { return {}; }
26 
27  thread_local const QRegularExpression reg("^[a-z]+", QRegularExpression::CaseInsensitiveOption);
28  const QRegularExpressionMatch match = reg.match(candidate);
29  const QString cn(match.hasMatch() ? match.captured(0) : candidate);
30  const CCountryList countries =
31  this->findBy([&](const CCountry &country) { return country.matchesCountryName(cn); });
32 
33  if (countries.isEmpty()) { return this->findFirstByAlias(cn); }
34  if (countries.size() < 2) { return countries.frontOrDefault(); }
35 
36  // find best match by further reducing
37  for (const CCountry &c : countries)
38  {
39  if (c.getName() == cn) { return c; }
40  if (c.getName().startsWith(cn, Qt::CaseInsensitive)) { return c; }
41  if (cn.startsWith(c.getName(), Qt::CaseInsensitive)) { return c; }
42  }
43  return countries.front();
44  }
45 
47  {
48  if (alias.isEmpty()) { return {}; }
49  const QString a(alias.toUpper().trimmed());
50  for (const CCountry &country : (*this))
51  {
52  if (country.matchesAlias(a)) { return country; }
53  }
54  return {};
55  }
56 
58  {
59  QStringList sl;
60  for (const CCountry &country : (*this))
61  {
62  const QString s = country.getCombinedStringIsoName();
63  if (s.isEmpty()) { continue; }
64  sl.append(s);
65  }
66  if (sorted) { sl.sort(); }
67  return sl;
68  }
69 
71  {
72  QStringList sl;
73  for (const CCountry &country : (*this))
74  {
75  const QString s = country.getCombinedStringNameIso();
76  if (s.isEmpty()) { continue; }
77  sl.append(s);
78  }
79  if (sorted) { sl.sort(); }
80  return sl;
81  }
82 
84  {
85  QStringList sl;
86  for (const CCountry &country : (*this))
87  {
88  const QString s = country.getName();
89  if (s.isEmpty()) { continue; }
90  sl.append(s);
91  }
92  if (sorted) { sl.sort(); }
93  return sl;
94  }
95 
97  {
98  QStringList sl;
99  for (const CCountry &country : (*this))
100  {
101  const QString s = country.getIsoCode();
102  if (s.length() != 2) { continue; }
103  sl.append(s);
104  }
105  if (sorted) { sl.sort(); }
106  return sl;
107  }
108 
110  {
111  QStringList sl;
112  for (const CCountry &country : (*this))
113  {
114  const QString s = country.getIso3Code();
115  if (s.length() != 3) { continue; }
116  sl.append(s);
117  }
118  if (sorted) { sl.sort(); }
119  return sl;
120  }
121 
123  {
124  CCountryList countries;
125  for (const QJsonValue &value : array)
126  {
127  CCountry country(CCountry::fromDatabaseJson(value.toObject()));
128  countries.push_back(country);
129  }
130  return countries;
131  }
132 } // namespace swift::misc
bool matchesCountryName(const QString &name) const
Matches country name.
Definition: country.cpp:77
static bool isValidIsoCode(const QString &isoCode)
Valid country iso code.
Definition: country.cpp:193
static CCountry fromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
From our database JSON format.
Definition: country.cpp:170
Value object encapsulating a list of countries.
Definition: countrylist.h:32
static CCountryList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
CCountry findFirstByAlias(const QString &alias) const
Find first by alias.
Definition: countrylist.cpp:46
CCountry findBestMatchByCountryName(const QString &candidate) const
Find "best match" by country.
Definition: countrylist.cpp:23
QStringList toIso3List(bool sorted=false) const
All ISO 3 codes.
CCountry findByIsoCode(const QString &isoCode) const
Find by ISO code.
Definition: countrylist.cpp:16
QStringList toIsoNameList(bool sorted=false) const
ISO/name string list.
Definition: countrylist.cpp:57
QStringList toNameList(bool sorted=false) const
Name string list.
Definition: countrylist.cpp:83
QStringList toIsoList(bool sorted=false) const
All ISO codes.
Definition: countrylist.cpp:96
QStringList toNameIsoList(bool sorted=false) const
Name/ISO string list.
Definition: countrylist.cpp:70
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
Return a copy containing only those elements for which a given predicate returns true.
Definition: sequence.h:398
Q_REQUIRED_RESULT CSequence sorted(Predicate p) const
Return a copy sorted by a given comparator predicate.
Definition: sequence.h:583
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
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)
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