Unescapes the character identified by the character or characters that
immediately follow a backslash. The backslash '\' should have already
been read. This supports both unicode escapes "u000A" and two-character
escapes "\n".
2979
private char readEscapeCharacter() throws JSONException {
        char escaped = in.charAt(pos++);
        switch (escaped) {
            case 'u':
                if (pos + 4 > in.length()) {
                    throw syntaxError("Unterminated escape sequence");
                }
                String hex = in.substring(pos, pos + 4);
                pos += 4;
                try {
                    return (char) Integer.parseInt(hex, 16);
                } catch (NumberFormatException nfe) {
                    throw syntaxError("Invalid escape sequence: " + hex);
                }

            case 't':
                return '\t';

            case 'b':
                return '\b';

            case 'n':
                return '\n';

            case 'r':
                return '\r';

            case 'f':
                return '\f';

            case '\'':
            case '"':
            case '\\':
            default:
                return escaped;
        }
    }
61494
function unescapeString(str) {
    let i = 0, result = '';
    const len = str.length;

    while (i < len) {
        const ch = str[i++];
        result += (ch === '\\') ? (str[i++] || '') : ch;
    }

    return result;
}
1301086
static void unescape(final char[] text, final int offset, final int len, final Writer writer)
                         throws IOException {

        if (text == null) {
            return;
        }

        final int max = (offset + len);

        int readOffset = offset;
        int referenceOffset = offset;

        for (int i = offset; i < max; i++) {

            final char c = text[i];

            /*
             * Check the need for an unescape operation at this point
             */

            if (c != ESCAPE_PREFIX || (i + 1) >= max) {
                continue;
            }

            int codepoint = -1;

            if (c == ESCAPE_PREFIX) {

                final char c1 = text[i + 1];

                switch (c1) {
                    case '0': if (!isOctalEscape(text,i + 1,max)) { codepoint = 0x00; referenceOffset = i + 1; }; break;
                    case 'b': codepoint = 0x08; referenceOffset = i + 1; break;
                    case 't': codepoint = 0x09; referenceOffset = i + 1; break;
                    case 'n': codepoint = 0x0A; referenceOffset = i + 1; break;
                    case 'v': codepoint = 0x0B; referenceOffset = i + 1; break;
                    case 'f': codepoint = 0x0C; referenceOffset = i + 1; break;
                    case 'r': codepoint = 0x0D; referenceOffset = i + 1; break;
                    case '"': codepoint = 0x22; referenceOffset = i + 1; break;
                    case '\'': codepoint = 0x27; referenceOffset = i + 1; break;
                    case '\\': codepoint = 0x5C; referenceOffset = i + 1; break;
                    case '/': codepoint = 0x2F; referenceOffset = i + 1; break;
                    // line feed. When escaped, this is a line continuator
                    case '\n': codepoint = -2; referenceOffset = i + 1; break;
                }

                if (codepoint == -1) {

                    if (c1 == ESCAPE_XHEXA_PREFIX2) {
                        // This can be a xhexa escape, we need exactly two more characters

                        int f = i + 2;
                        while (f < (i + 4) && f < max) {
                            final char cf = text[f];
                            if (!((cf >= '0' && cf <= '9') || (cf >= 'A' && cf <= 'F') || (cf >= 'a' && cf <= 'f'))) {
                                break;
                            }
                            f++;
                        }

                        if ((f - (i + 2)) < 2) {
                            // We weren't able to consume the required two hexa chars, leave it as slash+'x', which
                            // is invalid, and let the corresponding JavaScript engine fail.
                            i++;
                            continue;
                        }

                        codepoint = parseIntFromReference(text, i + 2, f, 16);

                        // Fast-forward to the first char after the parsed codepoint
                        referenceOffset = f - 1;

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c1 == ESCAPE_UHEXA_PREFIX2) {
                        // This can be a uhexa escape, we need exactly four more characters

                        int f = i + 2;
                        while (f < (i + 6) && f < max) {
                            final char cf = text[f];
                            if (!((cf >= '0' && cf <= '9') || (cf >= 'A' && cf <= 'F') || (cf >= 'a' && cf <= 'f'))) {
                                break;
                            }
                            f++;
                        }

                        if ((f - (i + 2)) < 4) {
                            // We weren't able to consume the required four hexa chars, leave it as slash+'u', which
                            // is invalid, and let the corresponding JavaScript engine fail.
                            i++;
                            continue;
                        }

                        codepoint = parseIntFromReference(text, i + 2, f, 16);

                        // Fast-forward to the first char after the parsed codepoint
                        referenceOffset = f - 1;

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c1 >= '0' && c1 <= '7') {
                        // This can be a octal escape, we need at least 1 more char, and up to 3 more.

                        int f = i + 2;
                        while (f < (i + 4) && f < max) { // We need only a max of two more chars
                            final char cf = text[f];
                            if (!(cf >= '0' && cf <= '7')) {
                                break;
                            }
                            f++;
                        }

                        codepoint = parseIntFromReference(text, i + 1, f, 8);
                        if (codepoint > 0xFF) {
                            // Maximum octal escape char is FF. Ignore the last digit
                            codepoint = parseIntFromReference(text, i + 1, f - 1, 8);
                            referenceOffset = f - 2;
                        } else {
                            referenceOffset = f - 1;
                        }

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c1 == '8' || c1 == '9' || c1 == '\r' || c1 == '\u2028' || c1 == '\u2029') {

                        // '8' and '9' are not valid octal escape sequences, and the other four characters
                        // are LineTerminators, which are not allowed as escape sequences. So we leave it as is
                        // and expect the corresponding JavaScript engine to fail.
                        i++;
                        continue;

                    } else {

                        // We weren't able to consume any valid escape chars, just consider it a normal char,
                        // which is allowed by the JavaScript specification (NonEscapeCharacter)

                        codepoint = (int) c1;
                        referenceOffset = i + 1;

                    }

                }

            }


            /*
             * At this point we know for sure we will need some kind of unescape, so we
             * write all the contents pending up to this point.
             */

            if (i - readOffset > 0) {
                writer.write(text, readOffset, (i - readOffset));
            }

            i = referenceOffset;
            readOffset = i + 1;

            /*
             * --------------------------
             *
             * Perform the real unescape
             *
             * --------------------------
             */

            if (codepoint > '\uFFFF') {
                writer.write(Character.toChars(codepoint));
            } else if (codepoint != -2){ // We use -2 to signal the line continuator, which should be ignored in output
                writer.write((char)codepoint);
            }

        }


        /*
         * -----------------------------------------------------------------------------------------------
         * Final cleaning: writer the remaining escaped text and return.
         * -----------------------------------------------------------------------------------------------
         */

        if (max - readOffset > 0) {
            writer.write(text, readOffset, (max - readOffset));
        }

    }
