private int consumeEscape() {
        if (isEof()) {
            return REPLACEMENT_CHAR;
        }

        if (isHexDigit(peek())) {
            int uc = 0;
            int len = 6;
            while (len > 0 && isHexDigit(peek())) {
                uc = (uc << 4) + hexValue(next());
                --len;
            }

            if (uc == 0 || uc > Character.MAX_CODE_POINT || (uc >= 0xD800 && uc <= 0xDFFF)) {
                uc = REPLACEMENT_CHAR;
            }

            if (isSpace(peek())) {
                next();
            }

            return uc;
        }

        return next();
    }