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

Commit33f2d74

Browse files
committed
brought back diffutils to life
1 parent696d22b commit33f2d74

File tree

19 files changed

+279
-148
lines changed

19 files changed

+279
-148
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ This is a test ~senctence~**for diffutils**.
5252
But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future.
5353

5454
###Changelog ###
55+
* Version 3.0-SNAPSHOT
56+
* Due to licensing issues Delta.java and DiffAlgorithm.java were removed.
5557
* Version 2.3-SNAPSHOT
5658
* Introduced a process listener to diff algorithms. For long running
5759
diffs one could implement some progress information.

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>com.github.wumpz</groupId>
44
<artifactId>diffutils</artifactId>
55
<packaging>jar</packaging>
6-
<version>2.3-SNAPSHOT</version>
6+
<version>3.0-SNAPSHOT</version>
77
<name>java-diff-utils</name>
88
<description>The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.</description>
99
<url>https://github.com/wumpz/java-diff-utils</url>

‎src/main/java/com/github/difflib/DiffUtils.java‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
*/
2020
packagecom.github.difflib;
2121

22-
importcom.github.difflib.algorithm.DiffAlgorithm;
22+
importcom.github.difflib.algorithm.DiffAlgorithmI;
2323
importcom.github.difflib.algorithm.DiffAlgorithmListener;
2424
importcom.github.difflib.algorithm.DiffException;
2525
importcom.github.difflib.algorithm.myers.MyersDiff;
26-
importcom.github.difflib.patch.Delta;
26+
importcom.github.difflib.patch.AbstractDelta;
2727
importcom.github.difflib.patch.Patch;
2828
importcom.github.difflib.patch.PatchFailedException;
2929
importjava.util.ArrayList;
@@ -61,27 +61,30 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffEx
6161
/**
6262
* Computes the difference between the original and revised text.
6363
*/
64-
publicstaticPatch<String>diff(StringoriginalText,StringrevisedText,DiffAlgorithmListenerprogress)throwsDiffException {
65-
returnDiffUtils.diff(Arrays.asList(originalText.split("\n")),Arrays.asList(revisedText.split("\n")),progress);
64+
publicstaticPatch<String>diff(StringsourceText,StringtargetText,
65+
DiffAlgorithmListenerprogress)throwsDiffException {
66+
returnDiffUtils.diff(
67+
Arrays.asList(sourceText.split("\n")),
68+
Arrays.asList(targetText.split("\n")),progress);
6669
}
6770

6871
/**
6972
* Computes the difference between the original and revised list of elements with default diff algorithm
7073
*
71-
* @paramoriginal The original text. Must not be {@code null}.
72-
* @paramrevised The revised text. Must not be {@code null}.
74+
* @paramsource The original text. Must not be {@code null}.
75+
* @paramtarget The revised text. Must not be {@code null}.
7376
*
7477
* @param equalizer the equalizer object to replace the default compare algorithm (Object.equals). If {@code null}
7578
* the default equalizer of the default algorithm is used..
7679
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
7780
*/
78-
publicstatic <T>Patch<T>diff(List<T>original,List<T>revised,
81+
publicstatic <T>Patch<T>diff(List<T>source,List<T>target,
7982
BiPredicate<T,T>equalizer)throwsDiffException {
8083
if (equalizer !=null) {
81-
returnDiffUtils.diff(original,revised,
84+
returnDiffUtils.diff(source,target,
8285
newMyersDiff<>(equalizer));
8386
}
84-
returnDiffUtils.diff(original,revised,newMyersDiff<>());
87+
returnDiffUtils.diff(source,target,newMyersDiff<>());
8588
}
8689

8790
/**
@@ -94,12 +97,12 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
9497
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
9598
*/
9699
publicstatic <T>Patch<T>diff(List<T>original,List<T>revised,
97-
DiffAlgorithm<T>algorithm,DiffAlgorithmListenerprogress)throwsDiffException {
100+
DiffAlgorithmI<T>algorithm,DiffAlgorithmListenerprogress)throwsDiffException {
98101
Objects.requireNonNull(original,"original must not be null");
99102
Objects.requireNonNull(revised,"revised must not be null");
100103
Objects.requireNonNull(algorithm,"algorithm must not be null");
101104

102-
returnPatch.generate(original,revised,algorithm.diff(original,revised,progress));
105+
returnPatch.generate(original,revised,algorithm.computeDiff(original,revised,progress));
103106
}
104107

105108
/**
@@ -111,7 +114,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
111114
* @return The patch describing the difference between the original and revised sequences. Never {@code null}.
112115
*/
113116
publicstatic <T>Patch<T>diff(List<T>original,List<T>revised,
114-
DiffAlgorithm<T>algorithm)throwsDiffException {
117+
DiffAlgorithmI<T>algorithm)throwsDiffException {
115118
returndiff(original,revised,algorithm,null);
116119
}
117120

@@ -133,9 +136,9 @@ public static Patch<String> diffInline(String original, String revised) throws D
133136
revList.add(character.toString());
134137
}
135138
Patch<String>patch =DiffUtils.diff(origList,revList);
136-
for (Delta<String>delta :patch.getDeltas()) {
137-
delta.getOriginal().setLines(compressLines(delta.getOriginal().getLines(),""));
138-
delta.getRevised().setLines(compressLines(delta.getRevised().getLines(),""));
139+
for (AbstractDelta<String>delta :patch.getDeltas()) {
140+
delta.getSource().setLines(compressLines(delta.getSource().getLines(),""));
141+
delta.getTarget().setLines(compressLines(delta.getTarget().getLines(),""));
139142
}
140143
returnpatch;
141144
}

‎src/main/java/com/github/difflib/UnifiedDiffUtils.java‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
importcom.github.difflib.patch.ChangeDelta;
1919
importcom.github.difflib.patch.Chunk;
20-
importcom.github.difflib.patch.Delta;
20+
importcom.github.difflib.patch.AbstractDelta;
2121
importcom.github.difflib.patch.Patch;
2222
importjava.util.ArrayList;
2323
importjava.util.List;
@@ -146,21 +146,21 @@ public static List<String> generateUnifiedDiff(String originalFileName,
146146
ret.add("--- " +originalFileName);
147147
ret.add("+++ " +revisedFileName);
148148

149-
List<Delta<String>>patchDeltas =newArrayList<>(
149+
List<AbstractDelta<String>>patchDeltas =newArrayList<>(
150150
patch.getDeltas());
151151

152152
// code outside the if block also works for single-delta issues.
153-
List<Delta<String>>deltas =newArrayList<>();// current
153+
List<AbstractDelta<String>>deltas =newArrayList<>();// current
154154
// list
155155
// of
156156
// Delta's to
157157
// process
158-
Delta<String>delta =patchDeltas.get(0);
158+
AbstractDelta<String>delta =patchDeltas.get(0);
159159
deltas.add(delta);// add the first Delta to the current set
160160
// if there's more than 1 Delta, we may need to output them together
161161
if (patchDeltas.size() >1) {
162162
for (inti =1;i <patchDeltas.size();i++) {
163-
intposition =delta.getOriginal().getPosition();// store
163+
intposition =delta.getSource().getPosition();// store
164164
// the
165165
// current
166166
// position
@@ -170,9 +170,9 @@ public static List<String> generateUnifiedDiff(String originalFileName,
170170
// Check if the next Delta is too close to the current
171171
// position.
172172
// And if it is, add it to the current set
173-
Delta<String>nextDelta =patchDeltas.get(i);
174-
if ((position +delta.getOriginal().size() +contextSize) >= (nextDelta
175-
.getOriginal().getPosition() -contextSize)) {
173+
AbstractDelta<String>nextDelta =patchDeltas.get(i);
174+
if ((position +delta.getSource().size() +contextSize) >= (nextDelta
175+
.getSource().getPosition() -contextSize)) {
176176
deltas.add(nextDelta);
177177
}else {
178178
// if it isn't, output the current set,
@@ -207,65 +207,65 @@ public static List<String> generateUnifiedDiff(String originalFileName,
207207
* @author Bill James (tankerbay@gmail.com)
208208
*/
209209
privatestaticList<String>processDeltas(List<String>origLines,
210-
List<Delta<String>>deltas,intcontextSize) {
210+
List<AbstractDelta<String>>deltas,intcontextSize) {
211211
List<String>buffer =newArrayList<>();
212212
intorigTotal =0;// counter for total lines output from Original
213213
intrevTotal =0;// counter for total lines output from Original
214214
intline;
215215

216-
Delta<String>curDelta =deltas.get(0);
216+
AbstractDelta<String>curDelta =deltas.get(0);
217217

218218
// NOTE: +1 to overcome the 0-offset Position
219-
intorigStart =curDelta.getOriginal().getPosition() +1 -contextSize;
219+
intorigStart =curDelta.getSource().getPosition() +1 -contextSize;
220220
if (origStart <1) {
221221
origStart =1;
222222
}
223223

224-
intrevStart =curDelta.getRevised().getPosition() +1 -contextSize;
224+
intrevStart =curDelta.getTarget().getPosition() +1 -contextSize;
225225
if (revStart <1) {
226226
revStart =1;
227227
}
228228

229229
// find the start of the wrapper context code
230-
intcontextStart =curDelta.getOriginal().getPosition() -contextSize;
230+
intcontextStart =curDelta.getSource().getPosition() -contextSize;
231231
if (contextStart <0) {
232232
contextStart =0;// clamp to the start of the file
233233
}
234234

235235
// output the context before the first Delta
236-
for (line =contextStart;line <curDelta.getOriginal().getPosition();line++) {//
236+
for (line =contextStart;line <curDelta.getSource().getPosition();line++) {//
237237
buffer.add(" " +origLines.get(line));
238238
origTotal++;
239239
revTotal++;
240240
}
241241

242242
// output the first Delta
243243
buffer.addAll(getDeltaText(curDelta));
244-
origTotal +=curDelta.getOriginal().getLines().size();
245-
revTotal +=curDelta.getRevised().getLines().size();
244+
origTotal +=curDelta.getSource().getLines().size();
245+
revTotal +=curDelta.getTarget().getLines().size();
246246

247247
intdeltaIndex =1;
248248
while (deltaIndex <deltas.size()) {// for each of the other Deltas
249-
Delta<String>nextDelta =deltas.get(deltaIndex);
250-
intintermediateStart =curDelta.getOriginal().getPosition()
251-
+curDelta.getOriginal().getLines().size();
252-
for (line =intermediateStart;line <nextDelta.getOriginal()
249+
AbstractDelta<String>nextDelta =deltas.get(deltaIndex);
250+
intintermediateStart =curDelta.getSource().getPosition()
251+
+curDelta.getSource().getLines().size();
252+
for (line =intermediateStart;line <nextDelta.getSource()
253253
.getPosition();line++) {
254254
// output the code between the last Delta and this one
255255
buffer.add(" " +origLines.get(line));
256256
origTotal++;
257257
revTotal++;
258258
}
259259
buffer.addAll(getDeltaText(nextDelta));// output the Delta
260-
origTotal +=nextDelta.getOriginal().getLines().size();
261-
revTotal +=nextDelta.getRevised().getLines().size();
260+
origTotal +=nextDelta.getSource().getLines().size();
261+
revTotal +=nextDelta.getTarget().getLines().size();
262262
curDelta =nextDelta;
263263
deltaIndex++;
264264
}
265265

266266
// Now output the post-Delta context code, clamping the end of the file
267-
contextStart =curDelta.getOriginal().getPosition()
268-
+curDelta.getOriginal().getLines().size();
267+
contextStart =curDelta.getSource().getPosition()
268+
+curDelta.getSource().getLines().size();
269269
for (line =contextStart; (line < (contextStart +contextSize))
270270
&& (line <origLines.size());line++) {
271271
buffer.add(" " +origLines.get(line));
@@ -275,7 +275,7 @@ private static List<String> processDeltas(List<String> origLines,
275275

276276
// Create and insert the block header, conforming to the Unified Diff
277277
// standard
278-
StringBufferheader =newStringBuffer();
278+
StringBuilderheader =newStringBuilder();
279279
header.append("@@ -");
280280
header.append(origStart);
281281
header.append(",");
@@ -297,12 +297,12 @@ private static List<String> processDeltas(List<String> origLines,
297297
* @return list of String lines of code.
298298
* @author Bill James (tankerbay@gmail.com)
299299
*/
300-
privatestaticList<String>getDeltaText(Delta<String>delta) {
300+
privatestaticList<String>getDeltaText(AbstractDelta<String>delta) {
301301
List<String>buffer =newArrayList<>();
302-
for (Stringline :delta.getOriginal().getLines()) {
302+
for (Stringline :delta.getSource().getLines()) {
303303
buffer.add("-" +line);
304304
}
305-
for (Stringline :delta.getRevised().getLines()) {
305+
for (Stringline :delta.getTarget().getLines()) {
306306
buffer.add("+" +line);
307307
}
308308
returnbuffer;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
packagecom.github.difflib.algorithm;
17+
18+
importjava.util.List;
19+
20+
/**
21+
* Interface of a diff algorithm.
22+
*
23+
* @author Tobias Warneke (t.warneke@gmx.net)
24+
*/
25+
publicinterfaceDiffAlgorithmI<T> {
26+
27+
/**
28+
* Computes the changeset to patch the source list to the target list.
29+
*
30+
* @param source source data
31+
* @param target target data
32+
* @param progress progress listener
33+
* @return
34+
* @throws DiffException
35+
*/
36+
List<Change>computeDiff(List<T>source,List<T>target,DiffAlgorithmListenerprogress)throwsDiffException;
37+
}

‎src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
packagecom.github.difflib.algorithm.jgit;
1717

1818
importcom.github.difflib.algorithm.Change;
19-
importcom.github.difflib.algorithm.DiffAlgorithm;
19+
importcom.github.difflib.algorithm.DiffAlgorithmI;
2020
importcom.github.difflib.algorithm.DiffAlgorithmListener;
2121
importcom.github.difflib.algorithm.DiffException;
2222
importcom.github.difflib.patch.DeltaType;
@@ -34,17 +34,17 @@
3434
*
3535
* @author toben
3636
*/
37-
publicclassHistogramDiff<T>implementsDiffAlgorithm<T> {
37+
publicclassHistogramDiff<T>implementsDiffAlgorithmI<T> {
3838

3939
@Override
40-
publicList<Change>diff(List<T>original,List<T>revised,DiffAlgorithmListenerprogress)throwsDiffException {
41-
Objects.requireNonNull(original,"original list must not be null");
42-
Objects.requireNonNull(revised,"revised list must not be null");
40+
publicList<Change>computeDiff(List<T>source,List<T>target,DiffAlgorithmListenerprogress)throwsDiffException {
41+
Objects.requireNonNull(source,"source list must not be null");
42+
Objects.requireNonNull(target,"target list must not be null");
4343
if (progress !=null) {
4444
progress.diffStart();
4545
}
4646
EditListdiffList =newEditList();
47-
diffList.addAll(neworg.eclipse.jgit.diff.HistogramDiff().diff(newDataListComparator<>(progress),newDataList<>(original),newDataList<>(revised)));
47+
diffList.addAll(neworg.eclipse.jgit.diff.HistogramDiff().diff(newDataListComparator<>(progress),newDataList<>(source),newDataList<>(target)));
4848
List<Change>patch =newArrayList<>();
4949
for (Editedit :diffList) {
5050
DeltaTypetype =DeltaType.EQUAL;

‎src/main/java/com/github/difflib/algorithm/myers/MyersDiff.java‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
packagecom.github.difflib.algorithm.myers;
2121

2222
importcom.github.difflib.algorithm.Change;
23-
importcom.github.difflib.algorithm.DiffAlgorithm;
23+
importcom.github.difflib.algorithm.DiffAlgorithmI;
2424
importcom.github.difflib.algorithm.DiffAlgorithmListener;
2525
importcom.github.difflib.algorithm.DiffException;
2626
importcom.github.difflib.algorithm.DifferentiationFailedException;
@@ -34,7 +34,7 @@
3434
/**
3535
* A clean-room implementation of Eugene Myers greedy differencing algorithm.
3636
*/
37-
publicfinalclassMyersDiff<T>implementsDiffAlgorithm<T> {
37+
publicfinalclassMyersDiff<T>implementsDiffAlgorithmI<T> {
3838

3939
privatefinalBiPredicate<T,T>DEFAULT_EQUALIZER =Object::equals;
4040
privatefinalBiPredicate<T,T>equalizer;
@@ -54,15 +54,15 @@ public MyersDiff(final BiPredicate<T, T> equalizer) {
5454
* Return empty diff if get the error while procession the difference.
5555
*/
5656
@Override
57-
publicList<Change>diff(finalList<T>original,finalList<T>revised,DiffAlgorithmListenerprogress)throwsDiffException {
58-
Objects.requireNonNull(original,"original list must not be null");
59-
Objects.requireNonNull(revised,"revised list must not be null");
57+
publicList<Change>computeDiff(finalList<T>source,finalList<T>target,DiffAlgorithmListenerprogress)throwsDiffException {
58+
Objects.requireNonNull(source,"source list must not be null");
59+
Objects.requireNonNull(target,"target list must not be null");
6060

6161
if (progress !=null) {
6262
progress.diffStart();
6363
}
64-
PathNodepath =buildPath(original,revised,progress);
65-
List<Change>result =buildRevision(path,original,revised);
64+
PathNodepath =buildPath(source,target,progress);
65+
List<Change>result =buildRevision(path,source,target);
6666
if (progress !=null) {
6767
progress.diffEnd();
6868
}
@@ -177,18 +177,7 @@ private List<Change> buildRevision(PathNode actualPath, List<T> orig, List<T> re
177177
}else {
178178
changes.add(newChange(DeltaType.CHANGE,ianchor,i,janchor,j));
179179
}
180-
// Chunk<T> original = new Chunk<>(ianchor, copyOfRange(orig, ianchor, i));
181-
// Chunk<T> revised = new Chunk<>(janchor, copyOfRange(rev, janchor, j));
182-
// Delta<T> delta = null;
183-
// if (original.size() == 0 && revised.size() != 0) {
184-
// delta = new InsertDelta<>(original, revised);
185-
// } else if (original.size() > 0 && revised.size() == 0) {
186-
// delta = new DeleteDelta<>(original, revised);
187-
// } else {
188-
// delta = new ChangeDelta<>(original, revised);
189-
// }
190-
//
191-
// patch.addDelta(delta);
180+
192181
if (path.isSnake()) {
193182
path =path.prev;
194183
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp