Skip to content

Commit 423fa17

Browse files
committed
VersionUtils.compare: fix bug with suffixes
When comparing e.g. "1" versus "a", the logic was stripping the "1" as leading and ultimately comparing "" with "a". This comparison happened to give the same result, because both "" and "1" compare less than "a", but for other strings like "%" which comes before the digits, the behavior differs.
1 parent ade6894 commit 423fa17

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/main/java/org/scijava/util/VersionUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,18 @@ private static String[] splitDots(final String s) {
146146
/** Compares one token of a multi-token version string. */
147147
private static int compareToken(final String t1, final String t2) {
148148
final int i1 = digitIndex(t1), i2 = digitIndex(t2);
149+
String suffix1 = t1, suffix2 = t2;
149150
if (i1 > 0 && i2 > 0) {
150151
// Versions start with digits; compare them numerically.
151152
final long d1 = Long.parseLong(t1.substring(0, i1));
152153
final long d2 = Long.parseLong(t2.substring(0, i2));
153154
if (d1 < d2) return -1;
154155
if (d1 > d2) return 1;
156+
suffix1 = t1.substring(i1);
157+
suffix2 = t2.substring(i2);
155158
}
156-
// Compare remaining characters lexicographically.
157-
return t1.substring(i1).compareTo(t2.substring(i2));
159+
// Compare lexicographically.
160+
return suffix1.compareTo(suffix2);
158161
}
159162

160163
/** Gets the subsequent index to all the given string's leading digits. */

src/test/java/org/scijava/util/VersionUtilsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void testCompare() {
8888

8989
// Check weird stuff.
9090
assertTrue(VersionUtils.compare("1", "a") < 0);
91+
assertTrue(VersionUtils.compare("1", "%") > 0);
9192
assertTrue(VersionUtils.compare("", "1") < 0);
9293
assertTrue(VersionUtils.compare("1", "1.") < 0);
9394
assertTrue(VersionUtils.compare("", ".") < 0);

0 commit comments

Comments
 (0)