From f0aba03dc4b038e8d96c47e8009ab3c106ac5184 Mon Sep 17 00:00:00 2001 From: kriss0r Date: Fri, 24 Jul 2015 21:42:39 +0200 Subject: [PATCH] Fix tab expansion and deprecated preg_replace use on fixSpaces. --- lib/Diff/Renderer/Html/Array.php | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/Diff/Renderer/Html/Array.php b/lib/Diff/Renderer/Html/Array.php index b012fb6b..542b63d4 100644 --- a/lib/Diff/Renderer/Html/Array.php +++ b/lib/Diff/Renderer/Html/Array.php @@ -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; } @@ -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; } /** @@ -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); } /**