From b7ffdea02b2a73c68580e9be9a1d748a2eac4a30 Mon Sep 17 00:00:00 2001 From: Dmitry Pischenko Date: Fri, 6 Jul 2018 11:38:30 +0300 Subject: [PATCH 1/2] removed uneeded /span --- lib/Diff/Renderer/Html/SideBySide.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Diff/Renderer/Html/SideBySide.php b/lib/Diff/Renderer/Html/SideBySide.php index 307af1c3..eb438483 100644 --- a/lib/Diff/Renderer/Html/SideBySide.php +++ b/lib/Diff/Renderer/Html/SideBySide.php @@ -83,9 +83,9 @@ public function render() $toLine = $change['changed']['offset'] + $no + 1; $html .= ''; $html .= ''.$fromLine.''; - $html .= ''.$line.' '; + $html .= ''.$line.' '; $html .= ''.$toLine.''; - $html .= ''.$line.' '; + $html .= ''.$line.' '; $html .= ''; } } From deec9a69867d3d254e06ef95f71de4d23cde88d2 Mon Sep 17 00:00:00 2001 From: Dmitry Pischenko Date: Fri, 6 Jul 2018 11:38:40 +0300 Subject: [PATCH 2/2] utf-8 lines support --- lib/Diff/Renderer/Html/Array.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/Diff/Renderer/Html/Array.php b/lib/Diff/Renderer/Html/Array.php index 521601c6..4c1c45ac 100644 --- a/lib/Diff/Renderer/Html/Array.php +++ b/lib/Diff/Renderer/Html/Array.php @@ -42,6 +42,13 @@ require_once dirname(__FILE__).'/../Abstract.php'; +function substr_replace_utf8($string, $replacement, $position_needle, $length_needle) +{ + $str1 = mb_substr ($string, 0, $position_needle, 'UTF-8'); + $str2 = mb_substr ($string, $position_needle + $length_needle, null, 'UTF-8'); + return $str1 . $replacement . $str2; +} + class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract { /** @@ -83,11 +90,11 @@ 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); + $fromLine = substr_replace_utf8($fromLine, "\0", $start, 0); + $fromLine = substr_replace_utf8($fromLine, "\1", $last + 1, 0); $last = $end + strlen($toLine); - $toLine = substr_replace($toLine, "\0", $start, 0); - $toLine = substr_replace($toLine, "\1", $last + 1, 0); + $toLine = substr_replace_utf8($toLine, "\0", $start, 0); + $toLine = substr_replace_utf8($toLine, "\1", $last + 1, 0); $a[$i1 + $i] = $fromLine; $b[$j1 + $i] = $toLine; } @@ -149,13 +156,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, 'UTF-8'), mb_strlen($toLine, 'UTF-8')); + while($start < $limit && mb_substr($fromLine, $start, 1, 'UTF-8') == mb_substr($toLine, $start, 1, 'UTF-8')) { ++$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, 'UTF-8') == mb_substr($toLine, $end, 1, 'UTF-8')) { --$end; } return array(