save preg_match() calls when possible

This commit is contained in:
Christian Flothmann 2018-08-01 20:31:02 +02:00
parent a31f4aa2e9
commit e6bea97b6d

View File

@ -161,13 +161,13 @@ class Parser
Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename); Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename);
$isRef = $mergeNode = false; $isRef = $mergeNode = false;
if (self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) { if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
if ($context && 'mapping' == $context) { if ($context && 'mapping' == $context) {
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
} }
$context = 'sequence'; $context = 'sequence';
if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) { if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref']; $isRef = $matches['ref'];
$values['value'] = $matches['value']; $values['value'] = $matches['value'];
} }
@ -229,7 +229,7 @@ class Parser
$key = (string) $key; $key = (string) $key;
} }
if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) { if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
$mergeNode = true; $mergeNode = true;
$allowOverwrite = true; $allowOverwrite = true;
if (isset($values['value'][0]) && '*' === $values['value'][0]) { if (isset($values['value'][0]) && '*' === $values['value'][0]) {
@ -286,7 +286,7 @@ class Parser
$data += $parsed; // array union $data += $parsed; // array union
} }
} }
} elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) { } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref']; $isRef = $matches['ref'];
$values['value'] = $matches['value']; $values['value'] = $matches['value'];
} }
@ -671,7 +671,7 @@ class Parser
return $this->refs[$value]; return $this->refs[$value];
} }
if (self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) { if (\in_array($value[0], array('!', '|', '>'), true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers)); $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
@ -773,8 +773,10 @@ class Parser
// determine indentation if not specified // determine indentation if not specified
if (0 === $indentation) { if (0 === $indentation) {
if (self::preg_match('/^ +/', $this->currentLine, $matches)) { $currentLineLength = \strlen($this->currentLine);
$indentation = \strlen($matches[0]);
for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
++$indentation;
} }
} }
@ -1009,7 +1011,7 @@ class Parser
*/ */
private function isBlockScalarHeader(): bool private function isBlockScalarHeader(): bool
{ {
return (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine); return '' !== $this->currentLine && (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
} }
/** /**