2011-05-27 18:20:44 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node\Scalar;
|
|
|
|
|
2015-06-13 20:51:02 +02:00
|
|
|
use PhpParser\Error;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
2015-03-20 21:47:20 +01:00
|
|
|
class String_ extends Scalar
|
2011-05-27 18:20:44 +02:00
|
|
|
{
|
2015-02-28 18:44:28 +01:00
|
|
|
/** @var string String value */
|
|
|
|
public $value;
|
|
|
|
|
2011-12-04 16:52:43 +01:00
|
|
|
protected static $replacements = array(
|
|
|
|
'\\' => '\\',
|
|
|
|
'$' => '$',
|
|
|
|
'n' => "\n",
|
|
|
|
'r' => "\r",
|
|
|
|
't' => "\t",
|
|
|
|
'f' => "\f",
|
|
|
|
'v' => "\v",
|
2011-12-04 17:35:30 +01:00
|
|
|
'e' => "\x1B",
|
2011-12-04 16:52:43 +01:00
|
|
|
);
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* Constructs a string scalar node.
|
|
|
|
*
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param string $value Value of the string
|
|
|
|
* @param array $attributes Additional attributes
|
2011-08-09 14:55:45 +02:00
|
|
|
*/
|
2015-07-12 22:02:18 +02:00
|
|
|
public function __construct($value, array $attributes = array()) {
|
2015-05-02 22:17:34 +02:00
|
|
|
parent::__construct($attributes);
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubNodeNames() {
|
|
|
|
return array('value');
|
2011-08-09 14:55:45 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 00:21:12 +02:00
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* Parses a string token.
|
2011-05-28 00:21:12 +02:00
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* @param string $str String token content
|
2015-06-13 20:51:02 +02:00
|
|
|
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
|
2011-06-01 22:37:10 +02:00
|
|
|
*
|
2012-10-19 15:17:08 +02:00
|
|
|
* @return string The parsed string
|
2011-05-28 00:21:12 +02:00
|
|
|
*/
|
2015-06-13 20:51:02 +02:00
|
|
|
public static function parse($str, $parseUnicodeEscape = true) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 0;
|
2011-08-20 10:40:27 +02:00
|
|
|
if ('b' === $str[0]) {
|
2011-08-09 14:19:44 +02:00
|
|
|
$bLength = 1;
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
|
|
|
|
2011-08-20 10:40:27 +02:00
|
|
|
if ('\'' === $str[$bLength]) {
|
2012-10-19 15:17:08 +02:00
|
|
|
return str_replace(
|
2011-05-28 00:21:12 +02:00
|
|
|
array('\\\\', '\\\''),
|
|
|
|
array( '\\', '\''),
|
2011-08-20 10:40:27 +02:00
|
|
|
substr($str, $bLength + 1, -1)
|
2011-05-28 00:21:12 +02:00
|
|
|
);
|
|
|
|
} else {
|
2015-06-13 20:51:02 +02:00
|
|
|
return self::parseEscapeSequences(
|
|
|
|
substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
|
|
|
|
);
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
2011-05-29 19:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2011-08-20 10:40:27 +02:00
|
|
|
* Parses escape sequences in strings (all string types apart from single quoted).
|
2011-05-29 19:38:04 +02:00
|
|
|
*
|
2011-08-20 10:40:27 +02:00
|
|
|
* @param string $str String without quotes
|
|
|
|
* @param null|string $quote Quote type
|
2015-06-13 20:51:02 +02:00
|
|
|
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
|
2011-06-01 22:37:10 +02:00
|
|
|
*
|
2011-05-29 19:38:04 +02:00
|
|
|
* @return string String with escape sequences parsed
|
|
|
|
*/
|
2015-06-13 20:51:02 +02:00
|
|
|
public static function parseEscapeSequences($str, $quote, $parseUnicodeEscape = true) {
|
2011-08-20 10:40:27 +02:00
|
|
|
if (null !== $quote) {
|
|
|
|
$str = str_replace('\\' . $quote, $quote, $str);
|
|
|
|
}
|
2011-05-29 19:38:04 +02:00
|
|
|
|
2015-06-13 20:51:02 +02:00
|
|
|
$extra = '';
|
|
|
|
if ($parseUnicodeEscape) {
|
|
|
|
$extra = '|u\{([0-9a-fA-F]+)\}';
|
|
|
|
}
|
|
|
|
|
2011-08-20 10:40:27 +02:00
|
|
|
return preg_replace_callback(
|
2015-06-13 20:51:02 +02:00
|
|
|
'~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
|
2015-05-02 22:35:15 +02:00
|
|
|
function($matches) {
|
|
|
|
$str = $matches[1];
|
|
|
|
|
|
|
|
if (isset(self::$replacements[$str])) {
|
|
|
|
return self::$replacements[$str];
|
|
|
|
} elseif ('x' === $str[0] || 'X' === $str[0]) {
|
|
|
|
return chr(hexdec($str));
|
2015-06-13 20:51:02 +02:00
|
|
|
} elseif ('u' === $str[0]) {
|
|
|
|
return self::codePointToUtf8(hexdec($matches[2]));
|
2015-05-02 22:35:15 +02:00
|
|
|
} else {
|
|
|
|
return chr(octdec($str));
|
|
|
|
}
|
|
|
|
},
|
2011-08-20 10:40:27 +02:00
|
|
|
$str
|
2011-05-29 19:38:04 +02:00
|
|
|
);
|
2011-05-28 00:21:12 +02:00
|
|
|
}
|
2011-08-20 10:40:27 +02:00
|
|
|
|
2015-06-13 20:51:02 +02:00
|
|
|
private static function codePointToUtf8($num) {
|
|
|
|
if ($num <= 0x7F) {
|
|
|
|
return chr($num);
|
|
|
|
}
|
|
|
|
if ($num <= 0x7FF) {
|
|
|
|
return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
|
|
|
|
}
|
|
|
|
if ($num <= 0xFFFF) {
|
|
|
|
return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
|
|
|
|
}
|
|
|
|
if ($num <= 0x1FFFFF) {
|
|
|
|
return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
|
|
|
|
. chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
|
|
|
|
}
|
|
|
|
throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
|
|
|
|
}
|
|
|
|
|
2011-12-04 16:52:43 +01:00
|
|
|
/**
|
2014-09-30 20:23:25 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2011-12-04 16:52:43 +01:00
|
|
|
* Parses a constant doc string.
|
|
|
|
*
|
|
|
|
* @param string $startToken Doc string start token content (<<<SMTHG)
|
|
|
|
* @param string $str String token content
|
2015-06-13 20:51:02 +02:00
|
|
|
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
|
2011-12-04 16:52:43 +01:00
|
|
|
*
|
|
|
|
* @return string Parsed string
|
|
|
|
*/
|
2015-06-13 20:51:02 +02:00
|
|
|
public static function parseDocString($startToken, $str, $parseUnicodeEscape = true) {
|
2011-12-04 16:52:43 +01:00
|
|
|
// strip last newline (thanks tokenizer for sticking it into the string!)
|
2015-09-19 16:05:23 +02:00
|
|
|
$str = preg_replace('~(\r\n|\n|\r)\z~', '', $str);
|
2011-12-04 16:52:43 +01:00
|
|
|
|
|
|
|
// nowdoc string
|
|
|
|
if (false !== strpos($startToken, '\'')) {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:51:02 +02:00
|
|
|
return self::parseEscapeSequences($str, null, $parseUnicodeEscape);
|
2011-12-04 16:52:43 +01:00
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|