PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - versioning.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 97 105 92.4 %
Date: 2014-10-14 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Stig Sæther Bakken <ssb@php.net>                             |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include <stdio.h>
      22             : #include <sys/types.h>
      23             : #include <ctype.h>
      24             : #include <stdlib.h>
      25             : #include <string.h>
      26             : #include "php.h"
      27             : #include "php_versioning.h"
      28             : 
      29             : #define sign(n) ((n)<0?-1:((n)>0?1:0))
      30             : 
      31             : /* {{{ php_canonicalize_version() */
      32             : 
      33             : PHPAPI char *
      34       27466 : php_canonicalize_version(const char *version)
      35             : {
      36       27466 :     int len = strlen(version);
      37       27466 :     char *buf = safe_emalloc(len, 2, 1), *q, lp, lq;
      38             :     const char *p;
      39             : 
      40       27466 :     if (len == 0) {
      41           0 :         *buf = '\0';
      42           0 :         return buf;
      43             :     }
      44             : 
      45       27466 :     p = version;
      46       27466 :     q = buf;
      47       27466 :     *q++ = lp = *p++;
      48             : 
      49      166876 :     while (*p) {
      50             : /*  s/[-_+]/./g;
      51             :  *  s/([^\d\.])([^\D\.])/$1.$2/g;
      52             :  *  s/([^\D\.])([^\d\.])/$1.$2/g;
      53             :  */
      54             : #define isdig(x) (isdigit(x)&&(x)!='.')
      55             : #define isndig(x) (!isdigit(x)&&(x)!='.')
      56             : #define isspecialver(x) ((x)=='-'||(x)=='_'||(x)=='+')
      57             : 
      58      111944 :                 lq = *(q - 1);
      59      112698 :                 if (isspecialver(*p)) {
      60         754 :                         if (lq != '.') {
      61         754 :                                 *q++ = '.';
      62             :                         }
      63      113295 :                 } else if ((isndig(lp) && isdig(*p)) || (isdig(lp) && isndig(*p))) {
      64        2105 :                         if (lq != '.') {
      65        2103 :                                 *q++ = '.';
      66             :                         }
      67        2105 :                         *q++ = *p;
      68      109085 :                 } else if (!isalnum(*p)) {
      69       52966 :                         if (lq != '.') {
      70       52966 :                                 *q++ = '.';
      71             :                         }
      72             :                 } else {
      73       56119 :                         *q++ = *p;
      74             :                 }
      75      111944 :                 lp = *p++;
      76             :     }
      77       27466 :     *q++ = '\0';
      78       27466 :     return buf;
      79             : }
      80             : 
      81             : /* }}} */
      82             : /* {{{ compare_special_version_forms() */
      83             : 
      84             : typedef struct {
      85             :         const char *name;
      86             :         int order;
      87             : } special_forms_t;
      88             : 
      89             : static int
      90         722 : compare_special_version_forms(char *form1, char *form2)
      91             : {
      92         722 :         int found1 = -1, found2 = -1;
      93             :         special_forms_t special_forms[11] = {
      94             :                 {"dev", 0},
      95             :                 {"alpha", 1},
      96             :                 {"a", 1},
      97             :                 {"beta", 2},
      98             :                 {"b", 2},
      99             :                 {"RC", 3},
     100             :                 {"rc", 3},
     101             :                 {"#", 4},
     102             :                 {"pl", 5},
     103             :                 {"p", 5},
     104             :                 {NULL, 0},
     105         722 :         };
     106             :         special_forms_t *pp;
     107             : 
     108        3994 :         for (pp = special_forms; pp && pp->name; pp++) {
     109        3993 :                 if (strncmp(form1, pp->name, strlen(pp->name)) == 0) {
     110         721 :                         found1 = pp->order;
     111         721 :                         break;
     112             :                 }
     113             :         }
     114        3994 :         for (pp = special_forms; pp && pp->name; pp++) {
     115        3993 :                 if (strncmp(form2, pp->name, strlen(pp->name)) == 0) {
     116         721 :                         found2 = pp->order;
     117         721 :                         break;
     118             :                 }
     119             :         }
     120         722 :         return sign(found1 - found2);
     121             : }
     122             : 
     123             : /* }}} */
     124             : /* {{{ php_version_compare() */
     125             : 
     126             : PHPAPI int
     127       13823 : php_version_compare(const char *orig_ver1, const char *orig_ver2)
     128             : {
     129             :         char *ver1;
     130             :         char *ver2;
     131             :         char *p1, *p2, *n1, *n2;
     132             :         long l1, l2;
     133       13823 :         int compare = 0;
     134             : 
     135       13823 :         if (!*orig_ver1 || !*orig_ver2) {
     136           0 :                 if (!*orig_ver1 && !*orig_ver2) {
     137           0 :                         return 0;
     138             :                 } else {
     139           0 :                         return *orig_ver1 ? 1 : -1;
     140             :                 }
     141             :         }
     142       13823 :         if (orig_ver1[0] == '#') {
     143          90 :                 ver1 = estrdup(orig_ver1);
     144             :         } else {
     145       13733 :                 ver1 = php_canonicalize_version(orig_ver1);
     146             :         }
     147       13823 :         if (orig_ver2[0] == '#') {
     148          90 :                 ver2 = estrdup(orig_ver2);
     149             :         } else {
     150       13733 :                 ver2 = php_canonicalize_version(orig_ver2);
     151             :         }
     152       13823 :         p1 = n1 = ver1;
     153       13823 :         p2 = n2 = ver2;
     154       41907 :         while (*p1 && *p2 && n1 && n2) {
     155       27766 :                 if ((n1 = strchr(p1, '.')) != NULL) {
     156       27353 :                         *n1 = '\0';
     157             :                 }
     158       27766 :                 if ((n2 = strchr(p2, '.')) != NULL) {
     159       27268 :                         *n2 = '\0';
     160             :                 }
     161       54810 :                 if (isdigit(*p1) && isdigit(*p2)) {
     162             :                         /* compare element numerically */
     163       27044 :                         l1 = strtol(p1, NULL, 10);
     164       27044 :                         l2 = strtol(p2, NULL, 10);
     165       27044 :                         compare = sign(l1 - l2);
     166        1442 :                 } else if (!isdigit(*p1) && !isdigit(*p2)) {
     167             :                         /* compare element names */
     168         720 :                         compare = compare_special_version_forms(p1, p2);
     169             :                 } else {
     170             :                         /* mix of names and numbers */
     171           2 :                         if (isdigit(*p1)) {
     172           1 :                                 compare = compare_special_version_forms("#N#", p2);
     173             :                         } else {
     174           1 :                                 compare = compare_special_version_forms(p1, "#N#");
     175             :                         }
     176             :                 }
     177       27766 :                 if (compare != 0) {
     178       13505 :                         break;
     179             :                 }
     180       14261 :                 if (n1 != NULL) {
     181       14036 :                         p1 = n1 + 1;
     182             :                 }
     183       14261 :                 if (n2 != NULL) {
     184       14033 :                         p2 = n2 + 1;
     185             :                 }
     186             :         }
     187       13823 :         if (compare == 0) {
     188         318 :                 if (n1 != NULL) {
     189          93 :                         if (isdigit(*p1)) {
     190           3 :                                 compare = 1;
     191             :                         } else {
     192          90 :                                 compare = php_version_compare(p1, "#N#");
     193             :                         }
     194         225 :                 } else if (n2 != NULL) {
     195          90 :                         if (isdigit(*p2)) {
     196           0 :                                 compare = -1;
     197             :                         } else {
     198          90 :                                 compare = php_version_compare("#N#", p2);
     199             :                         }
     200             :                 }
     201             :         }
     202       13823 :         efree(ver1);
     203       13823 :         efree(ver2);
     204       13823 :         return compare;
     205             : }
     206             : 
     207             : /* }}} */
     208             : /* {{{ proto int version_compare(string ver1, string ver2 [, string oper])
     209             :   Compares two "PHP-standardized" version number strings */
     210             : 
     211       13643 : PHP_FUNCTION(version_compare)
     212             : {
     213       13643 :         char *v1, *v2, *op = NULL;
     214       13643 :         size_t v1_len, v2_len, op_len = 0;
     215             :         int compare, argc;
     216             : 
     217       13643 :         argc = ZEND_NUM_ARGS();
     218       13643 :         if (zend_parse_parameters(argc TSRMLS_CC, "ss|s", &v1, &v1_len, &v2,
     219             :                                                           &v2_len, &op, &op_len) == FAILURE) {
     220           0 :                 return;
     221             :         }
     222       13643 :         compare = php_version_compare(v1, v2);
     223       13643 :         if (argc == 2) {
     224         286 :                 RETURN_LONG(compare);
     225             :         }
     226       13357 :         if (!strncmp(op, "<", op_len) || !strncmp(op, "lt", op_len)) {
     227         293 :                 RETURN_BOOL(compare == -1);
     228             :         }
     229       13064 :         if (!strncmp(op, "<=", op_len) || !strncmp(op, "le", op_len)) {
     230         117 :                 RETURN_BOOL(compare != 1);
     231             :         }
     232       12947 :         if (!strncmp(op, ">", op_len) || !strncmp(op, "gt", op_len)) {
     233         136 :                 RETURN_BOOL(compare == 1);
     234             :         }
     235       12811 :         if (!strncmp(op, ">=", op_len) || !strncmp(op, "ge", op_len)) {
     236       12440 :                 RETURN_BOOL(compare != -1);
     237             :         }
     238         371 :         if (!strncmp(op, "==", op_len) || !strncmp(op, "=", op_len) || !strncmp(op, "eq", op_len)) {
     239         193 :                 RETURN_BOOL(compare == 0);
     240             :         }
     241         178 :         if (!strncmp(op, "!=", op_len) || !strncmp(op, "<>", op_len) || !strncmp(op, "ne", op_len)) {
     242         178 :                 RETURN_BOOL(compare != 0);
     243             :         }
     244           0 :         RETURN_NULL();
     245             : }
     246             : 
     247             : /* }}} */
     248             : 
     249             : /*
     250             :  * Local variables:
     251             :  * tab-width: 4
     252             :  * c-basic-offset: 4
     253             :  * indent-tabs-mode: t
     254             :  * End:
     255             :  */

Generated by: LCOV version 1.10

Generated at Tue, 14 Oct 2014 07:25:50 +0000 (6 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.