-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Yaml] Improve YAML boolean escaping #13262
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 1 commit
3760e67
81a8090
1e0633e
a0ec0fe
afe827a
8fa056b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Moves dumping single-quoting logic into Yaml\Escaper - Ensures that PHP values which would be interpreted as booleans in older versions of the YAML spec are escaped with single quotes when dumped by the Dumper.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ public static function escapeWithDoubleQuotes($value) | |
| */ | ||
| public static function requiresSingleQuoting($value) | ||
| { | ||
| return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); | ||
| return self::containsCharRequiresSingleQuoting($value) || self::isValueRequiresSingleQuoting($value); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -86,4 +86,28 @@ public static function escapeWithSingleQuotes($value) | |
| { | ||
| return sprintf("'%s'", str_replace('\'', '\'\'', $value)); | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a PHP value contains any single characters that would cause | ||
| * the value to require single quoting in YAML. | ||
| * | ||
| * @param string $value A PHP value | ||
| * @return bool True if the value would require single quotes. | ||
| */ | ||
| private static function containsCharRequiresSingleQuoting($value) | ||
| { | ||
| return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value); | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a PHP value is entirely composed of a value that would | ||
| * require require single quoting in YAML. | ||
| * | ||
| * @param string $value A PHP value | ||
| * @return bool True if the value would require single quotes. | ||
| */ | ||
| private static function isValueRequiresSingleQuoting($value) | ||
| { | ||
| return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off')); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,12 +135,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp | |
| case Escaper::requiresDoubleQuoting($value): | ||
| return Escaper::escapeWithDoubleQuotes($value); | ||
| case Escaper::requiresSingleQuoting($value): | ||
| case preg_match(self::getTimestampRegex(), $value): | ||
| return Escaper::escapeWithSingleQuotes($value); | ||
| case '' == $value: | ||
|
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. I suggest moving this fast comparison before the heavier regex checks |
||
| return "''"; | ||
| case preg_match(self::getTimestampRegex(), $value): | ||
| case in_array(strtolower($value), array('null', '~', 'true', 'false')): | ||
| return "'$value'"; | ||
| default: | ||
| return $value; | ||
| } | ||
|
|
||
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.
doesValueRequireSingleQuoting?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.
Actually, I'm wondering if we really need those 2 private methods. What about inlining them with the right comments instead? Especially as they can be called quite a lot.