Skip to content

Commit f0aba03

Browse files
committed
Fix tab expansion and deprecated preg_replace use on fixSpaces.
1 parent f4db229 commit f0aba03

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lib/Diff/Renderer/Html/Array.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ protected function formatLines($lines)
177177
$lines = array_map(array($this, 'ExpandTabs'), $lines);
178178
$lines = array_map(array($this, 'HtmlSafe'), $lines);
179179
foreach($lines as &$line) {
180-
$line = preg_replace('# ( +)|^ #e', "\$this->fixSpaces('\\1')", $line);
180+
$line = str_replace(' ', ' ', $line);
181+
// $line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
181182
}
182183
return $lines;
183184
}
@@ -188,16 +189,19 @@ protected function formatLines($lines)
188189
* @param string $spaces The string of spaces.
189190
* @return string The HTML representation of the string.
190191
*/
191-
function fixSpaces($spaces='')
192+
function fixSpaces($matches)
192193
{
193-
$count = strlen($spaces);
194-
if($count == 0) {
195-
return '';
194+
$buffer = '';
195+
foreach($matches as $spaces){
196+
$count = strlen($spaces);
197+
if($count == 0) {
198+
continue;
199+
}
200+
$div = floor($count / 2);
201+
$mod = $count % 2;
202+
$buffer .= str_repeat('  ', $div).str_repeat(' ', $mod);
196203
}
197-
198-
$div = floor($count / 2);
199-
$mod = $count % 2;
200-
return str_repeat('  ', $div).str_repeat(' ', $mod);
204+
return $buffer;
201205
}
202206

203207
/**
@@ -208,7 +212,16 @@ function fixSpaces($spaces='')
208212
*/
209213
private function expandTabs($line)
210214
{
211-
return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
215+
$tabSize = $this->options['tabSize'];
216+
while(($pos = strpos($line, "\t")) !== FALSE){
217+
$left = substr($line, 0, $pos);
218+
$right = substr($line, $pos + 1);
219+
$length = $tabSize - ($pos % $tabSize);
220+
$spaces = str_repeat(' ', $length);
221+
$line = $left.$spaces.$right;
222+
}
223+
return $line;
224+
// return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
212225
}
213226

214227
/**

0 commit comments

Comments
 (0)