Skip to content

Commit 0ea259b

Browse files
committed
Merge branch 'utf-8-fix'
2 parents d3dcb28 + 19a92b3 commit 0ea259b

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

example/a.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
88

99
<h2>A heading we'll be removing</h2>
10-
10+
<pre>
11+
构建具有中国特色的医学人才培养体系
12+
</pre>
1113
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1214
</body>
13-
</html>
15+
</html>

example/b.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<html>
23
<head>
34
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
@@ -6,7 +7,9 @@
67
<body>
78
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
89

9-
10+
<pre>
11+
构建具有中国的医学人才培养体系
12+
</pre>
1013
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
1114

1215
<p>Just a small amount of new text...</p>

example/example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
22
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
33
<html>
44
<head>
55
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
6-
<title>PHP LibDiff - Examples</title>
6+
<title>PHP LibDiff - Examples 构建具有中国特色的医学人才培养体系</title>
77
<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8"/>
88
</head>
99
<body>

lib/Diff/Renderer/Html/Array.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,50 @@ class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract
5151
'tabSize' => 4
5252
);
5353

54+
/**
55+
* From https://gist.github.com/stemar/8287074
56+
* @param mixed $string The input string.
57+
* @param mixed $replacement The replacement string.
58+
* @param mixed $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.
59+
* @param mixed $length If given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, it represents the number of characters from the end of string at which to stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. Of course, if length is zero then this function will have the effect of inserting replacement into string at the given start offset.
60+
* @return string The result string is returned. If string is an array then array is returned.
61+
*/
62+
public function mb_substr_replace($string, $replacement, $start, $length=NULL) {
63+
if (is_array($string)) {
64+
$num = count($string);
65+
// $replacement
66+
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
67+
// $start
68+
if (is_array($start)) {
69+
$start = array_slice($start, 0, $num);
70+
foreach ($start as $key => $value)
71+
$start[$key] = is_int($value) ? $value : 0;
72+
}
73+
else {
74+
$start = array_pad(array($start), $num, $start);
75+
}
76+
// $length
77+
if (!isset($length)) {
78+
$length = array_fill(0, $num, 0);
79+
}
80+
elseif (is_array($length)) {
81+
$length = array_slice($length, 0, $num);
82+
foreach ($length as $key => $value)
83+
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
84+
}
85+
else {
86+
$length = array_pad(array($length), $num, $length);
87+
}
88+
// Recursive call
89+
return array_map(__FUNCTION__, $string, $replacement, $start, $length);
90+
}
91+
preg_match_all('/./us', (string)$string, $smatches);
92+
preg_match_all('/./us', (string)$replacement, $rmatches);
93+
if ($length === NULL) $length = mb_strlen($string);
94+
array_splice($smatches[0], $start, $length, $rmatches[0]);
95+
return join($smatches[0]);
96+
}
97+
5498
/**
5599
* Render and return an array structure suitable for generating HTML
56100
* based differences. Generally called by subclasses that generate a
@@ -83,11 +127,11 @@ public function render()
83127
list($start, $end) = $this->getChangeExtent($fromLine, $toLine);
84128
if($start != 0 || $end != 0) {
85129
$last = $end + strlen($fromLine);
86-
$fromLine = substr_replace($fromLine, "\0", $start, 0);
87-
$fromLine = substr_replace($fromLine, "\1", $last + 1, 0);
130+
$fromLine = $this->mb_substr_replace($fromLine, "\0", $start, 0);
131+
$fromLine = $this->mb_substr_replace($fromLine, "\1", $last + 1, 0);
88132
$last = $end + strlen($toLine);
89-
$toLine = substr_replace($toLine, "\0", $start, 0);
90-
$toLine = substr_replace($toLine, "\1", $last + 1, 0);
133+
$toLine = $this->mb_substr_replace($toLine, "\0", $start, 0);
134+
$toLine = $this->mb_substr_replace($toLine, "\1", $last + 1, 0);
91135
$a[$i1 + $i] = $fromLine;
92136
$b[$j1 + $i] = $toLine;
93137
}

0 commit comments

Comments
 (0)