KPublicTransport

ifoptutil.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 "ifoptutil.h"
8
9#include <QDebug>
10#include <QStringView>
11
12using namespace KPublicTransport;
13
15{
16 if (ifopt.size() < 6) {
17 return false;
18 }
19
20 qsizetype pos = 0;
21 int elementCount = 1;
22 do {
23 const auto idx = ifopt.indexOf(QLatin1Char(':'), pos);
24 elementCount += (idx > pos) ? 1 : 0;
25 if (idx == pos || idx == ifopt.size() - 1 || (elementCount < 3 && idx < 0)) {
26 return false;
27 }
28 pos = idx + 1;
29 } while (pos != 0 && pos < ifopt.size());
30
31 return elementCount >= 3 && elementCount <= 5 && ifopt[0].isLetter() && ifopt[1].isLetter() && ifopt[2] == QLatin1Char(':');
32}
33
34static QStringView ifoptPrefix(QStringView ifopt, int elementCount)
35{
36 qsizetype pos = 0;
37 for (int i = 0; i < elementCount; i++) {
38 pos = ifopt.indexOf(QLatin1Char(':'), pos) + 1;
39 }
40 return ifopt.left(pos - 1);
41}
42
44{
45 return ifoptPrefix(ifopt, 1);
46}
47
49{
50 return ifoptPrefix(ifopt, 3);
51}
52
54{
55 return ifoptPrefix(ifopt, 4);
56}
57
59{
60 return stopPlace(lhs) == stopPlace(rhs);
61}
62
64{
65 if (lhs.isEmpty()) {
66 return rhs;
67 }
68 if (rhs.isEmpty()) {
69 return lhs;
70 }
71
72 if (lhs == rhs) {
73 return lhs;
74 }
75
76 if (level(lhs) == level(rhs)) {
77 return level(lhs);
78 }
79
80 return stopPlace(lhs);
81}
82
84{
85 return QStringLiteral("ifopt");
86}
bool isSameStopPlace(QStringView lhs, QStringView rhs)
Checks whether two valid IFOPT ids refer to the same stop place.
Definition ifoptutil.cpp:58
QStringView country(QStringView ifopt)
Returns the country identifier for the given (valid!) IFOPT identifier ifopt.
Definition ifoptutil.cpp:43
QStringView stopPlace(QStringView ifopt)
Returns the stop place identifier for the given (valid!) IFOPT identifier ifopt.
Definition ifoptutil.cpp:48
QString identifierType()
The identifier type for use in Location::identifer for IFOPT ids.
Definition ifoptutil.cpp:83
QStringView merge(QStringView lhs, QStringView rhs)
Merge two IFOPT ids that refer to the same stop place while retaining the maximum level of detail.
Definition ifoptutil.cpp:63
bool isValid(QStringView ifopt)
Check if ifopt is a valid IFOPT identifier.
Definition ifoptutil.cpp:14
QStringView level(QStringView ifopt)
Returns the stop level identifier for the given (valid!) IFOPT identifier ifopt.
Definition ifoptutil.cpp:53
Query operations and data types for accessing realtime public transport information from online servi...
QStringView left(qsizetype length) const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype size() 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.