function StaticPrefixCollection::getCommonPrefix
Gets the full and static common prefixes between two route patterns.
The static prefix stops at last at the first opening bracket.
1 call to StaticPrefixCollection::getCommonPrefix()
- StaticPrefixCollection::addRoute in vendor/
symfony/ routing/ Matcher/ Dumper/ StaticPrefixCollection.php - Adds a route to a group.
File
-
vendor/
symfony/ routing/ Matcher/ Dumper/ StaticPrefixCollection.php, line 143
Class
- StaticPrefixCollection
- Prefix tree of routes preserving routes order.
Namespace
Symfony\Component\Routing\Matcher\DumperCode
private function getCommonPrefix(string $prefix, string $anotherPrefix) : array {
$baseLength = \strlen($this->prefix);
$end = min(\strlen($prefix), \strlen($anotherPrefix));
$staticLength = null;
set_error_handler(self::handleError(...));
try {
for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) {
if ('(' === $prefix[$i]) {
$staticLength ??= $i;
for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) {
if ($prefix[$j] !== $anotherPrefix[$j]) {
break 2;
}
if ('(' === $prefix[$j]) {
++$n;
}
elseif (')' === $prefix[$j]) {
--$n;
}
elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) {
--$j;
break;
}
}
if (0 < $n) {
break;
}
if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) {
break;
}
$subPattern = substr($prefix, $i, $j - $i);
if ($prefix !== $anotherPrefix && !preg_match('/^\\(\\[[^\\]]++\\]\\+\\+\\)$/', $subPattern) && !preg_match('{(?<!' . $subPattern . ')}', '')) {
// sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
break;
}
$i = $j - 1;
}
elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) {
--$i;
break;
}
}
} finally {
restore_error_handler();
}
if ($i < $end && 0b10 === \ord($prefix[$i]) >> 6 && preg_match('//u', $prefix . ' ' . $anotherPrefix)) {
do {
// Prevent cutting in the middle of an UTF-8 characters
--$i;
} while (0b10 === \ord($prefix[$i]) >> 6);
}
return [
substr($prefix, 0, $i),
substr($prefix, 0, $staticLength ?? $i),
];
}