Skip to content

Commit 690419d

Browse files
author
Chris Boulton
committed
Add in working ignoreWhitespace and ignoreCase options (self-describing), fix up an issue where a diff of two files exactly the same would show the last $context lines, general cleanup
1 parent 5c5c179 commit 690419d

File tree

10 files changed

+117
-74
lines changed

10 files changed

+117
-74
lines changed

README

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ More complete documentation will be available shortly.
2424

2525
Todo
2626
----
27-
* Ability to ignore blank line changes, case changes and spacing changes
27+
* Ability to ignore blank line changes
2828
* 3 way diff support
2929
* Performance optimizations
3030

example/example.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
// Options for generating the diff
2222
$options = array(
23+
//'ignoreWhitespace' => true,
24+
//'ignoreCase' => true,
2325
);
2426

2527
// Initialize the diff class

lib/Diff.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author Chris Boulton <[email protected]>
4040
* @copyright (c) 2009 Chris Boulton
4141
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42-
* @version 1.0
42+
* @version 1.1
4343
* @link http://github.com/chrisboulton/phpdiff
4444
*/
4545

@@ -98,7 +98,7 @@ public function __construct($a, $b, $options=array())
9898
public function render(Diff_Renderer_Abstract $renderer)
9999
{
100100
$renderer->diff = $this;
101-
return $renderer->Render();
101+
return $renderer->render();
102102
}
103103

104104
/**
@@ -169,7 +169,7 @@ public function getGroupedOpcodes()
169169
}
170170

171171
require_once dirname(__FILE__).'/Diff/SequenceMatcher.php';
172-
$sequenceMatcher = new Diff_SequenceMatcher($this->a, $this->b);
172+
$sequenceMatcher = new Diff_SequenceMatcher($this->a, $this->b, null, $this->options);
173173
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes();
174174
return $this->groupedCodes;
175175
}

lib/Diff/Renderer/Abstract.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -63,9 +63,9 @@ abstract class Diff_Renderer_Abstract
6363
*
6464
* @param array $options Optionally, an array of the options for the renderer.
6565
*/
66-
public function __construct($options=array())
66+
public function __construct(array $options = array())
6767
{
68-
$this->SetOptions($options);
68+
$this->setOptions($options);
6969
}
7070

7171
/**
@@ -75,7 +75,7 @@ public function __construct($options=array())
7575
*
7676
* @param array $options Array of options to set.
7777
*/
78-
public function SetOptions($options)
78+
public function setOptions(array $options)
7979
{
8080
$this->options = array_merge($this->defaultOptions, $options);
8181
}

lib/Diff/Renderer/Html/Array.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -58,13 +58,13 @@ class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract
5858
*
5959
* @return array An array of the generated chances, suitable for presentation in HTML.
6060
*/
61-
public function Render()
61+
public function render()
6262
{
6363
// As we'll be modifying a & b to include our change markers,
6464
// we need to get the contents and store them here. That way
6565
// we're not going to destroy the original data
66-
$a = $this->diff->GetA();
67-
$b = $this->diff->GetB();
66+
$a = $this->diff->getA();
67+
$b = $this->diff->getB();
6868

6969
$changes = array();
7070
$opCodes = $this->diff->getGroupedOpcodes();
@@ -80,21 +80,20 @@ public function Render()
8080
$fromLine = $a[$i1 + $i];
8181
$toLine = $b[$j1 + $i];
8282

83-
list($start, $end) = $this->GetChangeExtent($fromLine, $toLine);
83+
list($start, $end) = $this->getChangeExtent($fromLine, $toLine);
8484
if($start != 0 || $end != 0) {
8585
$last = $end + strlen($fromLine);
8686
$fromLine = substr_replace($fromLine, "\0", $start, 0);
8787
$fromLine = substr_replace($fromLine, "\1", $last + 1, 0);
8888
$last = $end + strlen($toLine);
8989
$toLine = substr_replace($toLine, "\0", $start, 0);
9090
$toLine = substr_replace($toLine, "\1", $last + 1, 0);
91-
$a[$i1] = $fromLine;
92-
$b[$j1] = $toLine;
91+
$a[$i1 + $i] = $fromLine;
92+
$b[$j1 + $i] = $toLine;
9393
}
9494
}
9595
}
9696

97-
9897
if($tag != $lastTag) {
9998
$blocks[] = array(
10099
'tag' => $tag,
@@ -114,21 +113,21 @@ public function Render()
114113

115114
if($tag == 'equal') {
116115
$lines = array_slice($a, $i1, ($i2 - $i1));
117-
$blocks[$lastBlock]['base']['lines'] += $this->FormatLines($lines);
116+
$blocks[$lastBlock]['base']['lines'] += $this->formatLines($lines);
118117
$lines = array_slice($b, $j1, ($j2 - $j1));
119-
$blocks[$lastBlock]['changed']['lines'] += $this->FormatLines($lines);
118+
$blocks[$lastBlock]['changed']['lines'] += $this->formatLines($lines);
120119
}
121120
else {
122121
if($tag == 'replace' || $tag == 'delete') {
123122
$lines = array_slice($a, $i1, ($i2 - $i1));
124-
$lines = $this->FormatLines($lines);
123+
$lines = $this->formatLines($lines);
125124
$lines = str_replace(array("\0", "\1"), array('<del>', '</del>'), $lines);
126125
$blocks[$lastBlock]['base']['lines'] += $lines;
127126
}
128127

129128
if($tag == 'replace' || $tag == 'insert') {
130129
$lines = array_slice($b, $j1, ($j2 - $j1));
131-
$lines = $this->FormatLines($lines);
130+
$lines = $this->formatLines($lines);
132131
$lines = str_replace(array("\0", "\1"), array('<ins>', '</ins>'), $lines);
133132
$blocks[$lastBlock]['changed']['lines'] += $lines;
134133
}
@@ -173,12 +172,12 @@ private function GetChangeExtent($fromLine, $toLine)
173172
* @param array $lines Array of lines to format.
174173
* @return array Array of the formatted lines.
175174
*/
176-
private function FormatLines($lines)
175+
private function formatLines($lines)
177176
{
178177
$lines = array_map(array($this, 'ExpandTabs'), $lines);
179178
$lines = array_map(array($this, 'HtmlSafe'), $lines);
180179
foreach($lines as &$line) {
181-
$line = preg_replace('# ( +)|^ #e', "\$this->FixSpaces('\\1')", $line);
180+
$line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line);
182181
}
183182
return $lines;
184183
}
@@ -189,7 +188,7 @@ private function FormatLines($lines)
189188
* @param string $spaces The string of spaces.
190189
* @return string The HTML representation of the string.
191190
*/
192-
function FixSpaces($spaces='')
191+
function fixSpaces($spaces='')
193192
{
194193
$count = strlen($spaces);
195194
if($count == 0) {
@@ -207,7 +206,7 @@ function FixSpaces($spaces='')
207206
* @param string $line The containing tabs to convert.
208207
* @return string The line with the tabs converted to spaces.
209208
*/
210-
private function ExpandTabs($line)
209+
private function expandTabs($line)
211210
{
212211
return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
213212
}
@@ -218,7 +217,7 @@ private function ExpandTabs($line)
218217
* @param string $string The string.
219218
* @return string The string with the HTML characters replaced by entities.
220219
*/
221-
private function HtmlSafe($string)
220+
private function htmlSafe($string)
222221
{
223222
return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
224223
}

lib/Diff/Renderer/Html/Inline.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -50,9 +50,9 @@ class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array
5050
*
5151
* @return string The generated inline diff.
5252
*/
53-
public function Render()
53+
public function render()
5454
{
55-
$changes = parent::Render();
55+
$changes = parent::render();
5656
$html = '';
5757
if(empty($changes)) {
5858
return $html;

lib/Diff/Renderer/Html/SideBySide.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -50,9 +50,9 @@ class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array
5050
*
5151
* @return string The generated side by side diff.
5252
*/
53-
public function Render()
53+
public function render()
5454
{
55-
$changes = parent::Render();
55+
$changes = parent::render();
5656

5757
$html = '';
5858
if(empty($changes)) {

lib/Diff/Renderer/Text/Context.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -59,7 +59,7 @@ class Diff_Renderer_Text_Context extends Diff_Renderer_Abstract
5959
*
6060
* @return string The generated context diff.
6161
*/
62-
public function Render()
62+
public function render()
6363
{
6464
$diff = '';
6565
$opCodes = $this->diff->getGroupedOpcodes();

lib/Diff/Renderer/Text/Unified.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Chris Boulton <[email protected]>
3737
* @copyright (c) 2009 Chris Boulton
3838
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
39-
* @version 1.0
39+
* @version 1.1
4040
* @link http://github.com/chrisboulton/phpdiff
4141
*/
4242

@@ -49,7 +49,7 @@ class Diff_Renderer_Text_Unified extends Diff_Renderer_Abstract
4949
*
5050
* @return string The unified diff.
5151
*/
52-
public function Render()
52+
public function render()
5353
{
5454
$diff = '';
5555
$opCodes = $this->diff->getGroupedOpcodes();

0 commit comments

Comments
 (0)