patch_reader.php
Gesponsorde koppelingen
PHP script bestanden
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
$string = "*** a.txt 2021-01-03 21:04:05.547897800 +0100
--- b.txt 2021-01-03 21:04:05.563893700 +0100
***************
*** 1,4 ****
Huis
! Tuin
Keukendeur
! Bloemetjesgordijn
\ No newline at end of file
--- 1,6 ----
Huis
! Tuintje
Keukendeur
! Raamkozijn
! Bloemetjesgordijn
! Stofzuiger
\ No newline at end of file
";
function diff_styled($diff) {
$diff_pattern = "/\*{3}\s([\d,]+)\s\*{4}\s?(.*)-{3}\s([\d,]+)\s-{4}\s?(.*)$/s";
$line_pattern = "/^[\r\n]?([ \-+!])\s(.*)$/m";
$result = '';
$diff = preg_split("/\*{15}/", htmlentities($diff));
for ($block_nr=1; $block_nr<count($diff); $block_nr++)
{
if (!preg_match($diff_pattern, $diff[$block_nr], $block))
break;
preg_match_all($line_pattern, $block[2], $new, PREG_SET_ORDER);
preg_match_all($line_pattern, $block[4], $old, PREG_SET_ORDER);
$result .= "<pre class=\"diff\"><address>" . diff_lines($block[3], $block[1]) . ":</address>";
$old_line = $new_line = 0;
while ($old_line<count($old) || $new_line<count($new))
{
$match = ($old[$old_line] ? $old[$old_line][1] : " ") . ($new[$new_line] ? $new[$new_line][1] : " ");
switch ($match)
{
case " ":
case " ":
$result .= ($old[$old_line] ? $old[$old_line][2] : $new[$new_line][2]);
$old_line++; $new_line++; break;
case "+ ":
case "! ":
$result .= "<del>" . $old[$old_line][2] . "</del>";
$old_line++; break;
case " -":
case " !":
$result .= "<ins>" . $new[$new_line][2] . "</ins>";
$new_line++; break;
case "!!":
$result .= "<del>" . $old[$old_line][2] . "</del>";
$result .= "<ins>" . $new[$new_line][2] . "</ins>";
$old_line++; $new_line++; break;
default:
$result .= "Error: Unknown '" . $match . "'\n";
$result .= $old[$old_line][2];
$result .= $new[$new_line][2];
$old_line++; $new_line++;
}
}
$result .= "";
}
return $result;
}
function diff_lines($lines_old, $lines_new) {
$lines_old = explode(",", $lines_old);
$lines_new = explode(",", $lines_new);
$lines_start = min($lines_old[0], $lines_new[0]);
$lines_stop = max($lines_old[1], $lines_new[1]);
$lines_count = ($lines_new[1] - $lines_new[0]) - ($lines_old[1] - $lines_old[0]);
#print_r($lines_old);
#print_r($lines_new);
if (!$lines_stop)
$result = sprintf("Regel %u", $lines_start);
else
$result = sprintf("Regels %u t/m %u", $lines_start, $lines_stop);
if ($lines_count)
if ($lines_count == 1 || $lines_count == -1)
$result .= sprintf(" (%+d regel)", $lines_count);
else
$result .= sprintf(" (%+d regels)", $lines_count);
return $result;
}
echo diff_styled($string);
?>
$string = "*** a.txt 2021-01-03 21:04:05.547897800 +0100
--- b.txt 2021-01-03 21:04:05.563893700 +0100
***************
*** 1,4 ****
Huis
! Tuin
Keukendeur
! Bloemetjesgordijn
\ No newline at end of file
--- 1,6 ----
Huis
! Tuintje
Keukendeur
! Raamkozijn
! Bloemetjesgordijn
! Stofzuiger
\ No newline at end of file
";
function diff_styled($diff) {
$diff_pattern = "/\*{3}\s([\d,]+)\s\*{4}\s?(.*)-{3}\s([\d,]+)\s-{4}\s?(.*)$/s";
$line_pattern = "/^[\r\n]?([ \-+!])\s(.*)$/m";
$result = '';
$diff = preg_split("/\*{15}/", htmlentities($diff));
for ($block_nr=1; $block_nr<count($diff); $block_nr++)
{
if (!preg_match($diff_pattern, $diff[$block_nr], $block))
break;
preg_match_all($line_pattern, $block[2], $new, PREG_SET_ORDER);
preg_match_all($line_pattern, $block[4], $old, PREG_SET_ORDER);
$result .= "<pre class=\"diff\"><address>" . diff_lines($block[3], $block[1]) . ":</address>";
$old_line = $new_line = 0;
while ($old_line<count($old) || $new_line<count($new))
{
$match = ($old[$old_line] ? $old[$old_line][1] : " ") . ($new[$new_line] ? $new[$new_line][1] : " ");
switch ($match)
{
case " ":
case " ":
$result .= ($old[$old_line] ? $old[$old_line][2] : $new[$new_line][2]);
$old_line++; $new_line++; break;
case "+ ":
case "! ":
$result .= "<del>" . $old[$old_line][2] . "</del>";
$old_line++; break;
case " -":
case " !":
$result .= "<ins>" . $new[$new_line][2] . "</ins>";
$new_line++; break;
case "!!":
$result .= "<del>" . $old[$old_line][2] . "</del>";
$result .= "<ins>" . $new[$new_line][2] . "</ins>";
$old_line++; $new_line++; break;
default:
$result .= "Error: Unknown '" . $match . "'\n";
$result .= $old[$old_line][2];
$result .= $new[$new_line][2];
$old_line++; $new_line++;
}
}
$result .= "";
}
return $result;
}
function diff_lines($lines_old, $lines_new) {
$lines_old = explode(",", $lines_old);
$lines_new = explode(",", $lines_new);
$lines_start = min($lines_old[0], $lines_new[0]);
$lines_stop = max($lines_old[1], $lines_new[1]);
$lines_count = ($lines_new[1] - $lines_new[0]) - ($lines_old[1] - $lines_old[0]);
#print_r($lines_old);
#print_r($lines_new);
if (!$lines_stop)
$result = sprintf("Regel %u", $lines_start);
else
$result = sprintf("Regels %u t/m %u", $lines_start, $lines_stop);
if ($lines_count)
if ($lines_count == 1 || $lines_count == -1)
$result .= sprintf(" (%+d regel)", $lines_count);
else
$result .= sprintf(" (%+d regels)", $lines_count);
return $result;
}
echo diff_styled($string);
?>