菜谱项目

String_.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace PhpParser\Node\Scalar;
  3. use PhpParser\Error;
  4. use PhpParser\Node\Scalar;
  5. class String_ extends Scalar
  6. {
  7. /* For use in "kind" attribute */
  8. const KIND_SINGLE_QUOTED = 1;
  9. const KIND_DOUBLE_QUOTED = 2;
  10. const KIND_HEREDOC = 3;
  11. const KIND_NOWDOC = 4;
  12. /** @var string String value */
  13. public $value;
  14. protected static $replacements = array(
  15. '\\' => '\\',
  16. '$' => '$',
  17. 'n' => "\n",
  18. 'r' => "\r",
  19. 't' => "\t",
  20. 'f' => "\f",
  21. 'v' => "\v",
  22. 'e' => "\x1B",
  23. );
  24. /**
  25. * Constructs a string scalar node.
  26. *
  27. * @param string $value Value of the string
  28. * @param array $attributes Additional attributes
  29. */
  30. public function __construct($value, array $attributes = array()) {
  31. parent::__construct($attributes);
  32. $this->value = $value;
  33. }
  34. public function getSubNodeNames() {
  35. return array('value');
  36. }
  37. /**
  38. * @internal
  39. *
  40. * Parses a string token.
  41. *
  42. * @param string $str String token content
  43. * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
  44. *
  45. * @return string The parsed string
  46. */
  47. public static function parse($str, $parseUnicodeEscape = true) {
  48. $bLength = 0;
  49. if ('b' === $str[0] || 'B' === $str[0]) {
  50. $bLength = 1;
  51. }
  52. if ('\'' === $str[$bLength]) {
  53. return str_replace(
  54. array('\\\\', '\\\''),
  55. array( '\\', '\''),
  56. substr($str, $bLength + 1, -1)
  57. );
  58. } else {
  59. return self::parseEscapeSequences(
  60. substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
  61. );
  62. }
  63. }
  64. /**
  65. * @internal
  66. *
  67. * Parses escape sequences in strings (all string types apart from single quoted).
  68. *
  69. * @param string $str String without quotes
  70. * @param null|string $quote Quote type
  71. * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
  72. *
  73. * @return string String with escape sequences parsed
  74. */
  75. public static function parseEscapeSequences($str, $quote, $parseUnicodeEscape = true) {
  76. if (null !== $quote) {
  77. $str = str_replace('\\' . $quote, $quote, $str);
  78. }
  79. $extra = '';
  80. if ($parseUnicodeEscape) {
  81. $extra = '|u\{([0-9a-fA-F]+)\}';
  82. }
  83. return preg_replace_callback(
  84. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
  85. function($matches) {
  86. $str = $matches[1];
  87. if (isset(self::$replacements[$str])) {
  88. return self::$replacements[$str];
  89. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  90. return chr(hexdec($str));
  91. } elseif ('u' === $str[0]) {
  92. return self::codePointToUtf8(hexdec($matches[2]));
  93. } else {
  94. return chr(octdec($str));
  95. }
  96. },
  97. $str
  98. );
  99. }
  100. private static function codePointToUtf8($num) {
  101. if ($num <= 0x7F) {
  102. return chr($num);
  103. }
  104. if ($num <= 0x7FF) {
  105. return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
  106. }
  107. if ($num <= 0xFFFF) {
  108. return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
  109. }
  110. if ($num <= 0x1FFFFF) {
  111. return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
  112. . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
  113. }
  114. throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
  115. }
  116. /**
  117. * @internal
  118. *
  119. * Parses a constant doc string.
  120. *
  121. * @param string $startToken Doc string start token content (<<<SMTHG)
  122. * @param string $str String token content
  123. * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
  124. *
  125. * @return string Parsed string
  126. */
  127. public static function parseDocString($startToken, $str, $parseUnicodeEscape = true) {
  128. // strip last newline (thanks tokenizer for sticking it into the string!)
  129. $str = preg_replace('~(\r\n|\n|\r)\z~', '', $str);
  130. // nowdoc string
  131. if (false !== strpos($startToken, '\'')) {
  132. return $str;
  133. }
  134. return self::parseEscapeSequences($str, null, $parseUnicodeEscape);
  135. }
  136. }