Skip to content

Commit 786b8b0

Browse files
committed
adding patch
1 parent 1494695 commit 786b8b0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

lib/src/diff.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,34 @@ void invert_patch(List<patchResult> patch) {
320320
//}
321321

322322
List<String> patch(List<String> file, List<patchResult> patch) {
323-
throw new UnimplementedError();
323+
// Applies a patch to a file.
324+
//
325+
// Given file1 and file2, Diff.patch(file1, Diff.diff_patch(file1, file2)) should give file2.
326+
327+
List<String> result = new List<String>();
328+
int commonOffset = 0;
329+
330+
void copyCommon(int targetOffset) {
331+
while (commonOffset < targetOffset) {
332+
result.add(file[commonOffset]);
333+
commonOffset++;
334+
}
335+
}
336+
337+
for (int chunkIndex = 0; chunkIndex < patch.length; chunkIndex++) {
338+
patchResult chunk = patch[chunkIndex];
339+
copyCommon(chunk.file1.Offset);
340+
341+
for (int lineIndex = 0; lineIndex < chunk.file2.Chunk.length; lineIndex++) {
342+
result.add(chunk.file2.Chunk[lineIndex]);
343+
}
344+
345+
commonOffset += chunk.file1.Length;
346+
}
347+
348+
copyCommon(file.length);
349+
350+
return result;
324351
}
325352

326353
List<String> diff_merge_keepall(List<String> file1, List<String> file2) {

0 commit comments

Comments
 (0)