-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Optimise Inline::evaluateScalar() for parsing strings. #10312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -386,51 +386,55 @@ private static function parseMapping($mapping, &$i = 0) | |
private static function evaluateScalar($scalar) | ||
{ | ||
$scalar = trim($scalar); | ||
|
||
$scalarLower = strtolower($scalar); | ||
switch (true) { | ||
case 'null' == strtolower($scalar): | ||
case 'null' == $scalarLower: | ||
case '' == $scalar: | ||
case '~' == $scalar: | ||
return null; | ||
case 0 === strpos($scalar, '!str'): | ||
return (string) substr($scalar, 5); | ||
case 0 === strpos($scalar, '! '): | ||
return intval(self::parseScalar(substr($scalar, 2))); | ||
case 0 === strpos($scalar, '!!php/object:'): | ||
if (self::$objectSupport) { | ||
return unserialize(substr($scalar, 13)); | ||
} | ||
|
||
if (self::$exceptionOnInvalidType) { | ||
throw new ParseException('Object support when parsing a YAML file has been disabled.'); | ||
} | ||
|
||
return null; | ||
case ctype_digit($scalar): | ||
$raw = $scalar; | ||
$cast = intval($scalar); | ||
|
||
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw); | ||
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)): | ||
$raw = $scalar; | ||
$cast = intval($scalar); | ||
|
||
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw); | ||
case 'true' === strtolower($scalar): | ||
case 'true' === $scalarLower: | ||
return true; | ||
case 'false' === strtolower($scalar): | ||
case 'false' === $scalarLower: | ||
return false; | ||
case is_numeric($scalar): | ||
return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar); | ||
case 0 == strcasecmp($scalar, '.inf'): | ||
case 0 == strcasecmp($scalar, '.NaN'): | ||
return -log(0); | ||
case 0 == strcasecmp($scalar, '-.inf'): | ||
return log(0); | ||
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar): | ||
return floatval(str_replace(',', '', $scalar)); | ||
case preg_match(self::getTimestampRegex(), $scalar): | ||
return strtotime($scalar); | ||
// Optimise for returning strings. | ||
case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || $scalar[0] === '!' || is_numeric($scalar[0]): | ||
switch (true) { | ||
case 0 === strpos($scalar, '!str'): | ||
return (string) substr($scalar, 5); | ||
case 0 === strpos($scalar, '! '): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The strpos() check would still be required for differentiating between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tobion have you missed the space after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed I have. |
||
return intval(self::parseScalar(substr($scalar, 2))); | ||
case 0 === strpos($scalar, '!!php/object:'): | ||
if (self::$objectSupport) { | ||
return unserialize(substr($scalar, 13)); | ||
} | ||
|
||
if (self::$exceptionOnInvalidType) { | ||
throw new ParseException('Object support when parsing a YAML file has been disabled.'); | ||
} | ||
|
||
return null; | ||
case ctype_digit($scalar): | ||
$raw = $scalar; | ||
$cast = intval($scalar); | ||
|
||
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw); | ||
case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)): | ||
$raw = $scalar; | ||
$cast = intval($scalar); | ||
|
||
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw); | ||
case is_numeric($scalar): | ||
return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar); | ||
case 0 == strcasecmp($scalar, '.inf'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #10323 |
||
case 0 == strcasecmp($scalar, '.NaN'): | ||
return -log(0); | ||
case 0 == strcasecmp($scalar, '-.inf'): | ||
return log(0); | ||
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar): | ||
return floatval(str_replace(',', '', $scalar)); | ||
case preg_match(self::getTimestampRegex(), $scalar): | ||
return strtotime($scalar); | ||
} | ||
default: | ||
return (string) $scalar; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to not just use in_array(..., TRUE)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is slower. Not hugely - but in_array is more expensive

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's better to split the case statements: