[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php declare(strict_types=1); 2 3 /** 4 * @package s9e\RegexpBuilder 5 * @copyright Copyright (c) 2016-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\RegexpBuilder\Passes; 9 10 use const false, true; 11 use function array_filter, array_pop, array_unshift, count, end; 12 13 /** 14 * Replaces (?:aax|bbx) with (?:aa|bb)x 15 */ 16 class MergeSuffix extends AbstractPass 17 { 18 /** 19 * {@inheritdoc} 20 */ 21 protected function canRun(array $strings): bool 22 { 23 return (count($strings) > 1 && $this->hasMatchingSuffix($strings)); 24 } 25 26 /** 27 * {@inheritdoc} 28 */ 29 protected function runPass(array $strings): array 30 { 31 $newString = []; 32 while ($this->hasMatchingSuffix($strings)) 33 { 34 array_unshift($newString, end($strings[0])); 35 $strings = $this->pop($strings); 36 } 37 array_unshift($newString, $strings); 38 39 return [$newString]; 40 } 41 42 /** 43 * Test whether all given strings have the same last element 44 * 45 * @param array[] $strings 46 * @return bool 47 */ 48 protected function hasMatchingSuffix(array $strings): bool 49 { 50 $suffix = end($strings[1]); 51 foreach ($strings as $string) 52 { 53 if (end($string) !== $suffix) 54 { 55 return false; 56 } 57 } 58 59 return ($suffix !== false); 60 } 61 62 /** 63 * Remove the last element of every string 64 * 65 * @param array[] $strings Original strings 66 * @return array[] Processed strings 67 */ 68 protected function pop(array $strings): array 69 { 70 $cnt = count($strings); 71 $i = $cnt; 72 while (--$i >= 0) 73 { 74 array_pop($strings[$i]); 75 } 76 77 // Remove empty elements then prepend one back at the start of the array if applicable 78 $strings = array_filter($strings); 79 if (count($strings) < $cnt) 80 { 81 array_unshift($strings, []); 82 } 83 84 return $strings; 85 } 86 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |