File indexing completed on 2024-05-05 04:42:47
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "ifoptutil.h" 0008 0009 #include <QDebug> 0010 #include <QStringView> 0011 0012 using namespace KPublicTransport; 0013 0014 bool IfoptUtil::isValid(QStringView ifopt) 0015 { 0016 if (ifopt.size() < 6) { 0017 return false; 0018 } 0019 0020 qsizetype pos = 0; 0021 int elementCount = 1; 0022 do { 0023 const auto idx = ifopt.indexOf(QLatin1Char(':'), pos); 0024 elementCount += (idx > pos) ? 1 : 0; 0025 if (idx == pos || idx == ifopt.size() - 1 || (elementCount < 3 && idx < 0)) { 0026 return false; 0027 } 0028 pos = idx + 1; 0029 } while (pos != 0 && pos < ifopt.size()); 0030 0031 return elementCount >= 3 && elementCount <= 5 && ifopt[0].isLetter() && ifopt[1].isLetter() && ifopt[2] == QLatin1Char(':'); 0032 } 0033 0034 static QStringView ifoptPrefix(QStringView ifopt, int elementCount) 0035 { 0036 qsizetype pos = 0; 0037 for (int i = 0; i < elementCount; i++) { 0038 pos = ifopt.indexOf(QLatin1Char(':'), pos) + 1; 0039 } 0040 return ifopt.left(pos - 1); 0041 } 0042 0043 QStringView IfoptUtil::country(QStringView ifopt) 0044 { 0045 return ifoptPrefix(ifopt, 1); 0046 } 0047 0048 QStringView IfoptUtil::stopPlace(QStringView ifopt) 0049 { 0050 return ifoptPrefix(ifopt, 3); 0051 } 0052 0053 QStringView IfoptUtil::level(QStringView ifopt) 0054 { 0055 return ifoptPrefix(ifopt, 4); 0056 } 0057 0058 bool IfoptUtil::isSameStopPlace(QStringView lhs, QStringView rhs) 0059 { 0060 return stopPlace(lhs) == stopPlace(rhs); 0061 } 0062 0063 QStringView IfoptUtil::merge(QStringView lhs, QStringView rhs) 0064 { 0065 if (lhs.isEmpty()) { 0066 return rhs; 0067 } 0068 if (rhs.isEmpty()) { 0069 return lhs; 0070 } 0071 0072 if (lhs == rhs) { 0073 return lhs; 0074 } 0075 0076 if (level(lhs) == level(rhs)) { 0077 return level(lhs); 0078 } 0079 0080 return stopPlace(lhs); 0081 } 0082 0083 QString IfoptUtil::identifierType() 0084 { 0085 return QStringLiteral("ifopt"); 0086 }