From 0fbc94e64b2b858d5b3b353920bab2a232d4ffec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 10 Mar 2016 12:33:16 +0100 Subject: [PATCH] [Yaml] Allow using _ in some numeric notations --- src/Symfony/Component/Yaml/Inline.php | 10 +++++++--- src/Symfony/Component/Yaml/Tests/InlineTest.php | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 5f15e3c5b4a9b..65d809e85d2b6 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -454,6 +454,8 @@ private static function evaluateScalar($scalar, $references = array()) } return; + case preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar): + $scalar = str_replace('_', '', (string) $scalar); case ctype_digit($scalar): $raw = $scalar; $cast = (int) $scalar; @@ -466,14 +468,16 @@ private static function evaluateScalar($scalar, $references = array()) return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw); case is_numeric($scalar): case preg_match(self::getHexRegex(), $scalar): + $scalar = str_replace('_', '', $scalar); + return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar; case '.inf' === $scalarLower: case '.nan' === $scalarLower: return -log(0); case '-.inf' === $scalarLower: return log(0); - case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar): - return (float) str_replace(',', '', $scalar); + case preg_match('/^(-|\+)?[0-9][0-9,_]*(\.[0-9_]+)?$/', $scalar): + return (float) str_replace(array(',', '_'), '', $scalar); case preg_match(self::getTimestampRegex(), $scalar): $timeZone = date_default_timezone_get(); date_default_timezone_set('UTC'); @@ -519,6 +523,6 @@ private static function getTimestampRegex() */ private static function getHexRegex() { - return '~^0x[0-9a-f]++$~i'; + return '~^0x[0-9a-f_]++$~i'; } } diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 255928d16d2cd..22f5864a0bdee 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -178,11 +178,18 @@ protected function getTestsForParse() 'true' => true, '12' => 12, '-12' => -12, + '1_2' => 12, + '_12' => '_12', + '12_' => 12, + '-12_' => -12, + '-1_2' => -12, '"quoted string"' => 'quoted string', "'quoted string'" => 'quoted string', '12.30e+02' => 12.30e+02, '0x4D2' => 0x4D2, + '0x_4_D_2_' => 0x4D2, '02333' => 02333, + '0_2_3_3_3' => 02333, '.Inf' => -log(0), '-.Inf' => log(0), "'686e444'" => '686e444',