]> granicus.if.org Git - php/commitdiff
@Added version_compare() function (Stig)
authorStig Bakken <ssb@php.net>
Sat, 6 Oct 2001 23:22:37 +0000 (23:22 +0000)
committerStig Bakken <ssb@php.net>
Sat, 6 Oct 2001 23:22:37 +0000 (23:22 +0000)
ext/standard/tests/versioning/version_compare.phpt [new file with mode: 0644]
ext/standard/versioning.c

diff --git a/ext/standard/tests/versioning/version_compare.phpt b/ext/standard/tests/versioning/version_compare.phpt
new file mode 100644 (file)
index 0000000..6045c41
--- /dev/null
@@ -0,0 +1,75 @@
+--TEST--
+version_compare test
+--FILE--
+<?php
+
+$special_forms = array("-dev", "a1", "b1", "RC1", "", "pl1");
+test("1", "2");
+test("10", "2");
+test("1.0", "1.1");
+test("1.2", "1.0.1");
+foreach ($special_forms as $f1) {
+    foreach ($special_forms as $f2) {
+       test("1.0$f1", "1.0$f2");
+    }
+}
+
+
+function test($v1, $v2) {
+    $compare = version_compare($v1, $v2);
+    switch ($compare) {
+       case -1:
+           print "$v1 < $v2\n";
+           break;
+       case 1:
+           print "$v1 > $v2\n";
+           break;
+       case 0:
+       default:
+           print "$v1 = $v2\n";
+           break;
+    }
+}
+
+?>
+--EXPECT--
+1 < 2
+10 > 2
+1.0 < 1.1
+1.2 > 1.0.1
+1.0-dev = 1.0-dev
+1.0-dev < 1.0a1
+1.0-dev < 1.0b1
+1.0-dev < 1.0RC1
+1.0-dev < 1.0
+1.0-dev < 1.0pl1
+1.0a1 > 1.0-dev
+1.0a1 = 1.0a1
+1.0a1 < 1.0b1
+1.0a1 < 1.0RC1
+1.0a1 < 1.0
+1.0a1 < 1.0pl1
+1.0b1 > 1.0-dev
+1.0b1 > 1.0a1
+1.0b1 = 1.0b1
+1.0b1 < 1.0RC1
+1.0b1 < 1.0
+1.0b1 < 1.0pl1
+1.0RC1 > 1.0-dev
+1.0RC1 > 1.0a1
+1.0RC1 > 1.0b1
+1.0RC1 = 1.0RC1
+1.0RC1 < 1.0
+1.0RC1 < 1.0pl1
+1.0 > 1.0-dev
+1.0 > 1.0a1
+1.0 > 1.0b1
+1.0 > 1.0RC1
+1.0 = 1.0
+1.0 < 1.0pl1
+1.0pl1 > 1.0-dev
+1.0pl1 > 1.0a1
+1.0pl1 > 1.0b1
+1.0pl1 > 1.0RC1
+1.0pl1 > 1.0
+1.0pl1 = 1.0pl1
index 6d56e760ea3728e2d565ff1eb513bd64fb768abd..cad5c6d9d405b528c15e2ccb17fa7baa23ba3648 100644 (file)
 #include <sys/types.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <string.h>
 #include "php.h"
 #include "php_versioning.h"
 
+#define sign(n) ((n)<0?-1:((n)>0?1:0))
+
 PHPAPI char *
 php_canonicalize_version(const char *version)
 {
@@ -78,18 +81,18 @@ compare_special_version_forms(const char *form1, const char *form2)
        };
 
        for (pp = special_forms, i = 0; *pp != NULL; pp++, i++) {
-               if (strcmp(form1, *pp) == 0) {
+               if (strncmp(form1, *pp, strlen(*pp)) == 0) {
                        found1 = i;
                        break;
                }
        }
        for (pp = special_forms, i = 0; *pp != NULL; pp++, i++) {
-               if (strcmp(form2, *pp) == 0) {
+               if (strncmp(form2, *pp, strlen(*pp)) == 0) {
                        found2 = i;
                        break;
                }
        }
-       return abs(found1 - found2);
+       return sign(found1 - found2);
 }
 
 PHPAPI int
@@ -114,7 +117,7 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2)
                        /* compare element numerically */
                        l1 = strtol(p1, NULL, 10);
                        l2 = strtol(p2, NULL, 10);
-                       compare = abs(l1 - l2);
+                       compare = sign(l1 - l2);
                } else if (!isdigit(*p1) && !isdigit(*p2)) {
                        /* compare element names */
                        compare = compare_special_version_forms(p1, p2);
@@ -138,16 +141,16 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2)
        }
        if (compare == 0) {
                if (n1 != NULL) {
-                       if (isdigit(*n1)) {
+                       if (isdigit(*p1)) {
                                compare = 1;
                        } else {
-                               compare = compare_special_version_forms(n1, "#N#");
+                               compare = php_version_compare(p1, "#N#");
                        }
                } else if (n2 != NULL) {
-                       if (isdigit(*n2)) {
+                       if (isdigit(*p2)) {
                                compare = -1;
                        } else {
-                               compare = compare_special_version_forms("#N", n2);
+                               compare = php_version_compare("#N#", p2);
                        }
                }
        }