Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option context not work #40

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
{
"name": "chrisboulton/php-diff",
"type": "library",
"name": "phpspec/php-diff",
"type": "library",
"description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).",
"license": "BSD-3-Clause",

"authors": [
{
"name": "Chris Boulton",
"email": "@chrisboulton"
"homepage": "http://github.com/chrisboulton"
}
],
"autoload": {
"psr-0": {
"Diff": "lib/"
}
}
}

"autoload": {
"psr-0": {
"Diff": "lib/"
}
},

"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
3 changes: 2 additions & 1 deletion lib/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Diff
*
* @param array $a Array containing the lines of the first string to compare.
* @param array $b Array containing the lines for the second string to compare.
* @param array $options
*/
public function __construct($a, $b, $options=array())
{
Expand All @@ -92,7 +93,7 @@ public function __construct($a, $b, $options=array())
/**
* Render a diff using the supplied rendering class and return it.
*
* @param object $renderer An instance of the rendering object to use for generating the diff.
* @param Diff_Renderer_Abstract $renderer An instance of the rendering object to use for generating the diff.
* @return mixed The generated diff. Exact return value depends on the rendered.
*/
public function render(Diff_Renderer_Abstract $renderer)
Expand Down
67 changes: 38 additions & 29 deletions lib/Diff/Renderer/Html/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
* PHP version 5
*
* Copyright (c) 2009 Chris Boulton <[email protected]>
*
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Chris Boulton nor the names of its contributors
* may be used to endorse or promote products derived from this software
* - Neither the name of the Chris Boulton nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package DiffLib
Expand Down Expand Up @@ -82,12 +82,18 @@ public function render()

list($start, $end) = $this->getChangeExtent($fromLine, $toLine);
if($start != 0 || $end != 0) {
$last = $end + strlen($fromLine);
$fromLine = substr_replace($fromLine, "\0", $start, 0);
$fromLine = substr_replace($fromLine, "\1", $last + 1, 0);
$last = $end + strlen($toLine);
$toLine = substr_replace($toLine, "\0", $start, 0);
$toLine = substr_replace($toLine, "\1", $last + 1, 0);
$realEnd = mb_strlen($fromLine) + $end;
$fromLine = mb_substr($fromLine, 0, $start)
. "\0"
. mb_substr($fromLine, $start, $realEnd - $start)
. "\1"
. mb_substr($fromLine, $realEnd);
$realEnd = mb_strlen($toLine) + $end;
$toLine = mb_substr($toLine, 0, $start)
. "\0"
. mb_substr($toLine, $start, $realEnd - $start)
. "\1"
. mb_substr($toLine, $realEnd);
$a[$i1 + $i] = $fromLine;
$b[$j1 + $i] = $toLine;
}
Expand Down Expand Up @@ -149,13 +155,13 @@ public function render()
private function getChangeExtent($fromLine, $toLine)
{
$start = 0;
$limit = min(strlen($fromLine), strlen($toLine));
while($start < $limit && $fromLine{$start} == $toLine{$start}) {
$limit = min(mb_strlen($fromLine), mb_strlen($toLine));
while($start < $limit && mb_substr($fromLine, $start, 1) == mb_substr($toLine, $start, 1)) {
++$start;
}
$end = -1;
$limit = $limit - $start;
while(-$end <= $limit && substr($fromLine, $end, 1) == substr($toLine, $end, 1)) {
while(-$end <= $limit && mb_substr($fromLine, $end, 1) == mb_substr($toLine, $end, 1)) {
--$end;
}
return array(
Expand All @@ -174,22 +180,25 @@ private function getChangeExtent($fromLine, $toLine)
*/
private function formatLines($lines)
{
$lines = array_map(array($this, 'ExpandTabs'), $lines);
if ($this->options['tabSize'] !== false) {
$lines = array_map(array($this, 'ExpandTabs'), $lines);
}
$lines = array_map(array($this, 'HtmlSafe'), $lines);
foreach($lines as &$line) {
$line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line);
$line = preg_replace_callback('# ( +)|^ #', __CLASS__."::fixSpaces", $line);
}
return $lines;
}

/**
* Replace a string containing spaces with a HTML representation using &nbsp;.
*
* @param string $spaces The string of spaces.
* @param string $matches Regex matches array.
* @return string The HTML representation of the string.
*/
function fixSpaces($spaces='')
public static function fixSpaces($matches)
{
$spaces = isset($matches[1]) ? $matches[1] : '';
$count = strlen($spaces);
if($count == 0) {
return '';
Expand Down Expand Up @@ -221,4 +230,4 @@ private function htmlSafe($string)
{
return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
}
}
}
2 changes: 1 addition & 1 deletion lib/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public function render()
foreach($change['changed']['lines'] as $no => $line) {
$toLine = $change['changed']['offset'] + $no + 1;
$html .= '<tr>';
$html .= '<th>'.$toLine.'</th>';
$html .= '<th>&nbsp;</th>';
$html .= '<th>'.$toLine.'</th>';
$html .= '<td class="Right"><span>'.$line.'</span></td>';
$html .= '</tr>';
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public function render()
$toLine = $change['changed']['offset'] + $no + 1;
$html .= '<tr>';
$html .= '<th>'.$fromLine.'</th>';
$html .= '<td class="Left"><span>'.$line.'</span>&nbsp;</span></td>';
$html .= '<td class="Left"><span>'.$line.'</span>&nbsp;</td>';
$html .= '<th>'.$toLine.'</th>';
$html .= '<td class="Right"><span>'.$line.'</span>&nbsp;</span></td>';
$html .= '<td class="Right"><span>'.$line.'</span>&nbsp;</td>';
$html .= '</tr>';
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Diff/Renderer/Text/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public function render()
$j2 = $group[$lastItem][4];

if($i2 - $i1 >= 2) {
$diff .= '*** '.($group[0][1] + 1).','.$i2." ****\n";
$diff .= '*** '.($group[0][1] + 1).','.$i2." ****".PHP_EOL;
}
else {
$diff .= '*** '.$i2." ****\n";
}

if($j2 - $j1 >= 2) {
$separator = '--- '.($j1 + 1).','.$j2." ----\n";
$separator = '--- '.($j1 + 1).','.$j2." ----".PHP_EOL;
}
else {
$separator = '--- '.$j2." ----\n";
$separator = '--- '.$j2." ----".PHP_EOL;
}

$hasVisible = false;
Expand All @@ -99,7 +99,7 @@ public function render()
if($tag == 'insert') {
continue;
}
$diff .= $this->tagMap[$tag].' '.implode("\n".$this->tagMap[$tag].' ', $this->diff->GetA($i1, $i2))."\n";
$diff .= $this->tagMap[$tag].' '.implode(PHP_EOL.$this->tagMap[$tag].' ', $this->diff->GetA($i1, $i2)).PHP_EOL;
}
}

Expand All @@ -119,7 +119,7 @@ public function render()
if($tag == 'delete') {
continue;
}
$diff .= $this->tagMap[$tag].' '.implode("\n".$this->tagMap[$tag].' ', $this->diff->GetB($j1, $j2))."\n";
$diff .= $this->tagMap[$tag].' '.implode(PHP_EOL.$this->tagMap[$tag].' ', $this->diff->GetB($j1, $j2)).PHP_EOL;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Diff/Renderer/Text/Unified.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ public function render()
$i2 = -1;
}

$diff .= '@@ -'.($i1 + 1).','.($i2 - $i1).' +'.($j1 + 1).','.($j2 - $j1)." @@\n";
$diff .= '@@ -'.($i1 + 1).','.($i2 - $i1).' +'.($j1 + 1).','.($j2 - $j1)." @@".PHP_EOL;
foreach($group as $code) {
list($tag, $i1, $i2, $j1, $j2) = $code;
if($tag == 'equal') {
$diff .= ' '.implode("\n ", $this->diff->GetA($i1, $i2))."\n";
$diff .= ' '.implode(PHP_EOL." ", $this->diff->GetA($i1, $i2)).PHP_EOL;
}
else {
if($tag == 'replace' || $tag == 'delete') {
$diff .= '-'.implode("\n-", $this->diff->GetA($i1, $i2))."\n";
$diff .= '-'.implode(PHP_EOL."-", $this->diff->GetA($i1, $i2)).PHP_EOL;
}

if($tag == 'replace' || $tag == 'insert') {
$diff .= '+'.implode("\n+", $this->diff->GetB($j1, $j2))."\n";
$diff .= '+'.implode(PHP_EOL."+", $this->diff->GetB($j1, $j2)).PHP_EOL;
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions lib/Diff/SequenceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class Diff_SequenceMatcher

private $options = array();

private $matchingBlocks = null;
private $opCodes = null;
private $fullBCount = null;

private $defaultOptions = array(
'ignoreNewLines' => false,
'ignoreWhitespace' => false,
Expand All @@ -83,8 +87,9 @@ class Diff_SequenceMatcher
* @param string|array $a A string or array containing the lines to compare against.
* @param string|array $b A string or array containing the lines to compare.
* @param string|array $junkCallback Either an array or string that references a callback function (if there is one) to determine 'junk' characters.
* @param array $options
*/
public function __construct($a, $b, $junkCallback=null, $options)
public function __construct($a, $b, $junkCallback=null, $options=[])
{
$this->a = null;
$this->b = null;
Expand All @@ -93,6 +98,11 @@ public function __construct($a, $b, $junkCallback=null, $options)
$this->setSequences($a, $b);
}

/**
* Set new options
*
* @param array $options
*/
public function setOptions($options)
{
$this->options = array_merge($this->defaultOptions, $options);
Expand Down Expand Up @@ -206,12 +216,12 @@ private function chainB()
/**
* Checks if a particular character is in the junk dictionary
* for the list of junk characters.
*
* @return boolean $b True if the character is considered junk. False if not.
* @param $b
* @return boolean True if the character is considered junk. False if not.
*/
private function isBJunk($b)
{
if(isset($this->juncDict[$b])) {
if(isset($this->junkDict[$b])) {
return true;
}

Expand Down Expand Up @@ -252,7 +262,7 @@ public function findLongestMatch($alo, $ahi, $blo, $bhi)
for($i = $alo; $i < $ahi; ++$i) {
$newJ2Len = array();
$jDict = $this->arrayGetDefault($this->b2j, $a[$i], $nothing);
foreach($jDict as $jKey => $j) {
foreach($jDict as $j) {
if($j < $blo) {
continue;
}
Expand Down Expand Up @@ -285,7 +295,7 @@ public function findLongestMatch($alo, $ahi, $blo, $bhi)
}

while($bestI > $alo && $bestJ > $blo && $this->isBJunk($b[$bestJ - 1]) &&
!$this->isLineDifferent($bestI - 1, $bestJ - 1)) {
!$this->linesAreDifferent($bestI - 1, $bestJ - 1)) {
--$bestI;
--$bestJ;
++$bestSize;
Expand Down Expand Up @@ -631,7 +641,7 @@ private function quickRatio()
{
if($this->fullBCount === null) {
$this->fullBCount = array();
$bLength = count ($b);
$bLength = count ($this->b);
for($i = 0; $i < $bLength; ++$i) {
$char = $this->b[$i];
$this->fullBCount[$char] = $this->arrayGetDefault($this->fullBCount, $char, 0) + 1;
Expand Down Expand Up @@ -729,7 +739,7 @@ private function tupleSort($a, $b)
}
}

if(count($a) == $count($b)) {
if(count($a) == count($b)) {
return 0;
}
else if(count($a) < count($b)) {
Expand All @@ -739,4 +749,4 @@ private function tupleSort($a, $b)
return 1;
}
}
}
}