1344216
function unescape(str) {

    return str.replace(constants.REGEX.unescape, (match, hex1, hex2, hex3, octal, special) => {

        let hex = hex1 !== undefined ? hex1 : hex2 !== undefined ? hex2 : hex3 !== undefined ? hex3 : undefined;

        if (hex !== undefined) {
            return String.fromCodePoint(parseInt(hex, 16));

        } else if (octal !== undefined) {
            return String.fromCodePoint(parseInt(octal, 8));

        } else {
            return constants.SPECIAL_CHARS[special];
        }

    });

}
807690
private static String unescapeString(String str){
        if(str.startsWith("\"") && str.endsWith("\"")){
            String s = str.replaceAll("\\\\\"", "\"");
            return s.substring(1, s.length() -1);
        }
        return null;
    }
583009
private static String unescape( String str, String encoding )
    {
        //We cannot unescape '+' to space because '+' is allowed in the file name
        //str = str.replace('+', ' ');
        
        //if the str does not contain "%", we don't need to do anything
        if ( str.indexOf( '%' ) < 0 )
        {
            return str;
        }

        if ( encoding == null || encoding.length() == 0 )
        {
            encoding = WLS_DEFAULT_ENCODING;
        }
        
        // Do not assume String only contains ascii.  str.length() <= str.getBytes().length
        int out = 0;

        byte[] strbytes = str.getBytes();
        int len = strbytes.length;

        boolean foundNonAscii = false;
        for ( int in = 0; in < len; in++, out++ )
        {
            if ( strbytes[in] == '%' && ( in + 2 < len ) )
            {
                if ( Hex.isHexChar( strbytes[in + 1] ) &&
                     Hex.isHexChar( strbytes[in + 2] ) )
                {
                    strbytes[out] =
                    ( byte ) ( ( Hex.hexValueOf( strbytes[in + 1] ) << 4 ) +
                               ( Hex.hexValueOf( strbytes[in + 2] ) << 0 ) );
                    in += 2;
                    continue;
                }
            }
            // IE takes non-ASCII URLs. We use the default encoding
            // if non-ASCII characters are contained in URLs.
            if ( !foundNonAscii &&
                 ( strbytes[in] <= 0x1f || strbytes[in] == 0x7f ) )
            {
                encoding = System.getProperty( "file.encoding" );
                foundNonAscii = true;
            }
            strbytes[out] = strbytes[in];
        }

        return newString( strbytes, 0, out, encoding );  // was:  BytesToString.newString(...)
    }
