You are here

class Unescaper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/yaml/Unescaper.php \Symfony\Component\Yaml\Unescaper

Unescaper encapsulates unescaping rules for single and double-quoted YAML strings.

@author Matthew Lewinski <matthew@lewinski.org>

Hierarchy

Expanded class hierarchy of Unescaper

File

vendor/symfony/yaml/Unescaper.php, line 20

Namespace

Symfony\Component\Yaml
View source
class Unescaper {

  /**
   * 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
   *
   * @internal
   */
  const ENCODING = 'UTF-8';

  /**
   * Regex fragment that matches an escaped character in a double quoted string.
   */
  const REGEX_ESCAPED_CHARACTER = "\\\\([0abt\tnvfre \\\"\\/\\\\N_LP]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})";

  /**
   * Unescapes a single quoted string.
   *
   * @param string $value A single quoted string.
   *
   * @return string The unescaped string.
   */
  public function unescapeSingleQuotedString($value) {
    return str_replace('\'\'', '\'', $value);
  }

  /**
   * Unescapes a double quoted string.
   *
   * @param string $value A double quoted string.
   *
   * @return string The unescaped string.
   */
  public function unescapeDoubleQuotedString($value) {
    $self = $this;
    $callback = function ($match) use ($self) {
      return $self
        ->unescapeCharacter($match[0]);
    };

    // evaluate the string
    return preg_replace_callback('/' . self::REGEX_ESCAPED_CHARACTER . '/u', $callback, $value);
  }

  /**
   * Unescapes a character that was found in a double-quoted string.
   *
   * @param string $value An escaped character
   *
   * @return string The unescaped character
   */
  public function unescapeCharacter($value) {
    switch ($value[1]) {
      case '0':
        return "\0";
      case 'a':
        return "\7";
      case 'b':
        return "\10";
      case 't':
        return "\t";
      case "\t":
        return "\t";
      case 'n':
        return "\n";
      case 'v':
        return "\v";
      case 'f':
        return "\f";
      case 'r':
        return "\r";
      case 'e':
        return "\33";
      case ' ':
        return ' ';
      case '"':
        return '"';
      case '/':
        return '/';
      case '\\':
        return '\\';
      case 'N':

        // U+0085 NEXT LINE
        return "…";
      case '_':

        // U+00A0 NO-BREAK SPACE
        return " ";
      case 'L':

        // U+2028 LINE SEPARATOR
        return "
";
      case 'P':

        // U+2029 PARAGRAPH SEPARATOR
        return "
";
      case 'x':
        return self::utf8chr(hexdec(substr($value, 2, 2)));
      case 'u':
        return self::utf8chr(hexdec(substr($value, 2, 4)));
      case 'U':
        return self::utf8chr(hexdec(substr($value, 2, 8)));
    }
  }

  /**
   * Get the UTF-8 character for the given code point.
   *
   * @param int $c The unicode code point
   *
   * @return string The corresponding UTF-8 character
   */
  private static function utf8chr($c) {
    if (0x80 > ($c %= 0x200000)) {
      return chr($c);
    }
    if (0x800 > $c) {
      return chr(0xc0 | $c >> 6) . chr(0x80 | $c & 0x3f);
    }
    if (0x10000 > $c) {
      return chr(0xe0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3f) . chr(0x80 | $c & 0x3f);
    }
    return chr(0xf0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3f) . chr(0x80 | $c >> 6 & 0x3f) . chr(0x80 | $c & 0x3f);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Unescaper::ENCODING Deprecated constant Parser and Inline assume UTF-8 encoding, so escaped Unicode characters must be converted to that encoding.
Unescaper::REGEX_ESCAPED_CHARACTER constant Regex fragment that matches an escaped character in a double quoted string.
Unescaper::unescapeCharacter public function Unescapes a character that was found in a double-quoted string.
Unescaper::unescapeDoubleQuotedString public function Unescapes a double quoted string.
Unescaper::unescapeSingleQuotedString public function Unescapes a single quoted string.
Unescaper::utf8chr private static function Get the UTF-8 character for the given code point.