function zip_longest_match()

in themes/docsy/static/js/deflate.js [414:492]


    function zip_longest_match(cur_match) {
        var chain_length = zip_max_chain_length; // max hash chain length
        var scanp = zip_strstart; // current string
        var matchp;		// matched string
        var len;		// length of current match
        var best_len = zip_prev_length;	// best match length so far

        /* Stop when cur_match becomes <= limit. To simplify the code,
            * we prevent matches with the string of window index 0.
            */
        var limit = (zip_strstart > zip_MAX_DIST ? zip_strstart - zip_MAX_DIST : zip_NIL);

        var strendp = zip_strstart + zip_MAX_MATCH;
        var scan_end1 = zip_window[scanp + best_len - 1];
        var scan_end = zip_window[scanp + best_len];

        /* Do not waste too much time if we already have a good match: */
        if (zip_prev_length >= zip_good_match)
            chain_length >>= 2;

        //  Assert(encoder->strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");

        do {
            //    Assert(cur_match < encoder->strstart, "no future");
            matchp = cur_match;

            /* Skip to next match if the match length cannot increase
                * or if the match length is less than 2:
            */
            if (zip_window[matchp + best_len] != scan_end ||
                zip_window[matchp + best_len - 1] != scan_end1 ||
                zip_window[matchp] != zip_window[scanp] ||
                zip_window[++matchp] != zip_window[scanp + 1]) {
                continue;
            }

            /* The check at best_len-1 can be removed because it will be made
                    * again later. (This heuristic is not always a win.)
                    * It is not necessary to compare scan[2] and match[2] since they
                    * are always equal when the other bytes match, given that
                    * the hash keys are equal and that HASH_BITS >= 8.
                    */
            scanp += 2;
            matchp++;

            /* We check for insufficient lookahead only every 8th comparison;
                    * the 256th check will be made at strstart+258.
                    */
            do {
            } while (zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
            zip_window[++scanp] == zip_window[++matchp] &&
                scanp < strendp);

            len = zip_MAX_MATCH - (strendp - scanp);
            scanp = strendp - zip_MAX_MATCH;

            if (len > best_len) {
                zip_match_start = cur_match;
                best_len = len;
                if (zip_FULL_SEARCH) {
                    if (len >= zip_MAX_MATCH) break;
                } else {
                    if (len >= zip_nice_match) break;
                }

                scan_end1 = zip_window[scanp + best_len - 1];
                scan_end = zip_window[scanp + best_len];
            }
        } while ((cur_match = zip_prev[cur_match & zip_WMASK]) > limit
            && --chain_length != 0);

        return best_len;
    }