KPublicTransport

uicstationcode.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "uicstationcode.h"
8
9#include <QString>
10#include <QStringView>
11
12using namespace Qt::Literals::StringLiterals;
13using namespace KPublicTransport;
14
15bool UicStationCode::isValid(QStringView id, const std::vector<uint8_t> &allowedCountryCodes)
16{
17 // too short, or not a number
18 if (id.size() < 7 || std::any_of(id.begin(), id.end(), [](QChar c) { return !c.isDigit() || c.row() > 0; })) {
19 return false;
20 }
21
22 // too long, but just 0 prefixed
23 if (id.size() > 7 && std::any_of(id.begin(), id.begin() + id.size() - 7, [](QChar c) { return c != QLatin1Char('0'); })) {
24 return false;
25 }
26
27 // one of the explicitly allowed UIC country codes
28 if (!allowedCountryCodes.empty()) {
29 const uint8_t countryCode = id.mid(id.size() - 7, 2).toInt();
30 return std::binary_search(allowedCountryCodes.begin(), allowedCountryCodes.end(), countryCode);
31 }
32
33 // if no UIC country codes are explicitly allowed, insist on the right length
34 return id.size() == 7 && id.at(0) != QLatin1Char('0');
35}
36
38{
39 return id.mid(0, 2);
40}
41
43{
44 return u"uic"_s;
45}
bool isValid(QStringView id, const std::vector< uint8_t > &allowedCountryCodes={})
Returns true if id is a valid UIC station code.
QStringView country(QStringView id)
Returns the country code of a given valid UIC station code.
QString identifierType()
The identifier type for use in Location::identifer for UIC station ids.
Query operations and data types for accessing realtime public transport information from online servi...
bool isDigit(char32_t ucs4)
uchar row() const const
QStringView mid(qsizetype start, qsizetype length) const const
int toInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.