Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Utils.php

function Utils::unquoteLiteralString

This function takes a string as it appears in the source code and returns an unquoted version of it

Parameters

string $str The string to unquote:

Return value

string

2 calls to Utils::unquoteLiteralString()
StringLiteral::setRaw in vendor/mck89/peast/lib/Peast/Syntax/Node/StringLiteral.php
Sets node's raw value
TemplateElement::setRawValue in vendor/mck89/peast/lib/Peast/Syntax/Node/TemplateElement.php
Sets node's raw value that must be wrapped in templates quotes.

File

vendor/mck89/peast/lib/Peast/Syntax/Utils.php, line 129

Class

Utils
Utilities class.

Namespace

Peast\Syntax

Code

public static function unquoteLiteralString($str) {
    
    //Remove quotes
    $str = substr($str, 1, -1);
    
    //Return immediately if the escape character is missing
    if (strpos($str, "\\") === false) {
        return $str;
    }
    $lineTerminators = self::getLineTerminators();
    
    //Surrogate pairs regex
    $surrogatePairsReg = sprintf('u(?:%1$s|\\{%1$s\\})\\\\u(?:%2$s|\\{%2$s\\})', "[dD][89abAB][0-9a-fA-F]{2}", "[dD][c-fC-F][0-9a-fA-F]{2}");
    
    //Handle escapes
    $patterns = array(
        $surrogatePairsReg,
        "u\\{[a-fA-F0-9]+\\}",
        "u[a-fA-F0-9]{4}",
        "x[a-fA-F0-9]{2}",
        "0[0-7]{2}",
        "[1-7][0-7]",
        ".",
    );
    $reg = "/\\\\(" . implode("|", $patterns) . ")/s";
    $simpleSequence = array(
        "n" => "\n",
        "f" => "\f",
        "r" => "\r",
        "t" => "\t",
        "v" => "\v",
        "b" => "\x08",
    );
    $replacement = function ($m) use ($simpleSequence, $lineTerminators) {
        $type = $m[1][0];
        if (isset($simpleSequence[$type])) {
            // \n, \r, \t ...
            return $simpleSequence[$type];
        }
        elseif ($type === "u" || $type === "x") {
            
            //Invalid unicode or hexadecimal sequences
            if (strlen($m[1]) === 1) {
                return "\\{$type}";
            }
            // Surrogate pair
            if ($type === "u" && strpos($m[1], "\\") !== false) {
                $points = explode("\\", $m[1]);
                return Utils::surrogatePairToUtf8(str_replace(array(
                    "{",
                    "}",
                    "u",
                ), "", $points[0]), str_replace(array(
                    "{",
                    "}",
                    "u",
                ), "", $points[1]));
            }
            // \uFFFF, \u{FFFF}, \xFF
            $code = substr($m[1], 1);
            $code = str_replace(array(
                "{",
                "}",
            ), "", $code);
            return Utils::unicodeToUtf8(hexdec($code));
        }
        elseif ($type >= "0" && $type <= "7") {
            
            //Invalid octal sequences
            if (strlen($m[1]) === 1) {
                return "\\{$type}";
            }
            // \123
            return Utils::unicodeToUtf8(octdec($m[1]));
        }
        elseif (in_array($m[1], $lineTerminators)) {
            // Escaped line terminators
            return "";
        }
        else {
            // Escaped characters
            return $m[1];
        }
    };
    return preg_replace_callback($reg, $replacement, $str);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal