wp/wp-includes/Text/Diff/Engine/native.php
author ymh <ymh.work@gmail.com>
Mon, 08 Sep 2025 19:44:41 +0200
changeset 23 417f20492bf7
parent 21 48c4eec2b7e6
permissions -rw-r--r--
Update Docker configuration and plugin versions - Upgrade MariaDB from 10.6 to 11 with auto-upgrade support - Add WordPress debug environment variable to FPM container - Update PHP-FPM Dockerfile base image - Update Include Mastodon Feed plugin with bug fixes and improvements - Update Portfolio plugin (v2.58) with latest translations and demo data enhancements - Remove old README.md from Mastodon Feed plugin 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Class used internally by Text_Diff to actually compute the diffs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * This class is implemented using native PHP code.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * The algorithm used here is mostly lifted from the perl module
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     9
 * https://cpan.metacpan.org/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * Some ideas (and a bit of code) are taken from analyze.c, of GNU
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * diffutils-2.7, which can be found at:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * code was written by him, and is used/adapted with his permission.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * See the enclosed file COPYING for license information (LGPL). If you did
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    24
 * not receive this file, see https://opensource.org/license/lgpl-2-1/.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * @package Text_Diff
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
class Text_Diff_Engine_native {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    31
    public $xchanged;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    32
    public $ychanged;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    33
    public $xv;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    34
    public $yv;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    35
    public $xind;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    36
    public $yind;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    37
    public $seq;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    38
    public $in_seq;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    39
    public $lcs;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    40
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    function diff($from_lines, $to_lines)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        $n_from = count($from_lines);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $n_to = count($to_lines);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        $this->xchanged = $this->ychanged = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        $this->xv = $this->yv = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        $this->xind = $this->yind = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        unset($this->seq);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        unset($this->in_seq);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        unset($this->lcs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        // Skip leading common lines.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            if ($from_lines[$skip] !== $to_lines[$skip]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            $this->xchanged[$skip] = $this->ychanged[$skip] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        // Skip trailing common lines.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        $xi = $n_from; $yi = $n_to;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            if ($from_lines[$xi] !== $to_lines[$yi]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            $this->xchanged[$xi] = $this->ychanged[$yi] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        // Ignore lines which do not exist in both files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            $xhash[$from_lines[$xi]] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            $line = $to_lines[$yi];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            if (($this->ychanged[$yi] = empty($xhash[$line]))) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            $yhash[$line] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            $this->yv[] = $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            $this->yind[] = $yi;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            $line = $from_lines[$xi];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            if (($this->xchanged[$xi] = empty($yhash[$line]))) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            $this->xv[] = $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            $this->xind[] = $xi;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        // Find the LCS.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        $this->_compareseq(0, count($this->xv), 0, count($this->yv));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        // Merge edits when possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        // Compute the edit operations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        $edits = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        $xi = $yi = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        while ($xi < $n_from || $yi < $n_to) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
            assert($yi < $n_to || $this->xchanged[$xi]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            assert($xi < $n_from || $this->ychanged[$yi]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
            // Skip matching "snake".
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
            $copy = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            while ($xi < $n_from && $yi < $n_to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                   && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                $copy[] = $from_lines[$xi++];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                ++$yi;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            if ($copy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
                $edits[] = new Text_Diff_Op_copy($copy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            // Find deletes & adds.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
            $delete = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            while ($xi < $n_from && $this->xchanged[$xi]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                $delete[] = $from_lines[$xi++];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            $add = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            while ($yi < $n_to && $this->ychanged[$yi]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                $add[] = $to_lines[$yi++];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
            if ($delete && $add) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                $edits[] = new Text_Diff_Op_change($delete, $add);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            } elseif ($delete) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
                $edits[] = new Text_Diff_Op_delete($delete);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            } elseif ($add) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
                $edits[] = new Text_Diff_Op_add($add);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        return $edits;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
     * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
     * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
     * segments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
     * Returns (LCS, PTS).  LCS is the length of the LCS. PTS is an array of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
     * NCHUNKS+1 (X, Y) indexes giving the diving points between sub
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
     * sequences.  The first sub-sequence is contained in (X0, X1), (Y0, Y1),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
     * the second in (X1, X2), (Y1, Y2) and so on.  Note that (X0, Y0) ==
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
     * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
     * This function assumes that the first lines of the specified portions of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
     * the two files do not match, and likewise that the last lines do not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
     * match.  The caller must trim matching lines from the beginning and end
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
     * of the portions it is going to specify.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
    function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
    {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        $flip = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        if ($xlim - $xoff > $ylim - $yoff) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
            /* Things seems faster (I'm not sure I understand why) when the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
             * shortest sequence is in X. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            $flip = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
            list ($xoff, $xlim, $yoff, $ylim)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
                = array($yoff, $ylim, $xoff, $xlim);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        if ($flip) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
            for ($i = $ylim - 1; $i >= $yoff; $i--) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
                $ymatches[$this->xv[$i]][] = $i;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
            for ($i = $ylim - 1; $i >= $yoff; $i--) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
                $ymatches[$this->yv[$i]][] = $i;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        $this->lcs = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        $this->seq[0]= $yoff - 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        $this->in_seq = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        $ymids[0] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        $numer = $xlim - $xoff + $nchunks - 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        $x = $xoff;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        for ($chunk = 0; $chunk < $nchunks; $chunk++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
            if ($chunk > 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
                for ($i = 0; $i <= $this->lcs; $i++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
                    $ymids[$i][$chunk - 1] = $this->seq[$i];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
            $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
            for (; $x < $x1; $x++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
                $line = $flip ? $this->yv[$x] : $this->xv[$x];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
                if (empty($ymatches[$line])) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
                    continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                $matches = $ymatches[$line];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                reset($matches);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
                while ($y = current($matches)) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
                    if (empty($this->in_seq[$y])) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
                        $k = $this->_lcsPos($y);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
                        assert($k > 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                        $ymids[$k] = $ymids[$k - 1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
                        break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                    }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
                    next($matches);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
                while ($y = current($matches)) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
                    if ($y > $this->seq[$k - 1]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                        assert($y <= $this->seq[$k]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                        /* Optimization: this is a common case: next match is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                         * just replacing previous match. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
                        $this->in_seq[$this->seq[$k]] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                        $this->seq[$k] = $y;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
                        $this->in_seq[$y] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                    } elseif (empty($this->in_seq[$y])) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
                        $k = $this->_lcsPos($y);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
                        assert($k > 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
                        $ymids[$k] = $ymids[$k - 1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
                    }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
                    next($matches);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        $ymid = $ymids[$this->lcs];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        for ($n = 0; $n < $nchunks - 1; $n++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
            $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
            $y1 = $ymid[$n] + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
            $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        return array($this->lcs, $seps);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    function _lcsPos($ypos)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        $end = $this->lcs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        if ($end == 0 || $ypos > $this->seq[$end]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
            $this->seq[++$this->lcs] = $ypos;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
            $this->in_seq[$ypos] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
            return $this->lcs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        $beg = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        while ($beg < $end) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
            $mid = (int)(($beg + $end) / 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
            if ($ypos > $this->seq[$mid]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
                $beg = $mid + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
            } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
                $end = $mid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        assert($ypos != $this->seq[$end]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        $this->in_seq[$this->seq[$end]] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        $this->seq[$end] = $ypos;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        $this->in_seq[$ypos] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        return $end;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
     * Finds LCS of two sequences.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
     * The results are recorded in the vectors $this->{x,y}changed[], by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
     * storing a 1 in the element for each line that is an insertion or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
     * deletion (ie. is not in the LCS).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
     * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
     * Note that XLIM, YLIM are exclusive bounds.  All line numbers are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
     * origin-0 and discarded lines are not counted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
    function _compareseq ($xoff, $xlim, $yoff, $ylim)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
    {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        /* Slide down the bottom initial diagonal. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
        while ($xoff < $xlim && $yoff < $ylim
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
               && $this->xv[$xoff] == $this->yv[$yoff]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
            ++$xoff;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
            ++$yoff;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
        /* Slide up the top initial diagonal. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
        while ($xlim > $xoff && $ylim > $yoff
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
               && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
            --$xlim;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
            --$ylim;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        if ($xoff == $xlim || $yoff == $ylim) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
            $lcs = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
            /* This is ad hoc but seems to work well.  $nchunks =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
             * sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); $nchunks =
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
             * max(2,min(8,(int)$nchunks)); */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
            $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
            list($lcs, $seps)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
                = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
        if ($lcs == 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
            /* X and Y sequences have no common subsequence: mark all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
             * changed. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
            while ($yoff < $ylim) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
                $this->ychanged[$this->yind[$yoff++]] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
            while ($xoff < $xlim) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
                $this->xchanged[$this->xind[$xoff++]] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
        } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
            /* Use the partitions to split this problem into subproblems. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
            reset($seps);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
            $pt1 = $seps[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
            while ($pt2 = next($seps)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
                $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
                $pt1 = $pt2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
     * Adjusts inserts/deletes of identical lines to join changes as much as
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
     * possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
     * We do something when a run of changed lines include a line at one end
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
     * and has an excluded, identical line at the other.  We are free to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
     * choose which identical line is included.  `compareseq' usually chooses
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
     * the one at the beginning, but usually it is cleaner to consider the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
     * following identical line to be the "change".
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
     * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
    function _shiftBoundaries($lines, &$changed, $other_changed)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
    {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        $i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
        $j = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
        assert(count($lines) == count($changed));
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
        $len = count($lines);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
        $other_len = count($other_changed);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        while (1) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
            /* Scan forward to find the beginning of another run of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
             * changes. Also keep track of the corresponding point in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
             * other file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
             *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
             * Throughout this code, $i and $j are adjusted together so that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
             * the first $i elements of $changed and the first $j elements of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
             * $other_changed both contain the same number of zeros (unchanged
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
             * lines).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
             *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
             * Furthermore, $j is always kept so that $j == $other_len or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
             * $other_changed[$j] == false. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
            while ($j < $other_len && $other_changed[$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
                $j++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
            while ($i < $len && ! $changed[$i]) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
                assert($j < $other_len && ! $other_changed[$j]);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
                $i++; $j++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
                while ($j < $other_len && $other_changed[$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
                    $j++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
            if ($i == $len) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
            $start = $i;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
            /* Find the end of this run of changes. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
            while (++$i < $len && $changed[$i]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
                continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
            do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
                /* Record the length of this run of changes, so that we can
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
                 * later determine whether the run has grown. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
                $runlength = $i - $start;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
                /* Move the changed region back, so long as the previous
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
                 * unchanged line matches the last changed one.  This merges
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
                 * with previous changed regions. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
                while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
                    $changed[--$start] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
                    $changed[--$i] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
                    while ($start > 0 && $changed[$start - 1]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
                        $start--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
                    }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
                    assert($j > 0);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
                    while ($other_changed[--$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
                        continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
                    }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
                    assert($j >= 0 && !$other_changed[$j]);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
                /* Set CORRESPONDING to the end of the changed run, at the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
                 * last point where it corresponds to a changed run in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
                 * other file. CORRESPONDING == LEN means no such point has
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
                 * been found. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
                $corresponding = $j < $other_len ? $i : $len;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
                /* Move the changed region forward, so long as the first
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
                 * changed line matches the following unchanged one.  This
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
                 * merges with following changed regions.  Do this second, so
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
                 * that if there are no merges, the changed region is moved
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
                 * forward as far as possible. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
                while ($i < $len && $lines[$start] == $lines[$i]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
                    $changed[$start++] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
                    $changed[$i++] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
                    while ($i < $len && $changed[$i]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
                        $i++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
                    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
                    assert($j < $other_len && ! $other_changed[$j]);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
                    $j++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
                    if ($j < $other_len && $other_changed[$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
                        $corresponding = $i;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
                        while ($j < $other_len && $other_changed[$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
                            $j++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
                        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
                    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
                }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
            } while ($runlength != $i - $start);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
            /* If possible, move the fully-merged run of changes back to a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
             * corresponding run in the other file. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
            while ($corresponding < $i) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
                $changed[--$start] = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
                $changed[--$i] = 0;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
                assert($j > 0);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
                while ($other_changed[--$j]) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
                    continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
                }
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
                assert($j >= 0 && !$other_changed[$j]);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
}