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 |
MergePrefix.php
001 <?php declare(strict_types=1);
002
003 /**
004 * @package s9e\RegexpBuilder
005 * @copyright Copyright (c) 2016-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\RegexpBuilder\Passes;
009
010 use const false, true;
011 use function array_slice, count;
012
013 /**
014 * Replaces (?:axx|ayy) with a(?:xx|yy)
015 */
016 class MergePrefix extends AbstractPass
017 {
018 /**
019 * {@inheritdoc}
020 */
021 protected function runPass(array $strings): array
022 {
023 $newStrings = [];
024 foreach ($this->getStringsByPrefix($strings) as $prefix => $strings)
025 {
026 $newStrings[] = (isset($strings[1])) ? $this->mergeStrings($strings) : $strings[0];
027 }
028
029 return $newStrings;
030 }
031
032 /**
033 * Get the number of leading elements common to all given strings
034 *
035 * @param array[] $strings
036 * @return integer
037 */
038 protected function getPrefixLength(array $strings): int
039 {
040 $len = 1;
041 $cnt = count($strings[0]);
042 while ($len < $cnt && $this->stringsMatch($strings, $len))
043 {
044 ++$len;
045 }
046
047 return $len;
048 }
049
050 /**
051 * Return given strings grouped by their first element
052 *
053 * NOTE: assumes that this pass is run before the first element of any string could be replaced
054 *
055 * @param array[] $strings
056 * @return array[]
057 */
058 protected function getStringsByPrefix(array $strings): array
059 {
060 $byPrefix = [];
061 foreach ($strings as $string)
062 {
063 $byPrefix[$string[0]][] = $string;
064 }
065
066 return $byPrefix;
067 }
068
069 /**
070 * Merge given strings into a new single string
071 *
072 * @param array[] $strings
073 * @return array
074 */
075 protected function mergeStrings(array $strings): array
076 {
077 $len = $this->getPrefixLength($strings);
078 $newString = array_slice($strings[0], 0, $len);
079 foreach ($strings as $string)
080 {
081 $newString[$len][] = array_slice($string, $len);
082 }
083
084 return $newString;
085 }
086
087 /**
088 * Test whether all given strings' elements match at given position
089 *
090 * @param array[] $strings
091 * @param integer $pos
092 * @return bool
093 */
094 protected function stringsMatch(array $strings, int $pos): bool
095 {
096 $value = $strings[0][$pos];
097 foreach ($strings as $string)
098 {
099 if (!isset($string[$pos]) || $string[$pos] !== $value)
100 {
101 return false;
102 }
103 }
104
105 return true;
106 }
107 }