Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ea9d6e7

Browse filesBrowse files
committed
feature #18486 [Yaml] Allow using _ in some numeric notations (Taluu)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Yaml] Allow using _ in some numeric notations | Q | A | ------------- | --- | Branch | master | Bug fix? | no ? | New feature? | yes ? | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18094 | License | MIT | Doc PR | ~ Allows to use the `_` to group "big" ints, as suggested in the yaml integer type specification. As discussed in #18094, we should check if it is still part of the 1.2 specification, but I don't really see why not ? I can't see anywhere anything saying it is not valid anymore... as there are links to these types in some other specs. This is #18096, but targetted on master as this is considered as a new feature. I also support the dump of such values as only strings. I think I should change how it is dumped thoug, and use the escape filter instead though (as I was misusing the data provider and it provided strange results at the time) Commits ------- e6da11c [Yaml] Allow using _ in some numeric notations
2 parents 88cf986 + e6da11c commit ea9d6e7
Copy full SHA for ea9d6e7

File tree

2 files changed

+20
-3
lines changed
Filter options

2 files changed

+20
-3
lines changed

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public static function dump($value, $flags = 0)
206206
case Escaper::requiresDoubleQuoting($value):
207207
return Escaper::escapeWithDoubleQuotes($value);
208208
case Escaper::requiresSingleQuoting($value):
209+
case preg_match('{^[0-9]+[_0-9]*$}', $value):
209210
case preg_match(self::getHexRegex(), $value):
210211
case preg_match(self::getTimestampRegex(), $value):
211212
return Escaper::escapeWithSingleQuotes($value);
@@ -564,6 +565,9 @@ private static function evaluateScalar($scalar, $flags, $references = array())
564565
return;
565566
case 0 === strpos($scalar, '!!float '):
566567
return (float) substr($scalar, 8);
568+
case preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
569+
$scalar = str_replace('_', '', (string) $scalar);
570+
// omitting the break / return as integers are handled in the next case
567571
case ctype_digit($scalar):
568572
$raw = $scalar;
569573
$cast = (int) $scalar;
@@ -576,6 +580,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
576580
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
577581
case is_numeric($scalar):
578582
case preg_match(self::getHexRegex(), $scalar):
583+
$scalar = str_replace('_', '', $scalar);
584+
579585
return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
580586
case '.inf' === $scalarLower:
581587
case '.nan' === $scalarLower:
@@ -584,8 +590,9 @@ private static function evaluateScalar($scalar, $flags, $references = array())
584590
return log(0);
585591
case 0 === strpos($scalar, '!!binary '):
586592
return self::evaluateBinaryScalar(substr($scalar, 9));
587-
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
588-
return (float) str_replace(',', '', $scalar);
593+
case preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
594+
case preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
595+
return (float) str_replace(array(',', '_'), '', $scalar);
589596
case preg_match(self::getTimestampRegex(), $scalar):
590597
if (Yaml::PARSE_DATETIME & $flags) {
591598
return new \DateTime($scalar, new \DateTimeZone('UTC'));
@@ -662,6 +669,6 @@ private static function getTimestampRegex()
662669
*/
663670
private static function getHexRegex()
664671
{
665-
return '~^0x[0-9a-f]++$~i';
672+
return '~^0x[0-9a-f_]++$~i';
666673
}
667674
}

‎src/Symfony/Component/Yaml/Tests/InlineTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/InlineTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,16 @@ public function getTestsForParse()
286286
array('true', true),
287287
array('12', 12),
288288
array('-12', -12),
289+
array('1_2', 12),
290+
array('_12', '_12'),
291+
array('12_', 12),
289292
array('"quoted string"', 'quoted string'),
290293
array("'quoted string'", 'quoted string'),
291294
array('12.30e+02', 12.30e+02),
292295
array('0x4D2', 0x4D2),
296+
array('0x_4_D_2_', 0x4D2),
293297
array('02333', 02333),
298+
array('0_2_3_3_3', 02333),
294299
array('.Inf', -log(0)),
295300
array('-.Inf', log(0)),
296301
array("'686e444'", '686e444'),
@@ -438,10 +443,15 @@ public function getTestsForDump()
438443
array('false', false),
439444
array('true', true),
440445
array('12', 12),
446+
array("'1_2'", '1_2'),
447+
array('_12', '_12'),
448+
array("'12_'", '12_'),
441449
array("'quoted string'", 'quoted string'),
442450
array('!!float 1230', 12.30e+02),
443451
array('1234', 0x4D2),
444452
array('1243', 02333),
453+
array("'0x_4_D_2_'", '0x_4_D_2_'),
454+
array("'0_2_3_3_3'", '0_2_3_3_3'),
445455
array('.Inf', -log(0)),
446456
array('-.Inf', log(0)),
447457
array("'686e444'", '686e444'),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.