From 89bd184ea01356c3d7798be911440c7fe2aa154a Mon Sep 17 00:00:00 2001 From: Stig Bakken Date: Sat, 6 Oct 2001 23:22:37 +0000 Subject: [PATCH] @Added version_compare() function (Stig) --- .../tests/versioning/version_compare.phpt | 75 +++++++++++++++++++ ext/standard/versioning.c | 19 +++-- 2 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 ext/standard/tests/versioning/version_compare.phpt 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-- + $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 #include #include +#include #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); } } } -- 2.40.0