2011-03-05 21:39:48 +00:00
< ? php
/*
* This file is part of the Symfony package .
2013-12-28 07:37:38 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-03-05 21:39:48 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Yaml ;
/**
* Unescaper encapsulates unescaping rules for single and double - quoted
* YAML strings .
*
* @ author Matthew Lewinski < matthew @ lewinski . org >
2015-10-12 12:16:35 +01:00
*
* @ internal
2011-03-05 21:39:48 +00:00
*/
class Unescaper
{
2014-12-30 09:01:12 +00:00
/**
* Parser and Inline assume UTF - 8 encoding , so escaped Unicode characters
* must be converted to that encoding .
*
* @ deprecated since version 2.5 , to be removed in 3.0
2015-08-24 07:53:33 +01:00
*
2015-01-08 11:13:07 +00:00
* @ internal
2014-12-30 09:01:12 +00:00
*/
2015-01-08 11:13:07 +00:00
const ENCODING = 'UTF-8' ;
2011-03-05 21:39:48 +00:00
2014-12-30 09:01:12 +00:00
/**
* Regex fragment that matches an escaped character in a double quoted string .
*/
2016-01-21 09:24:53 +00:00
const REGEX_ESCAPED_CHARACTER = '\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)' ;
2011-03-05 21:39:48 +00:00
/**
* Unescapes a single quoted string .
*
2016-06-28 06:50:50 +01:00
* @ param string $value A single quoted string
2011-03-05 21:39:48 +00:00
*
2016-06-28 06:50:50 +01:00
* @ return string The unescaped string
2011-03-05 21:39:48 +00:00
*/
public function unescapeSingleQuotedString ( $value )
{
return str_replace ( '\'\'' , '\'' , $value );
}
/**
* Unescapes a double quoted string .
*
2016-06-28 06:50:50 +01:00
* @ param string $value A double quoted string
2011-03-05 21:39:48 +00:00
*
2016-06-28 06:50:50 +01:00
* @ return string The unescaped string
2011-03-05 21:39:48 +00:00
*/
public function unescapeDoubleQuotedString ( $value )
{
$self = $this ;
2013-12-28 07:46:05 +00:00
$callback = function ( $match ) use ( $self ) {
2011-03-05 21:39:48 +00:00
return $self -> unescapeCharacter ( $match [ 0 ]);
};
// evaluate the string
return preg_replace_callback ( '/' . self :: REGEX_ESCAPED_CHARACTER . '/u' , $callback , $value );
}
/**
2014-12-21 17:00:50 +00:00
* Unescapes a character that was found in a double - quoted string .
2011-03-05 21:39:48 +00:00
*
* @ param string $value An escaped character
*
* @ return string The unescaped character
2015-10-11 13:05:15 +01:00
*
* @ internal This method is public to be usable as callback . It should not
* be used in user code . Should be changed in 3.0 .
2011-03-05 21:39:48 +00:00
*/
public function unescapeCharacter ( $value )
{
2015-10-11 13:05:15 +01:00
switch ( $value [ 1 ]) {
2011-03-05 21:39:48 +00:00
case '0' :
return " \x0 " ;
case 'a' :
return " \x7 " ;
case 'b' :
return " \x8 " ;
case 't' :
return " \t " ;
case " \t " :
return " \t " ;
case 'n' :
return " \n " ;
case 'v' :
2014-05-12 09:37:25 +01:00
return " \xB " ;
2011-03-05 21:39:48 +00:00
case 'f' :
2014-05-12 09:37:25 +01:00
return " \xC " ;
2011-03-05 21:39:48 +00:00
case 'r' :
2014-05-12 09:37:25 +01:00
return " \r " ;
2011-03-05 21:39:48 +00:00
case 'e' :
2014-05-12 09:37:25 +01:00
return " \x1B " ;
2011-03-05 21:39:48 +00:00
case ' ' :
return ' ' ;
case '"' :
return '"' ;
case '/' :
return '/' ;
case '\\' :
return '\\' ;
case 'N' :
// U+0085 NEXT LINE
2014-05-12 09:37:25 +01:00
return " \xC2 \x85 " ;
2011-03-05 21:39:48 +00:00
case '_' :
// U+00A0 NO-BREAK SPACE
2014-05-12 09:37:25 +01:00
return " \xC2 \xA0 " ;
2011-03-05 21:39:48 +00:00
case 'L' :
// U+2028 LINE SEPARATOR
2014-05-12 09:37:25 +01:00
return " \xE2 \x80 \xA8 " ;
2011-03-05 21:39:48 +00:00
case 'P' :
// U+2029 PARAGRAPH SEPARATOR
2014-05-12 09:37:25 +01:00
return " \xE2 \x80 \xA9 " ;
2011-03-05 21:39:48 +00:00
case 'x' :
2014-05-12 09:37:25 +01:00
return self :: utf8chr ( hexdec ( substr ( $value , 2 , 2 )));
2011-03-05 21:39:48 +00:00
case 'u' :
2014-05-12 09:37:25 +01:00
return self :: utf8chr ( hexdec ( substr ( $value , 2 , 4 )));
2011-03-05 21:39:48 +00:00
case 'U' :
2014-05-12 09:37:25 +01:00
return self :: utf8chr ( hexdec ( substr ( $value , 2 , 8 )));
2015-10-11 13:05:15 +01:00
default :
@ trigger_error ( 'Not escaping a backslash in a double-quoted string is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.' , E_USER_DEPRECATED );
return $value ;
2011-03-05 21:39:48 +00:00
}
}
/**
2014-05-12 09:37:25 +01:00
* Get the UTF - 8 character for the given code point .
2011-03-05 21:39:48 +00:00
*
2014-05-12 09:37:25 +01:00
* @ param int $c The unicode code point
2011-03-05 21:39:48 +00:00
*
2014-05-12 09:37:25 +01:00
* @ return string The corresponding UTF - 8 character
2011-03-05 21:39:48 +00:00
*/
2014-05-12 09:37:25 +01:00
private static function utf8chr ( $c )
2011-03-05 21:39:48 +00:00
{
2014-05-12 09:37:25 +01:00
if ( 0x80 > $c %= 0x200000 ) {
2018-07-05 12:24:53 +01:00
return \chr ( $c );
2014-05-12 09:37:25 +01:00
}
if ( 0x800 > $c ) {
2018-07-05 12:24:53 +01:00
return \chr ( 0xC0 | $c >> 6 ) . \chr ( 0x80 | $c & 0x3F );
2014-05-12 09:37:25 +01:00
}
if ( 0x10000 > $c ) {
2018-07-05 12:24:53 +01:00
return \chr ( 0xE0 | $c >> 12 ) . \chr ( 0x80 | $c >> 6 & 0x3F ) . \chr ( 0x80 | $c & 0x3F );
2011-03-05 21:39:48 +00:00
}
2018-07-05 12:24:53 +01:00
return \chr ( 0xF0 | $c >> 18 ) . \chr ( 0x80 | $c >> 12 & 0x3F ) . \chr ( 0x80 | $c >> 6 & 0x3F ) . \chr ( 0x80 | $c & 0x3F );
2011-03-05 21:39:48 +00:00
}
}