예제 #1
0
 public function get_source(&$wiki, $index = -1)
 {
     $lines = $this->m_lines;
     if ($index == -1) {
         $index = count($lines) - 1;
     }
     if (!(0 <= $index && $index < count($lines))) {
         return false;
     }
     $diffs = array();
     $wiki = null;
     for ($i = $index; $i >= 0; $i--) {
         if ($this->m_sources[$i] !== null) {
             $wiki = $this->m_sources[$i];
             break;
         }
         $f = $this->get_fields($i);
         if (substr($f[2], 0, 1) !== "!") {
             $wiki = urldecode($f[2]);
             $this->m_sources[$i] = $wiki;
             break;
         }
         array_push($diffs, urldecode(substr($f[2], 1)));
     }
     $ndiff = $index - $i;
     if ($i < 0) {
         $this->perr("{$this->fname_hist}: there is no base entry for the diff!");
         return false;
     } else {
         if ($ndiff > 0) {
             // 差分適用
             $ndiff = $index - $i;
             $wiki = ldiff_split_lines($wiki);
             while (++$i <= $index) {
                 $edits = preg_split('/\\n/u', $diffs[$index - $i]);
                 $wiki = $this->_apply_edits($wiki, $edits);
                 if ($wiki === false) {
                     return false;
                 }
                 $this->m_sources[$i] = implode("\n", $wiki);
             }
             $wiki = $this->m_sources[$index];
         }
     }
     return $ndiff;
 }
예제 #2
0
function ldiff_lines_sed($text1, $text2)
{
    $arr1 = ldiff_split_lines($text1);
    $arr2 = ldiff_split_lines($text2);
    $M = $text1 == '' ? 0 : count($arr1);
    $N = $text2 == '' ? 0 : count($arr2);
    $path = _ldiff_getpath_wu($arr1, $M, $arr2, $N);
    array_push($path, array($M, $N));
    // 終端
    $x = 0;
    $y = 0;
    $ret = '';
    foreach ($path as $p) {
        // 削除
        $x0 = $p[0];
        if ($x < $x0) {
            $a1 = $x + 1;
            $a2 = $x0;
            if ($a1 == $a2) {
                $ret .= $a1 . 'd' . PHP_EOL;
            } else {
                $ret .= $a1 . ',' . $a2 . 'd' . PHP_EOL;
            }
            $x = $x0;
        }
        // 挿入
        $y0 = $p[1];
        if ($y < $y0) {
            $a1 = $x0 + 1;
            for (; $y < $y0; $y++) {
                $ret .= $a1 . 'i ' . $arr2[$y] . PHP_EOL;
            }
        }
        // 共通部分は省略
        for (; $x < $M && $y < $N && $arr1[$x] == $arr2[$y]; $x++, $y++) {
        }
    }
    return $ret;
}