125962
def unescape_html(text):
    
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unicode_char(int(text[3:-1], 16))
                else:
                    return unicode_char(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unicode_char(htmlentities.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text  # leave as is
    return re.sub(r"&#?\w+;", fixup, text)
829596
function unescapeSpecialChars(text) {

        text = text.replace(/~E(\d+)E/g, function(wholeMatch,m1) {
            var charCodeToReplace = parseInt(m1);
            return String.fromCharCode(charCodeToReplace);
        });

        return text;
    }
176689
public static String unescapeLeniently(String s) {
        StringBuilder buf = new StringBuilder();
        int[] pos = new int[1];
        for (int i=0; i<s.length(); ) {
            char c = s.charAt(i++);
            if (c == '\\') {
                pos[0] = i;
                int e = unescapeAt(s, pos);
                if (e < 0) {
                    buf.append(c);
                } else {
                    buf.appendCodePoint(e);
                    i = pos[0];
                }
            } else {
                buf.append(c);
            }
        }
        return buf.toString();
    }
235176
function unescapeUnicode(str) {
    	    // unescape unicode codes
    	    var codes = [];
    	    var code = parseInt(str.substr(2), 16);
    	    if (code >= 0 && code < Math.pow(2, 16)) {
    		codes.push(code);
    	    }
    	    // convert codes to text
    	    var unescaped = '';
    	    for (var i = 0; i < codes.length; ++i) {
    		unescaped += String.fromCharCode(codes[i]);
    	    }
    	    return unescaped;
    	}
736285
def unescape( text ):
    """"""
    
    if isinstance( text, basestring ):
        if '&amp;' in text:
            text = text.replace( '&amp;', '&' )
        if '&gt;' in text:
            text = text.replace( '&gt;', '>' )
        if '&lt;' in text:
            text = text.replace( '&lt;', '<' )
        if '&quot;' in text:
            text = text.replace( '&quot;', '\"' )

    return text
218279
static void unescape(final char[] text, final int offset, final int len, final Writer writer)
                         throws IOException {

        if (text == null) {
            return;
        }

        char[] unicodeEscapedText = text;
        int unicodeEscapedOffset = offset;
        int unicodeEscapedLen = len;
        if (requiresUnicodeUnescape(text, offset, len)) {
            final CharArrayWriter charArrayWriter = new CharArrayWriter(len + 2);
            unicodeUnescape(text, offset, len, charArrayWriter);
            unicodeEscapedText = charArrayWriter.toCharArray();
            unicodeEscapedOffset = 0;
            unicodeEscapedLen = unicodeEscapedText.length;
        }

        final int max = (unicodeEscapedOffset + unicodeEscapedLen);

        int readOffset = unicodeEscapedOffset;
        int referenceOffset = unicodeEscapedOffset;

        for (int i = unicodeEscapedOffset; i < max; i++) {

            final char c = unicodeEscapedText[i];

            /*
             * Check the need for an unescape operation at this point
             */

            if (c != ESCAPE_PREFIX || (i + 1) >= max) {
                continue;
            }

            int codepoint = -1;

            if (c == ESCAPE_PREFIX) {

                final char c1 = unicodeEscapedText[i + 1];

                switch (c1) {
                    case '0': if (!isOctalEscape(unicodeEscapedText,i + 1,max)) { codepoint = 0x00; referenceOffset = i + 1; }; break;
                    case 'b': codepoint = 0x08; referenceOffset = i + 1; break;
                    case 't': codepoint = 0x09; referenceOffset = i + 1; break;
                    case 'n': codepoint = 0x0A; referenceOffset = i + 1; break;
                    case 'f': codepoint = 0x0C; referenceOffset = i + 1; break;
                    case 'r': codepoint = 0x0D; referenceOffset = i + 1; break;
                    case '"': codepoint = 0x22; referenceOffset = i + 1; break;
                    case '\'': codepoint = 0x27; referenceOffset = i + 1; break;
                    case '\\': codepoint = 0x5C; referenceOffset = i + 1; break;
                }

                if (codepoint == -1) {

                    if (c1 >= '0' && c1 <= '7') {
                        // This can be a octal escape, we need at least 1 more char, and up to 3 more.

                        int f = i + 2;
                        while (f < (i + 4) && f < max) { // We need only a max of two more chars
                            final char cf = unicodeEscapedText[f];
                            if (!(cf >= '0' && cf <= '7')) {
                                break;
                            }
                            f++;
                        }

                        codepoint = parseIntFromReference(unicodeEscapedText, i + 1, f, 8);

                        if (codepoint > 0xFF) {
                            // Maximum octal escape char is FF. Ignore the last digit
                            codepoint = parseIntFromReference(unicodeEscapedText, i + 1, f - 1, 8);
                            referenceOffset = f - 2;
                        } else {
                            referenceOffset = f - 1;
                        }

                        // Don't continue here, just let the unescape code below do its job

                    } else {

                        // Other escape sequences are not allowed by Java. So we leave it as is
                        // and expect the corresponding Java parser to fail.
                        i++;
                        continue;

                    }

                }

            }


            /*
             * At this point we know for sure we will need some kind of unescape, so we
             * can copy all the contents pending up to this point.
             */

            if (i - readOffset > 0) {
                writer.write(unicodeEscapedText, readOffset, (i - readOffset));
            }

            i = referenceOffset;
            readOffset = i + 1;

            /*
             * --------------------------
             *
             * Perform the real unescape
             *
             * --------------------------
             */

            if (codepoint > '\uFFFF') {
                writer.write(Character.toChars(codepoint));
            } else {
                writer.write((char)codepoint);
            }

        }


        /*
         * -----------------------------------------------------------------------------------------------
         * Final cleaning: append the remaining escaped text to the string builder and return.
         * -----------------------------------------------------------------------------------------------
         */

        if (max - readOffset > 0) {
            writer.write(unicodeEscapedText, readOffset, (max - readOffset));
        }


    }
1114855
def unescape(text):
    
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # leave as is

    return re.sub("&#?\w+;", fixup, text)
558657
def unescape(value):
    
    pattern = ESCAPE_FMT.replace('%02X', '(?P<code>[0-9A-Fa-f]{2})')
    # the pattern must be bytes to operate on bytes
    pattern_bytes = pattern.encode('ascii')
    re_esc = re.compile(pattern_bytes)
    return re_esc.sub(_unescape_code, value.encode('ascii')).decode('utf-8')
367138
static void unescape(final Reader reader, final Writer writer) throws IOException {

        if (reader == null) {
            return;
        }

        int escapei = 0;
        final char[] escapes = new char[4];
        int c1, c2, ce; // c1: current char, c2: next char, ce: current escape char

        c2 = reader.read();

        while (c2 >= 0) {

            c1 = c2;
            c2 = reader.read();

            escapei = 0;

            /*
             * Check the need for an unescape operation at this point
             */

            if (c1 != ESCAPE_PREFIX || c2 < 0) {
                writer.write(c1);
                continue;
            }

            int codepoint = -1;

            if (c1 == ESCAPE_PREFIX) {

                switch (c2) {
                    case 'b': codepoint = 0x08; c1 = c2; c2 = reader.read(); break;
                    case 't': codepoint = 0x09; c1 = c2; c2 = reader.read(); break;
                    case 'n': codepoint = 0x0A; c1 = c2; c2 = reader.read(); break;
                    case 'v': codepoint = 0x0B; c1 = c2; c2 = reader.read(); break;
                    case 'f': codepoint = 0x0C; c1 = c2; c2 = reader.read(); break;
                    case 'r': codepoint = 0x0D; c1 = c2; c2 = reader.read(); break;
                    case '"': codepoint = 0x22; c1 = c2; c2 = reader.read(); break;
                    case '\'': codepoint = 0x27; c1 = c2; c2 = reader.read(); break;
                    case '\\': codepoint = 0x5C; c1 = c2; c2 = reader.read(); break;
                    case '/': codepoint = 0x2F; c1 = c2; c2 = reader.read(); break;
                    // line feed. When escaped, this is a line continuator
                    case '\n': codepoint = -2; c1 = c2; c2 = reader.read(); break;
                }

                if (codepoint == -1) {

                    if (c2 == ESCAPE_XHEXA_PREFIX2) {
                        // This can be a xhexa escape, we need exactly two more characters

                        escapei = 0;
                        ce = reader.read();
                        while (ce >= 0 && escapei < 2) {
                            if (!((ce >= '0' && ce <= '9') || (ce >= 'A' && ce <= 'F') || (ce >= 'a' && ce <= 'f'))) {
                                break;
                            }
                            escapes[escapei] = (char) ce;
                            ce = reader.read();
                            escapei++;
                        }

                        if (escapei < 2) {
                            // We weren't able to consume the required four hexa chars, leave it as slash+'u', which
                            // is invalid, and let the corresponding JavaScript parser fail.
                            writer.write(c1);
                            writer.write(c2);
                            for (int i = 0; i < escapei; i++) {
                                c1 = c2;
                                c2 = escapes[i];
                                writer.write(c2);
                            }
                            c1 = c2;
                            c2 = ce;
                            continue;
                        }

                        c1 = escapes[3];
                        c2 = ce;

                        codepoint = parseIntFromReference(escapes, 0, 2, 16);

                        escapei = 0;

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c2 == ESCAPE_UHEXA_PREFIX2) {
                        // This can be a uhexa escape, we need exactly four more characters

                        escapei = 0;
                        ce = reader.read();
                        while (ce >= 0 && escapei < 4) {
                            if (!((ce >= '0' && ce <= '9') || (ce >= 'A' && ce <= 'F') || (ce >= 'a' && ce <= 'f'))) {
                                break;
                            }
                            escapes[escapei] = (char) ce;
                            ce = reader.read();
                            escapei++;
                        }

                        if (escapei < 4) {
                            // We weren't able to consume the required four hexa chars, leave it as slash+'u', which
                            // is invalid, and let the corresponding JavaScript parser fail.
                            writer.write(c1);
                            writer.write(c2);
                            for (int i = 0; i < escapei; i++) {
                                c1 = c2;
                                c2 = escapes[i];
                                writer.write(c2);
                            }
                            c1 = c2;
                            c2 = ce;
                            continue;
                        }

                        c1 = escapes[3];
                        c2 = ce;

                        codepoint = parseIntFromReference(escapes, 0, 4, 16);

                        escapei = 0;

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c2 >= '0' && c2 <= '7') {
                        // This can be a octal escape, we need at least 1 more char, and up to 3 more.

                        //  case '0': if (!isOctalEscape(text,i + 1,max)) { codepoint = 0x00; referenceOffset = i + 1; }; break;

                        escapei = 0;
                        ce = c2;
                        while (ce >= 0 && escapei < 3) {
                            if (!(ce >= '0' && ce <= '7')) {
                                break;
                            }
                            escapes[escapei] = (char) ce;
                            ce = reader.read();
                            escapei++;
                        }

                        c1 = escapes[escapei - 1];
                        c2 = ce;

                        codepoint = parseIntFromReference(escapes, 0, escapei, 8);

                        if (codepoint > 0xFF) {
                            // Maximum octal escape char is FF. Ignore the last digit
                            codepoint = parseIntFromReference(escapes, 0, escapei - 1, 8);
                            System.arraycopy(escapes, escapei - 2, escapes, 0, 1);
                            escapei = 1;
                        } else if (codepoint == 0x0 && escapei > 1) {
                            // In the case of '\000' , we will only consider the first '\0' as it is an escape on its
                            // own, and the rest of the '00' will have to be output. So we save them at the escapes array
                            System.arraycopy(escapes, 1, escapes, 0, escapei - 1);
                            escapei--;
                        } else {
                            escapei = 0;
                        }

                        // Don't continue here, just let the unescape code below do its job

                    } else if (c2 == '8' || c2 == '9' || c2 == '\r' || c2 == '\u2028' || c2 == '\u2029') {

                        // '8' and '9' are not valid octal escape sequences, and the other four characters
                        // are LineTerminators, which are not allowed as escape sequences. So we leave it as is
                        // and expect the corresponding JavaScript engine to fail.
                        writer.write(c1);
                        writer.write(c2);

                        c1 = c2;
                        c2 = reader.read();

                        continue;

                    } else {

                        // We weren't able to consume any valid escape chars, just consider it a normal char,
                        // which is allowed by the JavaScript specification (NonEscapeCharacter)

                        codepoint = c2;

                        c1 = c2;
                        c2 = reader.read();

                        escapei = 0;

                    }

                }

            }

            /*
             * --------------------------
             *
             * Perform the real unescape
             *
             * --------------------------
             */

            if (codepoint > '\uFFFF') {
                writer.write(Character.toChars(codepoint));
            } else if (codepoint != -2){ // We use -2 to signal the line continuator, which should be ignored in output
                writer.write((char)codepoint);
            }

            /*
             * ----------------------------------------
             * Cleanup, in case we had a partial match
             * ----------------------------------------
             */

            if (escapei > 0) {
                writer.write(escapes, 0, escapei);
                escapei = 0;
            }

        }

    }
75639
def unescape(s):
    """"""
    s = bytearray(s)
    i = 0
    while i < len(s):
        if s[i] == ord('%'):
            hex_bytes = bytes(s[i+1:i+3])
            value = int(hex_bytes.decode('ascii'), 16)
            s[i:i+3] = [value]
        i += 1
    return bytes(s)
829064
def unescape_all(string):
    """"""
    def escape_single(matchobj):
        return _unicode_for_entity_with_name(matchobj.group(1))
    return entities.sub(escape_single, string)