static void unescape(final char[] text, final int offset, final int len, final Writer writer,
                         final XmlEscapeSymbols symbols)
                         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 != REFERENCE_PREFIX || (i + 1) >= max) {
                continue;
            }

            int codepoint = 0;

            if (c == REFERENCE_PREFIX) {

                final char c1 = text[i + 1];

                if (c1 == '\u0020' || // SPACE
                        c1 == '\n' ||     // LF
                        c1 == '\u0009' || // TAB
                        c1 == '\u000C' || // FF
                        c1 == '\u003C' || // LES-THAN SIGN
                        c1 == '\u0026') { // AMPERSAND
                    // Not a character references. No characters are consumed, and nothing is returned.
                    continue;

                } else if (c1 == REFERENCE_NUMERIC_PREFIX2) {

                    if (i + 2 >= max) {
                        // No reference possible
                        continue;
                    }

                    final char c2 = text[i + 2];

                    if (c2 == REFERENCE_HEXA_PREFIX3 && (i + 3) < max) {
                        // This is a hexadecimal reference

                        int f = i + 3;
                        while (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 + 3)) <= 0) {
                            // We weren't able to consume any hexa chars
                            continue;
                        }

                        if ((f >= max) || text[f] != REFERENCE_SUFFIX) {
                            continue;
                        }

                        f++; // Count the REFERENCE_SUFFIX (semi-colon)

                        codepoint = parseIntFromReference(text, i + 3, f - 1, 16);
                        referenceOffset = f - 1;

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

                    } else if (c2 >= '0' && c2 <= '9') {
                        // This is a decimal reference

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

                        if ((f - (i + 2)) <= 0) {
                            // We weren't able to consume any decimal chars
                            continue;
                        }

                        if ((f >= max) || text[f] != REFERENCE_SUFFIX) {
                            continue;
                        }

                        f++; // Count the REFERENCE_SUFFIX (semi-colon)

                        codepoint = parseIntFromReference(text, i + 2, f - 1, 10);
                        referenceOffset = f - 1;

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

                    } else {
                        // This is not a valid reference, just discard
                        continue;
                    }


                } else {

                    // This is a named reference, must be comprised only of ALPHABETIC chars

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

                    if ((f - (i + 1)) <= 0) {
                        // We weren't able to consume any alphanumeric
                        continue;
                    }

                    if ((f < max) && text[f] == REFERENCE_SUFFIX) {
                        f++;
                    }

                    final int ncrPosition = XmlEscapeSymbols.binarySearch(symbols.SORTED_CERS, text, i, f);
                    if (ncrPosition >= 0) {
                        codepoint = symbols.SORTED_CODEPOINTS_BY_CER[ncrPosition];
                    } else {
                        // Not found! Just ignore our efforts to find a match.
                        continue;
                    }

                    referenceOffset = f - 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 {
                writer.write((char) codepoint);
            }

        }


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

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

    }
static void unescape ( final char [ ] text , final int offset , final int len , final Writer writer , final XmlEscapeSymbols symbols ) 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 ] ; if ( c != REFERENCE_PREFIX || ( i + 1 ) >= max ) { continue ; } int codepoint = 0 ; if ( c == REFERENCE_PREFIX ) { final char c1 = text [ i + 1 ] ; if ( c1 == ' ' || c1 == '\n' || c1 == '	' || c1 == '' || c1 == '<' || c1 == '&' ) { continue ; } else if ( c1 == REFERENCE_NUMERIC_PREFIX2 ) { if ( i + 2 >= max ) { continue ; } final char c2 = text [ i + 2 ] ; if ( c2 == REFERENCE_HEXA_PREFIX3 && ( i + 3 ) < max ) { int f = i + 3 ; while ( 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 + 3 ) ) <= 0 ) { continue ; } if ( ( f >= max ) || text [ f ] != REFERENCE_SUFFIX ) { continue ; } f ++ ; codepoint = parseIntFromReference ( text , i + 3 , f - 1 , 16 ) ; referenceOffset = f - 1 ; } else if ( c2 >= '0' && c2 <= '9' ) { int f = i + 2 ; while ( f < max ) { final char cf = text [ f ] ; if ( ! ( cf >= '0' && cf <= '9' ) ) { break ; } f ++ ; } if ( ( f - ( i + 2 ) ) <= 0 ) { continue ; } if ( ( f >= max ) || text [ f ] != REFERENCE_SUFFIX ) { continue ; } f ++ ; codepoint = parseIntFromReference ( text , i + 2 , f - 1 , 10 ) ; referenceOffset = f - 1 ; } else { continue ; } } else { int f = i + 1 ; while ( f < max ) { final char cf = text [ f ] ; if ( ! ( ( cf >= 'a' && cf <= 'z' ) || ( cf >= 'A' && cf <= 'Z' ) || ( cf >= '0' && cf <= '9' ) ) ) { break ; } f ++ ; } if ( ( f - ( i + 1 ) ) <= 0 ) { continue ; } if ( ( f < max ) && text [ f ] == REFERENCE_SUFFIX ) { f ++ ; } final int ncrPosition = XmlEscapeSymbols . binarySearch ( symbols . SORTED_CERS , text , i , f ) ; if ( ncrPosition >= 0 ) { codepoint = symbols . SORTED_CODEPOINTS_BY_CER [ ncrPosition ] ; } else { continue ; } referenceOffset = f - 1 ; } } if ( i - readOffset > 0 ) { writer . write ( text , readOffset , ( i - readOffset ) ) ; } i = referenceOffset ; readOffset = i + 1 ; if ( codepoint > '' ) { writer . write ( Character . toChars ( codepoint ) ) ; } else { writer . write ( ( char ) codepoint ) ; } } if ( max - readOffset > 0 ) { writer . write ( text , readOffset , ( max - readOffset ) ) ; } }
/*
Perform an unescape operation based on char[].