Development

Changeset 18235

You must first sign up to be able to contribute.

Changeset 18235

Show
Ignore:
Timestamp:
05/14/09 00:15:08 (4 years ago)
Author:
boutell
Message:

pkString::diff() now does a smarter job with the all-important first removed and added lines

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/pkToolkitPlugin/trunk/lib/pkString.class.php

    r18113 r18235  
    169169    $array1 = array_map('trim', explode("\n", wordwrap($text1, 70))); 
    170170    $array2 = array_map('trim', explode("\n", wordwrap($text2, 70))); 
    171     $onlyin1 = array_diff($array1, $array2); 
    172     $onlyin2 = array_diff($array2, $array1); 
     171    $onlyin1 = array_values(array_diff($array1, $array2)); 
     172    $onlyin2 = array_values(array_diff($array2, $array1)); 
     173    if (count($onlyin1) && count($onlyin2)) 
     174    { 
     175      // The first line is critical because history displays 
     176      // so little of a diff. So remove any shared prefix from the 
     177      // first deleted and first added lines unless that means we'd 
     178      // take it all 
     179      $s1 = $onlyin1[0]; 
     180      $s2 = $onlyin2[0]; 
     181      if (strlen($s1) !== strlen($s2)) 
     182      { 
     183        $min = min(strlen($s1), strlen($s2)); 
     184        sfContext::getInstance()->getLogger()->info("min is $min fromdiff"); 
     185        for ($i = 0; ($i < $min); $i++) 
     186        { 
     187          $c1 = substr($s1, $i, 1); 
     188          $c2 = substr($s2, $i, 1); 
     189          if ($c1 !== $c2) 
     190          { 
     191            sfContext::getInstance()->getLogger()->info("difference at $i fromdiff $c1 vs $c2"); 
     192            break; 
     193          } 
     194        } 
     195        sfContext::getInstance()->getLogger()->info("i is $i fromdiff"); 
     196        $onlyin1[0] = substr($s1, $i); 
     197        $onlyin2[0] = substr($s2, $i); 
     198        if (!strlen($onlyin1[0])) 
     199        { 
     200          array_shift($onlyin1); 
     201        } 
     202        if (!strlen($onlyin2[0])) 
     203        { 
     204          array_shift($onlyin2); 
     205        } 
     206      } 
     207    } 
     208    sfContext::getInstance()->getLogger()->info('returning fromdiff'); 
    173209    return array("onlyin1" => array_values($onlyin1), "onlyin2" => array_values($onlyin2)); 
    174210  }