Skip to content

Commit 1252d2f

Browse files
committed
adding diff_comm implementation
1 parent 8149f56 commit 1252d2f

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

lib/src/diff.dart

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,56 @@ CandidateThing longest_common_subsequence(List<String> file1, List<String>
200200
//}
201201

202202
List<commonOrDifferentThing> diff_comm(List<String> file1, List<String> file2) {
203-
throw new UnimplementedError();
203+
// We apply the LCS to build a "comm"-style picture of the
204+
// differences between file1 and file2.
205+
206+
List<commonOrDifferentThing> result = new List<commonOrDifferentThing>();
207+
208+
int tail1 = file1.length;
209+
int tail2 = file2.length;
210+
211+
commonOrDifferentThing common = new commonOrDifferentThing()
212+
..common = new List<String>();
213+
214+
void processCommon() {
215+
if (common.common.length > 0) {
216+
common.common = common.common.reversed.toList();
217+
result.add(common);
218+
common = new commonOrDifferentThing()
219+
..common = new List<String>();
220+
}
221+
}
222+
223+
for (CandidateThing candidate = longest_common_subsequence(file1, file2);
224+
candidate != null;
225+
candidate = candidate.chain) {
226+
commonOrDifferentThing different = new commonOrDifferentThing()
227+
..file1 = new List<String>()
228+
..file2 = new List<String>();
229+
230+
while (--tail1 > candidate.file1index) {
231+
different.file1.add(file1[tail1]);
232+
}
233+
234+
while (--tail2 > candidate.file2index) {
235+
different.file2.add(file2[tail2]);
236+
}
237+
238+
if (different.file1.length > 0 || different.file2.length > 0) {
239+
processCommon();
240+
different.file1 = different.file1.reversed.toList();
241+
different.file2 = different.file2.reversed.toList();
242+
result.add(different);
243+
}
244+
245+
if (tail1 >= 0) {
246+
common.common.add(file1[tail1]);
247+
}
248+
}
249+
250+
processCommon();
251+
252+
return result.reversed.toList();
204253
}
205254

206255
List<patchResult> diff_patch(List<String> file1, List<String> file2) {

0 commit comments

Comments
 (0)