diff options
-rw-r--r-- | ext/standard/tests/versioning/version_compare.phpt | 75 | ||||
-rw-r--r-- | ext/standard/versioning.c | 19 |
2 files changed, 86 insertions, 8 deletions
diff --git a/ext/standard/tests/versioning/version_compare.phpt b/ext/standard/tests/versioning/version_compare.phpt new file mode 100644 index 0000000000..6045c41735 --- /dev/null +++ b/ext/standard/tests/versioning/version_compare.phpt @@ -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 diff --git a/ext/standard/versioning.c b/ext/standard/versioning.c index 6d56e760ea..cad5c6d9d4 100644 --- a/ext/standard/versioning.c +++ b/ext/standard/versioning.c @@ -23,9 +23,12 @@ #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); } } } |