-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangesCharacterPredicate.php
More file actions
59 lines (50 loc) · 1.06 KB
/
Copy pathRangesCharacterPredicate.php
File metadata and controls
59 lines (50 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace petitparser;
class RangesCharacterPredicate extends CharacterPredicate
{
/**
* @var int
*/
public $length;
/**
* @var int[]
*/
public $starts;
/**
* @var int[]
*/
public $stops;
/**
* @param int $length
* @param int[] $starts
* @param int[] $stops
*/
public function __construct($length, $starts, $stops)
{
$this->length = $length;
$this->starts = $starts;
$this->stops = $stops;
}
/**
* @param int $value
*
* @return bool
*/
public function test($value)
{
$min = 0;
$max = $this->length;
while ($min < $max) {
$mid = $min + (($max - $min) >> 1);
$comp = $this->starts[$mid] - $value;
if ($comp === 0) {
return true;
} elseif ($comp < 0) {
$min = $mid + 1;
} else {
$max = $mid;
}
}
return (0 < $min) && ($value <= $this->stops[$min - 1]);
}
}