Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
CoalesceSingleCharacterPrefix.php
01 <?php declare(strict_types=1);
02
03 /**
04 * @package s9e\RegexpBuilder
05 * @copyright Copyright (c) 2016-2022 The s9e authors
06 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
07 */
08 namespace s9e\RegexpBuilder\Passes;
09
10 use function array_merge, array_slice, count, is_array, serialize;
11
12 /**
13 * Replaces (?:ab|bb|c) with (?:[ab]b|c)
14 */
15 class CoalesceSingleCharacterPrefix extends AbstractPass
16 {
17 /**
18 * {@inheritdoc}
19 */
20 protected function runPass(array $strings): array
21 {
22 $newStrings = [];
23 foreach ($this->getEligibleKeys($strings) as $keys)
24 {
25 // Create a new string to hold the merged strings and replace the first element with
26 // an empty character class
27 $newString = $strings[$keys[0]];
28 $newString[0] = [];
29
30 // Fill the character class with the prefix of each string in this group before removing
31 // the original string
32 foreach ($keys as $key)
33 {
34 $newString[0][] = [$strings[$key][0]];
35 unset($strings[$key]);
36 }
37 $newStrings[] = $newString;
38 }
39
40 return array_merge($newStrings, $strings);
41 }
42
43 /**
44 * Filter the list of eligible keys and keep those that have at least two matches
45 *
46 * @param array[] $eligibleKeys List of lists of keys
47 * @return array[]
48 */
49 protected function filterEligibleKeys(array $eligibleKeys): array
50 {
51 $filteredKeys = [];
52 foreach ($eligibleKeys as $k => $keys)
53 {
54 if (count($keys) > 1)
55 {
56 $filteredKeys[] = $keys;
57 }
58 }
59
60 return $filteredKeys;
61 }
62
63 /**
64 * Get a list of keys of strings eligible to be merged together, grouped by suffix
65 *
66 * @param array[] $strings
67 * @return array[]
68 */
69 protected function getEligibleKeys(array $strings): array
70 {
71 $eligibleKeys = [];
72 foreach ($strings as $k => $string)
73 {
74 if (!is_array($string[0]) && isset($string[1]))
75 {
76 $suffix = serialize(array_slice($string, 1));
77 $eligibleKeys[$suffix][] = $k;
78 }
79 }
80
81 return $this->filterEligibleKeys($eligibleKeys);
82 }
83 }