-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathTextEditDiff.php
More file actions
149 lines (129 loc) · 4.02 KB
/
TextEditDiff.php
File metadata and controls
149 lines (129 loc) · 4.02 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Phpactor\TextDocument;
final class TextEditDiff
{
private const REPLACE = 'r';
private const NOOP = 'o';
private const ADD = '+';
private const DEL = '-';
public function diff(string $one, string $two): TextEdits
{
$table = $this->lcsTable($one, $two);
$ops = $this->resolveOps(
$table,
mb_str_split($one),
mb_str_split($two),
mb_strlen($one) - 1,
mb_strlen($two) - 1
);
$edits = $this->textEdits($ops);
return $edits;
}
/**
* @param array<int, array<int,int>> $table
* @param list<string> $x
* @param list<string> $y
* @param list<array{string,string,int}> $ops
* @return list<array{string,string,int}>
*/
public function resolveOps(array $table, array $x, array $y, int $i, int $j, array $ops = []): array
{
if ($i > 0 && $j > 0 && $x[$i] === $y[$j]) {
$ops = $this->resolveOps($table, $x, $y, $i-1, $j-1);
$ops[] = [self::NOOP, $x[$i], $i];
return $ops;
}
if ($j > 0 && ($i === 0 || $table[$i][$j-1] >= $table[$i-1][$j])) {
$ops = $this->resolveOps($table, $x, $y, $i, $j-1);
$ops[] = [self::ADD, $y[$j], $i + 1];
return $ops;
}
if ($i > 0 && ($j === 0 || $table[$i][$j-1] < $table[$i-1][$j])) {
$ops = $this->resolveOps($table, $x, $y, $i - 1, $j);
$ops[] = [self::DEL, $x[$i], $i];
return $ops;
}
if ($j === 0 && $i === 0 && $x[$i] !== $y[$j]) {
$ops[] = [self::REPLACE, $y[$i], $i];
return $ops;
}
return $ops;
}
/**
* @return array<int,array<int, int>>
*/
private function lcsTable(string $one, string $two): array
{
$m = mb_strlen($one);
$n = mb_strlen($two);
$table = [];
for ($i = 0; $i <= $m; $i++) {
$table[$i][0] = 0;
}
for ($j = 0; $j <= $n; $j++) {
$table[0][$j] = 0;
}
for ($i = 1; $i <= $m; $i++) {
for ($j = 1; $j <= $n; $j++) {
if (substr($one, $i - 1, 1) === substr($two, $j - 1, 1)) {
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
} else {
$table[$i][$j] = max($table[$i][$j - 1], $table[$i - 1][$j]);
}
}
}
return $table;
}
/**
* @param list<array{string,string,int}> $ops
*/
private function textEdits(array $ops): TextEdits
{
$chunks = [];
$currentOps = [];
$currentOpName = null;
$lastOp = null;
// chunk by operation
foreach ($ops as $op) {
$opName = $op[0];
if ($lastOp === null) {
$currentOps[] = $op;
} elseif ($opName != $lastOp) {
$chunks[] = $currentOps;
$currentOps = [$op];
} else {
$currentOps[] = $op;
}
$lastOp = $opName;
}
if ($currentOps) {
$chunks[] = $currentOps;
}
// covert to text edits
$edits = [];
foreach ($chunks as $chunk) {
if ($chunk[0][0] === self::ADD) {
$edits[] = TextEdit::create(
$chunk[0][2],
0,
implode('', array_map(fn (array $op) => $op[1], $chunk))
);
}
if ($chunk[0][0] === self::DEL) {
$edits[] = TextEdit::create(
$chunk[0][2],
count($chunk),
'',
);
}
if ($chunk[0][0] === self::REPLACE) {
$edits[] = TextEdit::create(
$chunk[0][2],
count($chunk),
implode('', array_map(fn (array $ops) => $ops[1], $chunk)),
);
}
}
return TextEdits::fromTextEdits($edits);
}
}