PageRenderTime 89ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/quizbd-master-main/vendor/symfony/translation/Extractor/PhpStringTokenParser.php

https://gitlab.com/IR31121994/quizbd-master
PHP | 136 lines | 75 code | 7 blank | 54 comment | 4 complexity | 1bc1ced5ec089cf45fbf9c3572e827e1 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Extractor;
  11. /*
  12. * The following is derived from code at http://github.com/nikic/PHP-Parser
  13. *
  14. * Copyright (c) 2011 by Nikita Popov
  15. *
  16. * Some rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are
  20. * met:
  21. *
  22. * * Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * * Redistributions in binary form must reproduce the above
  26. * copyright notice, this list of conditions and the following
  27. * disclaimer in the documentation and/or other materials provided
  28. * with the distribution.
  29. *
  30. * * The names of the contributors may not be used to endorse or
  31. * promote products derived from this software without specific
  32. * prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. class PhpStringTokenParser
  47. {
  48. protected static $replacements = [
  49. '\\' => '\\',
  50. '$' => '$',
  51. 'n' => "\n",
  52. 'r' => "\r",
  53. 't' => "\t",
  54. 'f' => "\f",
  55. 'v' => "\v",
  56. 'e' => "\x1B",
  57. ];
  58. /**
  59. * Parses a string token.
  60. *
  61. * @param string $str String token content
  62. */
  63. public static function parse(string $str): string
  64. {
  65. $bLength = 0;
  66. if ('b' === $str[0]) {
  67. $bLength = 1;
  68. }
  69. if ('\'' === $str[$bLength]) {
  70. return str_replace(
  71. ['\\\\', '\\\''],
  72. ['\\', '\''],
  73. substr($str, $bLength + 1, -1)
  74. );
  75. } else {
  76. return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
  77. }
  78. }
  79. /**
  80. * Parses escape sequences in strings (all string types apart from single quoted).
  81. *
  82. * @param string $str String without quotes
  83. * @param string|null $quote Quote type
  84. */
  85. public static function parseEscapeSequences(string $str, string $quote = null): string
  86. {
  87. if (null !== $quote) {
  88. $str = str_replace('\\'.$quote, $quote, $str);
  89. }
  90. return preg_replace_callback(
  91. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
  92. [__CLASS__, 'parseCallback'],
  93. $str
  94. );
  95. }
  96. private static function parseCallback(array $matches): string
  97. {
  98. $str = $matches[1];
  99. if (isset(self::$replacements[$str])) {
  100. return self::$replacements[$str];
  101. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  102. return \chr(hexdec($str));
  103. } else {
  104. return \chr(octdec($str));
  105. }
  106. }
  107. /**
  108. * Parses a constant doc string.
  109. *
  110. * @param string $startToken Doc string start token content (<<<SMTHG)
  111. * @param string $str String token content
  112. */
  113. public static function parseDocString(string $startToken, string $str): string
  114. {
  115. // strip last newline (thanks tokenizer for sticking it into the string!)
  116. $str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
  117. // nowdoc string
  118. if (str_contains($startToken, '\'')) {
  119. return $str;
  120. }
  121. return self::parseEscapeSequences($str, null);
  122. }
  123. }