1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\RegexpBuilder |
5
|
|
|
* @copyright Copyright (c) 2016 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\RegexpBuilder\Passes; |
9
|
|
|
|
10
|
|
|
class MergePrefix extends AbstractPass |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
5 |
|
protected function processStrings(array $strings) |
16
|
|
|
{ |
17
|
5 |
|
$newStrings = []; |
18
|
5 |
|
foreach ($this->getStringsByPrefix($strings) as $prefix => $strings) |
19
|
|
|
{ |
20
|
4 |
|
$newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0]; |
21
|
|
|
} |
22
|
|
|
|
23
|
5 |
|
return $newStrings; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the number of leading elements common to all given strings |
28
|
|
|
* |
29
|
|
|
* @param array[] $strings |
30
|
|
|
* @return integer |
31
|
|
|
*/ |
32
|
4 |
|
protected function getPrefixLength(array $strings) |
33
|
|
|
{ |
34
|
4 |
|
$len = 1; |
35
|
4 |
|
$cnt = count($strings[0]); |
36
|
4 |
|
while ($len < $cnt && $this->stringsMatch($strings, $len)) |
37
|
|
|
{ |
38
|
3 |
|
++$len; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
return $len; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return given strings grouped by their first element |
46
|
|
|
* |
47
|
|
|
* NOTE: assumes that this pass is run before the first element of any string could be replaced |
48
|
|
|
* |
49
|
|
|
* @param array[] $strings |
50
|
|
|
* @return array[] |
51
|
|
|
*/ |
52
|
5 |
|
protected function getStringsByPrefix(array $strings) |
53
|
|
|
{ |
54
|
5 |
|
$byPrefix = []; |
55
|
5 |
|
foreach ($strings as $string) |
56
|
|
|
{ |
57
|
4 |
|
$byPrefix[$string[0]][] = $string; |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
return $byPrefix; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Merge given strings into a new single string |
65
|
|
|
* |
66
|
|
|
* @param array[] $strings |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
4 |
|
protected function mergeStrings(array $strings) |
70
|
|
|
{ |
71
|
4 |
|
$len = $this->getPrefixLength($strings); |
72
|
4 |
|
$newString = array_slice($strings[0], 0, $len); |
73
|
4 |
|
foreach ($strings as $string) |
74
|
|
|
{ |
75
|
4 |
|
$newString[$len][] = array_slice($string, $len); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return $newString; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test whether all given strings' elements match at given position |
83
|
|
|
* |
84
|
|
|
* @param array[] $strings |
85
|
|
|
* @param integer $pos |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
3 |
|
protected function stringsMatch(array $strings, $pos) |
89
|
|
|
{ |
90
|
3 |
|
$value = $strings[0][$pos]; |
91
|
3 |
|
foreach ($strings as $string) |
92
|
|
|
{ |
93
|
3 |
|
if (!isset($string[$pos]) || $string[$pos] !== $value) |
94
|
|
|
{ |
95
|
3 |
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |