Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fbe4bc1

Browse files
committed
feature #28106 [Yaml] save preg_match() calls when possible (xabbuh)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Yaml] save preg_match() calls when possible | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | possibly #27960 | License | MIT | Doc PR | Commits ------- e6bea97 save preg_match() calls when possible
2 parents aaa3bb3 + e6bea97 commit fbe4bc1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ private function doParse(string $value, int $flags)
161161
Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename);
162162

163163
$isRef = $mergeNode = false;
164-
if (self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
164+
if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
165165
if ($context && 'mapping' == $context) {
166166
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
167167
}
168168
$context = 'sequence';
169169

170-
if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
170+
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
171171
$isRef = $matches['ref'];
172172
$values['value'] = $matches['value'];
173173
}
@@ -229,7 +229,7 @@ private function doParse(string $value, int $flags)
229229
$key = (string) $key;
230230
}
231231

232-
if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
232+
if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
233233
$mergeNode = true;
234234
$allowOverwrite = true;
235235
if (isset($values['value'][0]) && '*' === $values['value'][0]) {
@@ -286,7 +286,7 @@ private function doParse(string $value, int $flags)
286286
$data += $parsed; // array union
287287
}
288288
}
289-
} elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
289+
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
290290
$isRef = $matches['ref'];
291291
$values['value'] = $matches['value'];
292292
}
@@ -671,7 +671,7 @@ private function parseValue(string $value, int $flags, string $context)
671671
return $this->refs[$value];
672672
}
673673

674-
if (self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
674+
if (\in_array($value[0], array('!', '|', '>'), true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
675675
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
676676

677677
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
@@ -773,8 +773,10 @@ private function parseBlockScalar(string $style, string $chomping = '', int $ind
773773

774774
// determine indentation if not specified
775775
if (0 === $indentation) {
776-
if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
777-
$indentation = \strlen($matches[0]);
776+
$currentLineLength = \strlen($this->currentLine);
777+
778+
for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
779+
++$indentation;
778780
}
779781
}
780782

@@ -1009,7 +1011,7 @@ private function isStringUnIndentedCollectionItem(): bool
10091011
*/
10101012
private function isBlockScalarHeader(): bool
10111013
{
1012-
return (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
1014+
return '' !== $this->currentLine && (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
10131015
}
10141016

10151017
/**

0 commit comments

Comments
 (0)