Commit e91e1fb6 authored by Volker Krause's avatar Volker Krause
Browse files

Implement merging of IFOPT identifiers

Will be needed during Location merging.
parent b0bb1892
Pipeline #64233 passed with stage
in 25 seconds
......@@ -58,6 +58,21 @@ private Q_SLOTS:
QVERIFY(IfoptUtil::isSameStopPlace(s("de:08115:4512:5:B"), s("de:08115:4512:2:1")));
QVERIFY(!IfoptUtil::isSameStopPlace(s("de:08115:4512:5:B"), s("de:08115:4513:2:1")));
}
void testCountry()
{
QCOMPARE(IfoptUtil::country(s("de:08115:4512:5:B")), s("de"));
}
void testMerge()
{
QCOMPARE(IfoptUtil::merge(s("de:08115:4512:5:B"), s("de:08115:4512")), s("de:08115:4512"));
QCOMPARE(IfoptUtil::merge(s("de:08115:4512:5:B"), s("de:08115:4512:5:A")), s("de:08115:4512:5"));
QCOMPARE(IfoptUtil::merge(s("de:08115:4512:5:B"), s("de:08115:4512:5")), s("de:08115:4512:5"));
QCOMPARE(IfoptUtil::merge(s("de:08115:4512:5:B"), s("de:08115:4512:1:2")), s("de:08115:4512"));
QCOMPARE(IfoptUtil::merge(s("de:08115:4512:5:B"), s("de:08115:4512:5:B")), s("de:08115:4512:5:B"));
QCOMPARE(IfoptUtil::merge(s("de:08115:4512"), s("de:08115:4512")), s("de:08115:4512"));
}
};
QTEST_APPLESS_MAIN(IfoptTest)
......
......@@ -31,16 +31,44 @@ bool IfoptUtil::isValid(QStringView ifopt)
return elementCount >= 3 && elementCount <= 5 && ifopt[0].isLetter() && ifopt[1].isLetter() && ifopt[2] == QLatin1Char(':');
}
QStringView IfoptUtil::stopPlace(QStringView ifopt)
static QStringView ifoptPrefix(QStringView ifopt, int elementCount)
{
qsizetype pos = 0;
for (int i = 0; i < 3; i++) {
for (int i = 0; i < elementCount; i++) {
pos = ifopt.indexOf(QLatin1Char(':'), pos) + 1;
}
return ifopt.left(pos - 1);
}
QStringView IfoptUtil::country(QStringView ifopt)
{
return ifoptPrefix(ifopt, 1);
}
QStringView IfoptUtil::stopPlace(QStringView ifopt)
{
return ifoptPrefix(ifopt, 3);
}
QStringView IfoptUtil::level(QStringView ifopt)
{
return ifoptPrefix(ifopt, 4);
}
bool IfoptUtil::isSameStopPlace(QStringView lhs, QStringView rhs)
{
return stopPlace(lhs) == stopPlace(rhs);
}
QStringView IfoptUtil::merge(QStringView lhs, QStringView rhs)
{
if (lhs == rhs) {
return lhs;
}
if (level(lhs) == level(rhs)) {
return level(lhs);
}
return stopPlace(lhs);
}
......@@ -21,12 +21,23 @@ namespace IfoptUtil
/** Check if @p ifopt is a valid IFOPT identifier. */
bool isValid(QStringView ifopt);
/** Returns the country identifier for the given (valid!) IFOPT identifier @p ifopt. */
QStringView country(QStringView ifopt);
/** Returns the stop place identifier for the given (valid!) IFOPT identifier @p ifopt. */
QStringView stopPlace(QStringView ifopt);
/** Returns the stop level identifier for the given (valid!) IFOPT identifier @p ifopt. */
QStringView level(QStringView ifopt);
/** Checks whether two valid IFOPT ids refer to the same stop place. */
bool isSameStopPlace(QStringView lhs, QStringView rhs);
/** Merge two IFOPT ids that refer to the same stop place while retaining the maximum level of detail.
* Precondition: isValid(lhs) && isValid(rhs) && isSameStopPlace(lhs, rhs)
*/
QStringView merge(QStringView lhs, QStringView rhs);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment