Quellcode durchsuchen

Oh dear god the craziness.

Fixes version comparison for versions with letters in.
tags/0.6.3m1rc2
Chris Smith vor 15 Jahren
Ursprung
Commit
537061a0e0
2 geänderte Dateien mit 47 neuen und 5 gelöschten Zeilen
  1. 40
    5
      src/com/dmdirc/updater/Version.java
  2. 7
    0
      test/com/dmdirc/updater/VersionTest.java

+ 40
- 5
src/com/dmdirc/updater/Version.java Datei anzeigen

@@ -67,15 +67,50 @@ public class Version implements Comparable<Version> {
67 67
             final String myParts[] = this.strVersion.split("-");
68 68
             final String thParts[] = o.strVersion.split("-");
69 69
 
70
-            final String myFirstParts[] = myParts[0].split("\\.");
71
-            final String thFirstParts[] = thParts[0].split("\\.");
70
+            final String myFirstParts[] = myParts[0].split("\\.|(?=a|b|rc|m)");
71
+            final String thFirstParts[] = thParts[0].split("\\.|(?=a|b|rc|m)");
72 72
 
73 73
             for (int i = 0; i < Math.max(myFirstParts.length, thFirstParts.length); i++) {
74
-                final int myInt = myFirstParts.length > i ? Integer.parseInt(myFirstParts[i]) : 0;
75
-                final int thInt = thFirstParts.length > i ? Integer.parseInt(thFirstParts[i]) : 0;
74
+                final boolean myExists = myFirstParts.length > i;
75
+                final boolean thExists = thFirstParts.length > i;
76 76
 
77
-                if (myInt != thInt) {
77
+                final boolean myIsInt = myExists && myFirstParts[i].matches("[0-9]+");
78
+                final boolean thIsInt = thExists && thFirstParts[i].matches("[0-9]+");
79
+
80
+                final int myInt = myIsInt ? Integer.parseInt(myFirstParts[i]) : 0;
81
+                final int thInt = thIsInt ? Integer.parseInt(thFirstParts[i]) : 0;
82
+
83
+                // Please consult handwritten truth table in the care of
84
+                // Chris for an explanation of what the hell is going on here.
85
+                // If there's a bug in this code it should probably be
86
+                // rewritten.
87
+                if ((!myExists && !thExists)
88
+                        || (myIsInt && thIsInt && myInt == thInt)
89
+                        || (myExists && thExists && !myIsInt && !thIsInt
90
+                        && myFirstParts[i].equals(thFirstParts[i]))) {
91
+                    continue;
92
+                } else if ((!thExists && myIsInt)
93
+                        || (thExists && !thIsInt && (!myExists || myIsInt))) {
94
+                    return +1;
95
+                } else if ((thIsInt && !myExists)
96
+                        || (myExists && !myIsInt && (!thExists || thIsInt))) {
97
+                    return -1;
98
+                } else if (thIsInt && myIsInt) {
78 99
                     return myInt - thInt;
100
+                } else {
101
+                    final String myLetterParts[] = myFirstParts[i].split("(?=[0-9])", 2);
102
+                    final String thLetterParts[] = thFirstParts[i].split("(?=[0-9])", 2);
103
+
104
+                    if (myLetterParts[0].equals(thLetterParts[0])) {
105
+                        return Integer.parseInt(myLetterParts[1])
106
+                                - Integer.parseInt(thLetterParts[1]);
107
+                    } else if (myLetterParts[0].equals("m")
108
+                            || thLetterParts[0].equals("rc")
109
+                            || (myLetterParts[0].equals("a") && thLetterParts[0].equals("b"))) {
110
+                        return -1;
111
+                    } else {
112
+                        return +1;
113
+                    }
79 114
                 }
80 115
             }
81 116
 

+ 7
- 0
test/com/dmdirc/updater/VersionTest.java Datei anzeigen

@@ -62,6 +62,13 @@ public class VersionTest {
62 62
             {new Version("0.6-149-gaaaaaaa"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
63 63
             {new Version("0.6-149-gaaaaaaa"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
64 64
             {new Version("0.6.3"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
65
+            {new Version("0.6.3a1"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
66
+            {new Version("0.6.3b1"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
67
+            {new Version("0.6.3rc1"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
68
+            {new Version("0.6.3m1"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
69
+            {new Version("0.6.3m1b1"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
70
+            {new Version("0.6.3m1a1"), new Version("0.6.3m1b1"), 0, Integer.MIN_VALUE},
71
+            {new Version("0.6.3m1a1"), new Version("0.6.3m1a1-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
65 72
             {new Version("0.6.3-148-gaaaaaaa"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
66 73
             {new Version("0.6.3-148-gaaaaaaa"), new Version("0.6.3-148-gaaaaaaa"), 1, -1},
67 74
         });

Laden…
Abbrechen
Speichern