Skip to content

[Yaml] Allow using _ in some numeric notations #18096

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I saw anywhere that the , was authorized though ? I kept it, but nothing in the specs says it should be used as a separator (And as @nicolas-grekas noticed, _123_456_ should not be a valid number, so shouldn't ,123,456, be also considered invalid or a string ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check with a reference parser, e.g. http://yaml-online-parser.appspot.com/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That tool is awesome, and yes, the , is not allowed as a separator (it renders as string when one is used in a number). I'd rather not touch it, as it would introduce a bc break.

But 123_ seems to be a valid integer too, so I need to keep that (it will simplify some regex)

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');
Expand Down Expand Up @@ -519,6 +523,6 @@ private static function getTimestampRegex()
*/
private static function getHexRegex()
{
return '~^0x[0-9a-f]++$~i';
return '~^0x[0-9a-f_]++$~i';
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down