Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Optimise Inline::evaluateScalar() for parsing strings.

  • Loading branch information...
commit d5aca818b2d3fb13e7e30328c83f620d14867d37 1 parent 7ee758c
Alex Pott authored fabpot committed
Showing with 43 additions and 39 deletions.
  1. +43 −39 src/Symfony/Component/Yaml/Inline.php
View
82 src/Symfony/Component/Yaml/Inline.php
@@ -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, '! '):
+ 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'):
+ 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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.