Skip to content

Fix tab expansion and deprecated preg_replace use on fixSpaces. #32

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
33 changes: 23 additions & 10 deletions lib/Diff/Renderer/Html/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ protected function formatLines($lines)
$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 = str_replace(' ', ' ', $line);
// $line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
}
return $lines;
}
Expand All @@ -188,16 +189,19 @@ protected function formatLines($lines)
* @param string $spaces The string of spaces.
* @return string The HTML representation of the string.
*/
function fixSpaces($spaces='')
function fixSpaces($matches)
{
$count = strlen($spaces);
if($count == 0) {
return '';
$buffer = '';
foreach($matches as $spaces){
$count = strlen($spaces);
if($count == 0) {
continue;
}
$div = floor($count / 2);
$mod = $count % 2;
$buffer .= str_repeat('  ', $div).str_repeat(' ', $mod);
}

$div = floor($count / 2);
$mod = $count % 2;
return str_repeat('  ', $div).str_repeat(' ', $mod);
return $buffer;
}

/**
Expand All @@ -208,7 +212,16 @@ function fixSpaces($spaces='')
*/
private function expandTabs($line)
{
return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
$tabSize = $this->options['tabSize'];
while(($pos = strpos($line, "\t")) !== FALSE){
$left = substr($line, 0, $pos);
$right = substr($line, $pos + 1);
$length = $tabSize - ($pos % $tabSize);
$spaces = str_repeat(' ', $length);
$line = $left.$spaces.$right;
}
return $line;
// return str_replace("\t", str_repeat(' ', $this->options['tabSize']), $line);
}

/**
Expand Down