Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Tobias edited this pageMar 7, 2020 ·11 revisions

All examples are part of the tests sourcecode atdiffutils/examples or are simply tests. The examples are reduced to present the interesting parts of it.

Compute the difference between two files and print its deltas

File:ComputeDifference.java

//build simple lists of the lines of the two testfilesList<String>original =Files.readAllLines(newFile(ORIGINAL).toPath());List<String>revised =Files.readAllLines(newFile(RIVISED).toPath());//compute the patch: this is the diffutils partPatch<String>patch =DiffUtils.diff(original,revised);//simple output the computed patch to consolefor (AbstractDelta<String>delta :patch.getDeltas()) {System.out.println(delta);}

Get the file in unified format and apply it as the patch to given text

File:ApplyPatch.java

List<String>original =Files.readAllLines(newFile(ORIGINAL).toPath());List<String>patched =Files.readAllLines(newFile(PATCH).toPath());// At first, parse the unified diff file and get the patchPatch<String>patch =UnifiedDiffUtils.parseUnifiedDiff(patched);// Then apply the computed patch to the given textList<String>result =DiffUtils.patch(original,patch);//simple output to consoleSystem.out.println(result);

Generate a file in unified diff format import it and apply the patch

List<String>text1=Arrays.asList("this is a test","a test");List<String>text2=Arrays.asList("this is a testfile","a test");//generating diff information.Patch<String>diff =DiffUtils.diff(text1,text2);//generating unified diff formatList<String>unifiedDiff =UnifiedDiffUtils.generateUnifiedDiff("original-file.txt","new-file.txt",text1,diff,0);unifiedDiff.forEach(System.out::println);//importing unified diff format from file or here from memory to a PatchPatch<String>importedPatch =UnifiedDiffUtils.parseUnifiedDiff(unifiedDiff);//apply patch to original listList<String>patchedText =DiffUtils.patch(text1,importedPatch);System.out.println(patchedText);

Compute the difference between two texts and print it in human-readable markup style

one liner

Test:DiffRowGeneratorTest.testGeneratorExample1

The DiffRowGenerator does the main part in producing readable texts. Its instantiated using a builder pattern.

//create a configured DiffRowGeneratorDiffRowGeneratorgenerator =DiffRowGenerator.create()                .showInlineDiffs(true)                .mergeOriginalRevised(true)                .inlineDiffByWord(true)                .oldTag(f ->"~")//introduce markdown style for strikethrough                .newTag(f ->"**")//introduce markdown style for bold                .build();//compute the differences for two test texts.List<DiffRow>rows =generator.generateDiffRows(Arrays.asList("This is a test senctence."),Arrays.asList("This is a test for diffutils."));System.out.println(rows.get(0).getOldLine());

output is:

This is a testsenctencefor diffutils.

multi liner

Test:DiffRowGeneratorTest.testGeneratorExample2

The DiffRowGenerator does the main part in producing readable texts. Its instantiated using a builder pattern.

DiffRowGeneratorgenerator =DiffRowGenerator.create()                .showInlineDiffs(true)                .inlineDiffByWord(true)                .oldTag(f ->"~")                .newTag(f ->"**")                .build();List<DiffRow>rows =generator.generateDiffRows(Arrays.asList("This is a test senctence.","This is the second line.","And here is the finish."),Arrays.asList("This is a test for diffutils.","This is the second line."));System.out.println("|original|new|");System.out.println("|--------|---|");for (DiffRowrow :rows) {System.out.println("|" +row.getOldLine() +"|" +row.getNewLine() +"|");}

output is:

originalnew
This is a testsenctence.This is a testfor diffutils.
This is the second line.This is the second line.
And here is the finish.

Compute the difference between two non textual inputs

The difference computing part is generified. Therefore a List of objects could be used. Here is an example,that demonstrates a patch for an Integer List.Test:DiffUtilsTest.testDiffIntegerList

List<Integer>original =Arrays.asList(1,2,3,4,5);List<Integer>revised =Arrays.asList(2,3,4,6);Patch<Integer>patch =DiffUtils.diff(original,revised);for (Deltadelta :patch.getDeltas()) {System.out.println(delta);}

output is:

[DeleteDelta, position: 0, lines: [1]][ChangeDelta, position: 4, lines: [5] to [6]]

[8]ページ先頭

©2009-2025 Movatter.jp