PageRenderTime 22ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/String_.php

https://gitlab.com/ealexis.t/trends
PHP | 153 lines | 114 code | 10 blank | 29 comment | 5 complexity | 112dcb8a5c853aadf293435968e5132f MD5 | raw file
  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. }