File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -320,7 +320,34 @@ void invert_patch(List<patchResult> patch) {
320320//}
321321
322322List <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
326353List <String > diff_merge_keepall (List <String > file1, List <String > file2) {
You can’t perform that action at this time.
0 commit comments