diff options
author | Konstantin Podsvirov <konstantin@podsvirov.pro> | 2017-08-28 14:25:27 +0300 |
---|---|---|
committer | Konstantin Podsvirov <konstantin@podsvirov.pro> | 2017-11-13 14:50:07 +0000 |
commit | 1f3eded578314fb903bf50447d614fd314bd0eec (patch) | |
tree | ddf8f4b80dab5386c8c3f881c46b940173e3c17a /src/libs/kdtools | |
parent | ee24c7aefce6acbcac7a51f7c8c53c84ecdc7eca (diff) |
Allow comparing non-numeric versions
Changes:
- Improve KDUpdater::compareVersion method;
- Add tst_CompareVersion test.
Task-number: QTIFW-948
Change-Id: If64f807cfb04e56d2bdd5fa250f456631df3f239
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
Diffstat (limited to 'src/libs/kdtools')
-rw-r--r-- | src/libs/kdtools/updatefinder.cpp | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/src/libs/kdtools/updatefinder.cpp b/src/libs/kdtools/updatefinder.cpp index ec1be8a4e..b64f922c3 100644 --- a/src/libs/kdtools/updatefinder.cpp +++ b/src/libs/kdtools/updatefinder.cpp @@ -590,34 +590,62 @@ int KDUpdater::compareVersion(const QString &v1, const QString &v2) if (v1 == v2) return 0; - // Split version numbers across "." - const QStringList v1_comps = v1.split(QRegExp(QLatin1String( "\\.|-"))); - const QStringList v2_comps = v2.split(QRegExp(QLatin1String( "\\.|-"))); + // Split version components across ".", "-" or "_" + QStringList v1_comps = v1.split(QRegExp(QLatin1String( "\\.|-|_"))); + QStringList v2_comps = v2.split(QRegExp(QLatin1String( "\\.|-|_"))); // Check each component of the version int index = 0; while (true) { - if (index == v1_comps.count() && index < v2_comps.count()) - return -1; - if (index < v1_comps.count() && index == v2_comps.count()) - return +1; + bool v1_ok = false; + bool v2_ok = false; + + if (index == v1_comps.count() && index < v2_comps.count()) { + v2_comps.at(index).toInt(&v2_ok); + return v2_ok ? -1 : +1; + } + if (index < v1_comps.count() && index == v2_comps.count()) { + v1_comps.at(index).toInt(&v1_ok); + return v1_ok ? +1 : -1; + } if (index >= v1_comps.count() || index >= v2_comps.count()) break; - bool v1_ok, v2_ok; - int v1_comp = v1_comps[index].toInt(&v1_ok); - int v2_comp = v2_comps[index].toInt(&v2_ok); + int v1_comp = v1_comps.at(index).toInt(&v1_ok); + int v2_comp = v2_comps.at(index).toInt(&v2_ok); if (!v1_ok) { - if (v1_comps[index] == QLatin1String("x")) + if (v1_comps.at(index) == QLatin1String("x")) return 0; } if (!v2_ok) { - if (v2_comps[index] == QLatin1String("x")) + if (v2_comps.at(index) == QLatin1String("x")) return 0; } - if (!v1_ok && !v2_ok) - return v1_comps[index].compare(v2_comps[index]); + if (!v1_ok && !v2_ok) { + // try remove equal start + int i = 0; + while (i < v1_comps.at(index).size() + && i < v2_comps.at(index).size() + && v1_comps.at(index).at(i) == v2_comps.at(index).at(i)) { + ++i; + } + if (i > 0) { + v1_comps[index] = v1_comps.at(index).mid(i); + v2_comps[index] = v2_comps.at(index).mid(i); + // compare again + continue; + } + } + if (!v1_ok || !v2_ok) { + int res = v1_comps.at(index).compare(v2_comps.at(index)); + if (res == 0) { + // v1_comps.at(index) == v2_comps(index) + ++index; + continue; + } + return res > 0 ? +1 : -1; + } if (v1_comp < v2_comp) return -1; |