Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/strnatcmp.c
Line
Count
Source
1
/*
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 <stdio.h>
28
29
#include "php.h"
30
#include "php_string.h"
31
32
/* {{{ compare_right */
33
static int
34
compare_right(char const **a, char const *aend, char const **b, char const *bend)
35
3.63k
{
36
3.63k
  int bias = 0;
37
38
  /* The longest run of digits wins.  That aside, the greatest
39
     value wins, but we can't know that it will until we've scanned
40
     both numbers to know that they have the same magnitude, so we
41
     remember it in BIAS. */
42
23.6k
  for(;; (*a)++, (*b)++) {
43
23.6k
    if ((*a == aend || !isdigit((unsigned char)**a)) &&
44
2.63k
      (*b == bend || !isdigit((unsigned char)**b)))
45
2.23k
      return bias;
46
21.4k
    else if (*a == aend || !isdigit((unsigned char)**a))
47
400
      return -1;
48
21.0k
    else if (*b == bend || !isdigit((unsigned char)**b))
49
1.00k
      return +1;
50
19.9k
    else if (**a < **b) {
51
2.80k
      if (!bias)
52
675
        bias = -1;
53
17.1k
    } else if (**a > **b) {
54
2.60k
      if (!bias)
55
998
        bias = +1;
56
2.60k
    }
57
23.6k
  }
58
59
0
  return 0;
60
3.63k
}
61
/* }}} */
62
63
/* {{{ compare_left */
64
static int
65
compare_left(char const **a, char const *aend, char const **b, char const *bend)
66
1.40k
{
67
  /* Compare two left-aligned numbers: the first to have a
68
     different value wins. */
69
4.57k
  for(;; (*a)++, (*b)++) {
70
4.57k
    if ((*a == aend || !isdigit((unsigned char)**a)) &&
71
747
      (*b == bend || !isdigit((unsigned char)**b)))
72
737
      return 0;
73
3.83k
    else if (*a == aend || !isdigit((unsigned char)**a))
74
10
      return -1;
75
3.82k
    else if (*b == bend || !isdigit((unsigned char)**b))
76
14
      return +1;
77
3.81k
     else if (**a < **b)
78
217
       return -1;
79
3.59k
     else if (**a > **b)
80
422
       return +1;
81
4.57k
  }
82
83
0
  return 0;
84
1.40k
}
85
/* }}} */
86
87
/* {{{ strnatcmp_ex */
88
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive)
89
29.7k
{
90
29.7k
  unsigned char ca, cb;
91
29.7k
  char const *ap, *bp;
92
29.7k
  char const *aend = a + a_len,
93
29.7k
         *bend = b + b_len;
94
29.7k
  int fractional, result;
95
96
29.7k
  if (a_len == 0 || b_len == 0) {
97
1.79k
    return (a_len == b_len ? 0 : (a_len > b_len ? 1 : -1));
98
1.79k
  }
99
100
28.0k
  ap = a;
101
28.0k
  bp = b;
102
103
28.0k
  ca = *ap; cb = *bp;
104
105
  /* skip over leading zeros */
106
28.0k
  while (ca == '0' && (ap+1 < aend) && isdigit((unsigned char)ap[1])) {
107
0
    ca = *++ap;
108
0
  }
109
110
28.0k
  while (cb == '0' && (bp+1 < bend) && isdigit((unsigned char)bp[1])) {
111
0
    cb = *++bp;
112
0
  }
113
114
298k
  while (1) {
115
116
    /* Skip consecutive whitespace */
117
298k
    while (isspace(ca)) {
118
37.4k
      ca = *++ap;
119
37.4k
    }
120
121
298k
    while (isspace(cb)) {
122
37.1k
      cb = *++bp;
123
37.1k
    }
124
125
    /* process run of digits */
126
298k
    if (isdigit(ca)  &&  isdigit(cb)) {
127
5.03k
      fractional = (ca == '0' || cb == '0');
128
129
5.03k
      if (fractional)
130
1.40k
        result = compare_left(&ap, aend, &bp, bend);
131
3.63k
      else
132
3.63k
        result = compare_right(&ap, aend, &bp, bend);
133
134
5.03k
      if (result != 0)
135
2.74k
        return result;
136
2.29k
      else if (ap == aend && bp == bend)
137
        /* End of the strings. Let caller sort them out. */
138
348
        return 0;
139
1.94k
      else if (ap == aend)
140
3
        return -1;
141
1.93k
      else if (bp == bend)
142
12
        return 1;
143
1.92k
      else {
144
        /* Keep on comparing from the current point. */
145
1.92k
        ca = *ap; cb = *bp;
146
1.92k
      }
147
5.03k
    }
148
149
295k
    if (is_case_insensitive) {
150
131
      ca = toupper(ca);
151
131
      cb = toupper(cb);
152
131
    }
153
154
295k
    if (ca < cb)
155
6.10k
      return -1;
156
289k
    else if (ca > cb)
157
13.4k
      return +1;
158
159
276k
    ++ap; ++bp;
160
276k
    if (ap >= aend && bp >= bend)
161
      /* The strings compare the same.  Perhaps the caller
162
         will want to call strcmp to break the tie. */
163
4.35k
      return 0;
164
271k
    else if (ap >= aend)
165
247
      return -1;
166
271k
    else if (bp >= bend)
167
704
      return 1;
168
169
270k
    ca = *ap; cb = *bp;
170
270k
  }
171
28.0k
}
172
/* }}} */