10BC0 [Yaml] Improve YAML boolean escaping by petert82 · Pull Request #13262 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Inline private 'is quoting required' methods in Escaper
  • Loading branch information
pthompson committed Jan 16, 2015
commit 8fa056bc95abe1c323be5460fda446329a402185
36 changes: 9 additions & 27 deletions src/Symfony/Component/Yaml/Escaper.php
7BB4
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ public static function escapeWithDoubleQuotes($value)
*/
public static function requiresSingleQuoting($value)
{
return self::containsCharRequiresSingleQuoting($value) || self::isValueRequiresSingleQuoting($value);
// Determines if the PHP value contains any single characters that would
// cause it to require single quoting in YAML.
if (preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value)) {
return true;
}

// Determines if a PHP value is entirely composed of a value that would
// require single quoting in YAML.
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
Copy link
Member

Choose a reason for hiding this comment

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

I would do the in_array check before the regex check. It is likely to be faster

}

/**
Expand All @@ -86,30 +94,4 @@ 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 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)
{
// Note that whilst 'y' and 'n' are not supported as valid Booleans,
// they are escaped here for interoperability.
return in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'));
}
}
0