FS#11439: Fix version comparison regression.
Improve string suffix handling by distinguishing between version number
separators (i.e. dots) and extended separators and additional version
characters. Corrects false update information displayed for 64bit binaries of
Rockbox Utility.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27201 a1c6a512-1295-4272-9138-f99709370657
diff --git a/rbutil/rbutilqt/base/utils.cpp b/rbutil/rbutilqt/base/utils.cpp
index 1452ada..bd2bce0 100644
--- a/rbutil/rbutilqt/base/utils.cpp
+++ b/rbutil/rbutilqt/base/utils.cpp
@@ -211,9 +211,9 @@
*/
int Utils::compareVersionStrings(QString s1, QString s2)
{
+ qDebug() << "[Utils] comparing version strings" << s1 << "and" << s2;
QString a = s1.trimmed();
QString b = s2.trimmed();
- qDebug() << "[Utils] comparing version strings" << a << "and" << b;
// if strings are identical return 0.
if(a.isEmpty())
return 1;
@@ -252,8 +252,19 @@
a.remove(QRegExp("^\\d*"));
b.remove(QRegExp("^\\d*"));
+ // If only one of the following characters is a dot that one is
+ // "greater" then anything else. Make sure it's followed by a number,
+ // Otherwise it might be the end of the string or suffix. Do this
+ // before version addon characters check to avoid stopping too early.
+ bool adot = a.contains(QRegExp("^[a-zA-Z]*\\.[0-9]"));
+ bool bdot = b.contains(QRegExp("^[a-zA-Z]*\\.[0-9]"));
+ if(adot && !bdot)
+ return -1;
+ if(!adot && bdot)
+ return 1;
// if number is immediately followed by a character consider it as
- // version addon (like 1.2.3b). In this case compare characters too.
+ // version addon (like 1.2.3b). In this case compare characters and end
+ // (version numbers like 1.2b.3 aren't handled).
QChar ltra;
QChar ltrb;
if(a.contains(QRegExp("^[a-zA-Z]")))
@@ -262,6 +273,7 @@
ltrb = b.at(0);
if(ltra != ltrb)
return (ltra < ltrb) ? 1 : -1;
+
// both are identical or no addon characters, ignore.
// remove modifiers and following dot.
a.remove(QRegExp("^[a-zA-Z]*\\."));
diff --git a/rbutil/rbutilqt/test/compareversion.cpp b/rbutil/rbutilqt/test/compareversion.cpp
index db1c62e..80bddf7 100644
--- a/rbutil/rbutilqt/test/compareversion.cpp
+++ b/rbutil/rbutilqt/test/compareversion.cpp
@@ -62,6 +62,12 @@
{ "test-1.2.3.tar.gz", "program-1.2.3.1.tar.bz2", 1 },
{ "program-1.2.3.zip", "program-1.2.3a.zip", 1 },
{ "program-1.2.3.tar.bz2", "2.0.0", 1 },
+ { "prog-1.2-64bit.tar.bz2", "prog-1.2.3.tar.bz2", 1 },
+ { "prog-1.2-64bit.tar.bz2", "prog-1.2-64bit.tar.bz2", 0 },
+ { "prog-1.2-64bit.tar.bz2", "prog-1.2.3-64bit.tar.bz2", 1 },
+ { "prog-1.2a-64bit.tar.bz2","prog-1.2b-64bit.tar.bz2", 1 },
+ { "prog-1.2-64bit.tar.bz2", "prog-1.2.3a-64bit.tar.bz2", 1 },
+ { "prog-1.2a-64bit.tar.bz2","prog-1.2.3-64bit.tar.bz2", 1 },
};
@@ -72,8 +78,9 @@
QCOMPARE(Utils::compareVersionStrings(testdata[i].first,
testdata[i].second), testdata[i].expected);
// inverse test possible because function return values are symmetrical.
- QCOMPARE(Utils::compareVersionStrings(testdata[i].second,
- testdata[i].first), -testdata[i].expected);
+ if(testdata[i].expected != 0)
+ QCOMPARE(Utils::compareVersionStrings(testdata[i].second,
+ testdata[i].first), -testdata[i].expected);
}
}