[ 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 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 }
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 |