cvs: php4 /ext/standard versioning.c /ext/standard/tests/versioning version_compare.phpt

From: Date: Sat, 06 Oct 2001 23:22:37 +0000
Subject: cvs: php4 /ext/standard versioning.c /ext/standard/tests/versioning version_compare.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7707@lists.php.net to get a copy of this message
ssb Sat Oct 6 19:22:37 2001 EDT Added files: /php4/ext/standard/tests/versioning version_compare.phpt Modified files: /php4/ext/standard versioning.c Log: @Added version_compare() function (Stig) Index: php4/ext/standard/versioning.c diff -u php4/ext/standard/versioning.c:1.1 php4/ext/standard/versioning.c:1.2 --- php4/ext/standard/versioning.c:1.1 Fri Oct 5 19:56:19 2001 +++ php4/ext/standard/versioning.c Sat Oct 6 19:22:36 2001 @@ -17,15 +17,18 @@ +----------------------------------------------------------------------+ */ -/* $Id: versioning.c,v 1.1 2001/10/05 23:56:19 ssb Exp $ */ +/* $Id: versioning.c,v 1.2 2001/10/06 23:22:36 ssb Exp $ */ #include <stdio.h> #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 @@ }; 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 @@ /* 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 @@ } 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); } } } Index: php4/ext/standard/tests/versioning/version_compare.phpt +++ php4/ext/standard/tests/versioning/version_compare.phpt --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

« previous php.cvs (#7707) next »