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 - strnatcmp.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 72 74 97.3 %
Date: 2016-09-27 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- mode: c; c-file-style: "k&r" -*-
       2             : 
       3             :   Modified for PHP by Andrei Zmievski <andrei@ispi.net>
       4             : 
       5             :   strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
       6             :   Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
       7             : 
       8             :   This software is provided 'as-is', without any express or implied
       9             :   warranty.  In no event will the authors be held liable for any damages
      10             :   arising from the use of this software.
      11             : 
      12             :   Permission is granted to anyone to use this software for any purpose,
      13             :   including commercial applications, and to alter it and redistribute it
      14             :   freely, subject to the following restrictions:
      15             : 
      16             :   1. The origin of this software must not be misrepresented; you must not
      17             :      claim that you wrote the original software. If you use this software
      18             :      in a product, an acknowledgment in the product documentation would be
      19             :      appreciated but is not required.
      20             :   2. Altered source versions must be plainly marked as such, and must not be
      21             :      misrepresented as being the original software.
      22             :   3. This notice may not be removed or altered from any source distribution.
      23             : */
      24             : 
      25             : #include <ctype.h>
      26             : #include <string.h>
      27             : #include <assert.h>
      28             : #include <stdio.h>
      29             : 
      30             : #include "php.h"
      31             : #include "php_string.h"
      32             : 
      33             : /* {{{ compare_right
      34             :  */
      35             : static int
      36         198 : compare_right(char const **a, char const *aend, char const **b, char const *bend)
      37             : {
      38         198 :         int bias = 0;
      39             : 
      40             :         /* The longest run of digits wins.  That aside, the greatest
      41             :            value wins, but we can't know that it will until we've scanned
      42             :            both numbers to know that they have the same magnitude, so we
      43             :            remember it in BIAS. */
      44         286 :         for(;; (*a)++, (*b)++) {
      45         690 :                 if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
      46         206 :                         (*b == bend || !isdigit((int)(unsigned char)**b)))
      47         111 :                         return bias;
      48         373 :                 else if (*a == aend || !isdigit((int)(unsigned char)**a))
      49          35 :                         return -1;
      50         338 :                 else if (*b == bend || !isdigit((int)(unsigned char)**b))
      51          52 :                         return +1;
      52         286 :                 else if (**a < **b) {
      53         109 :                         if (!bias)
      54          76 :                                 bias = -1;
      55         177 :                 } else if (**a > **b) {
      56         101 :                         if (!bias)
      57          78 :                                 bias = +1;
      58             :                 }
      59         286 :      }
      60             : 
      61             :      return 0;
      62             : }
      63             : /* }}} */
      64             : 
      65             : /* {{{ compare_left
      66             :  */
      67             : static int
      68          58 : compare_left(char const **a, char const *aend, char const **b, char const *bend)
      69             : {
      70             :      /* Compare two left-aligned numbers: the first to have a
      71             :         different value wins. */
      72          11 :         for(;; (*a)++, (*b)++) {
      73          71 :                 if ((*a == aend || !isdigit((int)(unsigned char)**a)) &&
      74          13 :                         (*b == bend || !isdigit((int)(unsigned char)**b)))
      75           9 :                         return 0;
      76          49 :                 else if (*a == aend || !isdigit((int)(unsigned char)**a))
      77           0 :                         return -1;
      78          49 :                 else if (*b == bend || !isdigit((int)(unsigned char)**b))
      79           0 :                         return +1;
      80          49 :                  else if (**a < **b)
      81           2 :                          return -1;
      82          47 :                  else if (**a > **b)
      83          36 :                          return +1;
      84          11 :      }
      85             : 
      86             :      return 0;
      87             : }
      88             : /* }}} */
      89             : 
      90             : /* {{{ strnatcmp_ex
      91             :  */
      92         672 : PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
      93             : {
      94             :         unsigned char ca, cb;
      95             :         char const *ap, *bp;
      96         672 :         char const *aend = a + a_len,
      97         672 :                            *bend = b + b_len;
      98             :         int fractional, result;
      99         672 :         short leading = 1;
     100             : 
     101         672 :         if (a_len == 0 || b_len == 0) {
     102          32 :                 return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
     103             :         }
     104             : 
     105         640 :         ap = a;
     106         640 :         bp = b;
     107             :         while (1) {
     108        1296 :                 ca = *ap; cb = *bp;
     109             : 
     110             :                 /* skip over leading zeros */
     111        2660 :                 while (leading && ca == '0' && (ap+1 < aend) && isdigit((int)(unsigned char)*(ap+1))) {
     112          68 :                         ca = *++ap;
     113             :                 }
     114             : 
     115        2615 :                 while (leading && cb == '0' && (bp+1 < bend) && isdigit((int)(unsigned char)*(bp+1))) {
     116          23 :                         cb = *++bp;
     117             :                 }
     118             : 
     119        1296 :                 leading = 0;
     120             : 
     121             :                 /* Skip consecutive whitespace */
     122        2604 :                 while (isspace((int)(unsigned char)ca)) {
     123          12 :                         ca = *++ap;
     124             :                 }
     125             : 
     126        2616 :                 while (isspace((int)(unsigned char)cb)) {
     127          24 :                         cb = *++bp;
     128             :                 }
     129             : 
     130             :                 /* process run of digits */
     131        1296 :                 if (isdigit((int)(unsigned char)ca)  &&  isdigit((int)(unsigned char)cb)) {
     132         245 :                         fractional = (ca == '0' || cb == '0');
     133             : 
     134         245 :                         if (fractional)
     135          47 :                                 result = compare_left(&ap, aend, &bp, bend);
     136             :                         else
     137         198 :                                 result = compare_right(&ap, aend, &bp, bend);
     138             : 
     139         245 :                         if (result != 0)
     140         220 :                                 return result;
     141          25 :                         else if (ap == aend && bp == bend)
     142             :                                 /* End of the strings. Let caller sort them out. */
     143          10 :                                 return 0;
     144             :                         else {
     145             :                                 /* Keep on comparing from the current point. */
     146          15 :                                 ca = *ap; cb = *bp;
     147             :                         }
     148             :                 }
     149             : 
     150        1066 :                 if (fold_case) {
     151         705 :                         ca = toupper((int)(unsigned char)ca);
     152         705 :                         cb = toupper((int)(unsigned char)cb);
     153             :                 }
     154             : 
     155        1066 :                 if (ca < cb)
     156          90 :                         return -1;
     157         976 :                 else if (ca > cb)
     158         243 :                         return +1;
     159             : 
     160         733 :                 ++ap; ++bp;
     161         733 :                 if (ap >= aend && bp >= bend)
     162             :                         /* The strings compare the same.  Perhaps the caller
     163             :                            will want to call strcmp to break the tie. */
     164          29 :                         return 0;
     165         704 :                 else if (ap >= aend)
     166          10 :                         return -1;
     167         694 :                 else if (bp >= bend)
     168          38 :                         return 1;
     169         656 :         }
     170             : }
     171             : /* }}} */
     172             : 
     173             : /*
     174             :  * Local variables:
     175             :  * tab-width: 4
     176             :  * c-basic-offset: 4
     177             :  * End:
     178             :  * vim600: sw=4 ts=4 fdm=marker
     179             :  * vim<600: sw=4 ts=4
     180             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:08 +0000 (23 hours ago)

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