|
1 | 1 | /*
|
2 |
| - Copyright 2009 Dmitry Naumenko ( [email protected]) |
| 2 | + * Copyright 2009-2017 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 | +package com.github.difflib; |
| 17 | + |
| 18 | +import com.github.difflib.algorithm.DiffAlgorithmI; |
| 19 | +import com.github.difflib.algorithm.DiffAlgorithmListener; |
| 20 | +import com.github.difflib.algorithm.myers.MyersDiff; |
| 21 | +import com.github.difflib.patch.AbstractDelta; |
| 22 | +import com.github.difflib.patch.Patch; |
| 23 | +import com.github.difflib.patch.PatchFailedException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.function.BiPredicate; |
| 30 | + |
| 31 | +/** |
| 32 | + * Implements the difference and patching engine |
| 33 | + */ |
| 34 | +public final class DiffUtils { |
| 35 | + |
| 36 | + /** |
| 37 | + * Computes the difference between the original and revised list of elements with default diff |
| 38 | + * algorithm |
| 39 | + * |
| 40 | + * @param <T> types to be diffed |
| 41 | + * @param original The original text. Must not be {@code null}. |
| 42 | + * @param revised The revised text. Must not be {@code null}. |
| 43 | + * @param progress progress listener |
| 44 | + * @return The patch describing the difference between the original and revised sequences. Never |
| 45 | + * {@code null}. |
| 46 | + */ |
| 47 | + public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress) { |
| 48 | + return DiffUtils.diff(original, revised, new MyersDiff<>(), progress); |
| 49 | + } |
| 50 | + |
| 51 | + public static <T> Patch<T> diff(List<T> original, List<T> revised) { |
| 52 | + return DiffUtils.diff(original, revised, new MyersDiff<>(), null); |
| 53 | + } |
3 | 54 |
|
4 |
| - This file is part of Java Diff Utills Library. |
| 55 | + public static <T> Patch<T> diff(List<T> original, List<T> revised, boolean includeEqualParts) { |
| 56 | + return DiffUtils.diff(original, revised, new MyersDiff<>(), null, includeEqualParts); |
| 57 | + } |
5 | 58 |
|
6 |
| - Java Diff Utills Library is free software: you can redistribute it and/or modify |
7 |
| - it under the terms of the GNU General Public License as published by |
8 |
| - the Free Software Foundation, either version 3 of the License, or |
9 |
| - (at your option) any later version. |
| 59 | + /** |
| 60 | + * Computes the difference between the original and revised text. |
| 61 | + */ |
| 62 | + public static Patch<String> diff(String sourceText, String targetText, |
| 63 | + DiffAlgorithmListener progress) { |
| 64 | + return DiffUtils.diff( |
| 65 | + Arrays.asList(sourceText.split("\n")), |
| 66 | + Arrays.asList(targetText.split("\n")), progress); |
| 67 | + } |
10 | 68 |
|
11 |
| - Java Diff Utills Library is distributed in the hope that it will be useful, |
12 |
| - but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
| - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
| - GNU General Public License for more details. |
| 69 | + /** |
| 70 | + * Computes the difference between the original and revised list of elements with default diff |
| 71 | + * algorithm |
| 72 | + * |
| 73 | + * @param source The original text. Must not be {@code null}. |
| 74 | + * @param target The revised text. Must not be {@code null}. |
| 75 | + * |
| 76 | + * @param equalizer the equalizer object to replace the default compare algorithm |
| 77 | + * (Object.equals). If {@code null} the default equalizer of the default algorithm is used.. |
| 78 | + * @return The patch describing the difference between the original and revised sequences. Never |
| 79 | + * {@code null}. |
| 80 | + */ |
| 81 | + public static <T> Patch<T> diff(List<T> source, List<T> target, |
| 82 | + BiPredicate<T, T> equalizer) { |
| 83 | + if (equalizer != null) { |
| 84 | + return DiffUtils.diff(source, target, |
| 85 | + new MyersDiff<>(equalizer)); |
| 86 | + } |
| 87 | + return DiffUtils.diff(source, target, new MyersDiff<>()); |
| 88 | + } |
15 | 89 |
|
16 |
| - You should have received a copy of the GNU General Public License |
17 |
| - along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>. |
18 |
| -*/ |
19 |
| -package difflib; |
| 90 | + public static <T> Patch<T> diff(List<T> original, List<T> revised, |
| 91 | + DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) { |
| 92 | + return diff(original, revised, algorithm, progress, false); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Computes the difference between the original and revised list of elements with default diff |
| 97 | + * algorithm |
| 98 | + * |
| 99 | + * @param original The original text. Must not be {@code null}. |
| 100 | + * @param revised The revised text. Must not be {@code null}. |
| 101 | + * @param algorithm The diff algorithm. Must not be {@code null}. |
| 102 | + * @param progress The diff algorithm listener. |
| 103 | + * @param includeEqualParts Include equal data parts into the patch. |
| 104 | + * @return The patch describing the difference between the original and revised sequences. Never |
| 105 | + * {@code null}. |
| 106 | + */ |
| 107 | + public static <T> Patch<T> diff(List<T> original, List<T> revised, |
| 108 | + DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress, |
| 109 | + boolean includeEqualParts) { |
| 110 | + Objects.requireNonNull(original, "original must not be null"); |
| 111 | + Objects.requireNonNull(revised, "revised must not be null"); |
| 112 | + Objects.requireNonNull(algorithm, "algorithm must not be null"); |
20 | 113 |
|
21 |
| -import java.util.*; |
22 |
| -import java.util.regex.Matcher; |
23 |
| -import java.util.regex.Pattern; |
| 114 | + return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts); |
| 115 | + } |
24 | 116 |
|
25 |
| -import difflib.myers.*; |
| 117 | + /** |
| 118 | + * Computes the difference between the original and revised list of elements with default diff |
| 119 | + * algorithm |
| 120 | + * |
| 121 | + * @param original The original text. Must not be {@code null}. |
| 122 | + * @param revised The revised text. Must not be {@code null}. |
| 123 | + * @param algorithm The diff algorithm. Must not be {@code null}. |
| 124 | + * @return The patch describing the difference between the original and revised sequences. Never |
| 125 | + * {@code null}. |
| 126 | + */ |
| 127 | + public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm) { |
| 128 | + return diff(original, revised, algorithm, null); |
| 129 | + } |
26 | 130 |
|
27 |
| -/** |
28 |
| - * Implements the difference and patching engine |
29 |
| - * |
30 |
| - * @author <a href=" [email protected]">Dmitry Naumenko</a> |
31 |
| - * @version 0.4.1 |
32 |
| - */ |
33 |
| -public class DiffUtils { |
34 |
| - private static DiffAlgorithm defaultDiffAlgorithm = new MyersDiff(); |
35 |
| - private static Pattern unifiedDiffChunkRe = |
36 |
| - Pattern.compile("@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@"); |
37 |
| - |
38 |
| - /** |
39 |
| - * Compute the difference between the original and revised texts with default diff algorithm |
40 |
| - * |
41 |
| - * @param original the original text |
42 |
| - * @param revised the revised text |
43 |
| - * @return the patch describing the difference between the original and revised texts |
44 |
| - */ |
45 |
| - public static Patch diff(List<?> original, List<?> revised) { |
46 |
| - return DiffUtils.diff(original, revised, defaultDiffAlgorithm); |
47 |
| - } |
48 |
| - |
49 |
| - /** |
50 |
| - * Compute the difference between the original and revised texts with given diff algorithm |
51 |
| - * |
52 |
| - * @param original the original text |
53 |
| - * @param revised the revised text |
54 |
| - * @param algorithm the given algorithm |
55 |
| - * @return the patch describing the difference between the original and revised texts |
56 |
| - */ |
57 |
| - public static Patch diff(List<?> original, List<?> revised, DiffAlgorithm algorithm) { |
58 |
| - return algorithm.diff(original, revised); |
59 |
| - } |
60 |
| - |
61 |
| - /** |
62 |
| - * Patch the original text with given patch |
63 |
| - * |
64 |
| - * @param original the original text |
65 |
| - * @param patch the given patch |
66 |
| - * @return the revised text |
67 |
| - * @throws PatchFailedException if can't apply patch |
68 |
| - */ |
69 |
| - public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException { |
70 |
| - return patch.applyTo(original); |
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Unpatch the revised text for a given patch |
75 |
| - * |
76 |
| - * @param revised the revised text |
77 |
| - * @param patch the given patch |
78 |
| - * @return the original text |
79 |
| - */ |
80 |
| - public static List<?> unpatch(List<?> revised, Patch patch) { |
81 |
| - return patch.restore(revised); |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Parse the given text in unified format and creates the list of deltas for it. |
86 |
| - * |
87 |
| - * @param diff the text in unified format |
88 |
| - * @return the patch with deltas. |
89 |
| - */ |
90 |
| - public static Patch parseUnifiedDiff(List<String> diff) { |
91 |
| - boolean inPrelude = true; |
92 |
| - List<Object[]> rawChunk = new ArrayList<Object[]>(); |
93 |
| - Patch patch = new Patch(); |
94 |
| - |
95 |
| - int old_ln = 0, old_n = 0, new_ln = 0, new_n = 0; |
96 |
| - String tag = "", rest = ""; |
97 |
| - for (String line: diff) { |
98 |
| - // Skip leading lines until after we've seen one starting with '+++' |
99 |
| - if (inPrelude) { |
100 |
| - if (line.startsWith("+++")) { |
101 |
| - inPrelude = false; |
102 |
| - } |
103 |
| - continue; |
104 |
| - } |
105 |
| - Matcher m = unifiedDiffChunkRe.matcher(line); |
106 |
| - if (m.find()) { |
107 |
| - // Process the lines in the previous chunk |
108 |
| - if (rawChunk.size() != 0) { |
109 |
| - List<String> oldChunkLines = new ArrayList<String>(); |
110 |
| - List<String> newChunkLines = new ArrayList<String>(); |
111 |
| - |
112 |
| - for (Object[] raw_line: rawChunk) { |
113 |
| - tag = (String)raw_line[0]; |
114 |
| - rest = (String)raw_line[1]; |
115 |
| - if (tag.equals(" ") || tag.equals("-")) { |
116 |
| - oldChunkLines.add(rest); |
117 |
| - } |
118 |
| - if (tag.equals(" ") || tag.equals("+")) { |
119 |
| - newChunkLines.add(rest); |
120 |
| - } |
121 |
| - } |
122 |
| - patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines), |
123 |
| - new Chunk(new_ln - 1, new_n, newChunkLines))); |
124 |
| - rawChunk.clear(); |
125 |
| - } |
126 |
| - // Parse the @@ header |
127 |
| - old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1)); |
128 |
| - old_n = m.group(2) == null ? 1 : Integer.parseInt(m.group(2)); |
129 |
| - new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3)); |
130 |
| - new_n = m.group(4) == null ? 1 : Integer.parseInt(m.group(4)); |
131 |
| - old_ln = Integer.parseInt(m.group(1)); |
132 |
| - |
133 |
| - if (old_ln == 0) { |
134 |
| - old_ln += 1; |
135 |
| - } |
136 |
| - if (new_ln == 0) { |
137 |
| - new_ln += 1; |
138 |
| - } |
139 |
| - } else { |
140 |
| - if (line.length() > 0) { |
141 |
| - tag = line.substring(0, 1); |
142 |
| - rest = line.substring(1); |
143 |
| - if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) { |
144 |
| - rawChunk.add(new Object[] {tag, rest}); |
145 |
| - } |
146 |
| - } |
147 |
| - } |
148 |
| - } |
149 |
| - |
150 |
| - // Process the lines in the last chunk |
151 |
| - if (rawChunk.size() != 0) { |
152 |
| - List<String> oldChunkLines = new ArrayList<String>(); |
153 |
| - List<String> newChunkLines = new ArrayList<String>(); |
154 |
| - |
155 |
| - for (Object[] raw_line: rawChunk) { |
156 |
| - tag = (String)raw_line[0]; |
157 |
| - rest = (String)raw_line[1]; |
158 |
| - if (tag.equals(" ") || tag.equals("-")) { |
159 |
| - oldChunkLines.add(rest); |
160 |
| - } |
161 |
| - if (tag.equals(" ") || tag.equals("+")) { |
162 |
| - newChunkLines.add(rest); |
163 |
| - } |
164 |
| - } |
165 |
| - |
166 |
| - patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines), |
167 |
| - new Chunk(new_ln - 1, new_n, newChunkLines))); |
168 |
| - rawChunk.clear(); |
169 |
| - } |
170 |
| - |
171 |
| - return patch; |
172 |
| - } |
173 |
| - |
| 131 | + private static List<String> compressLines(List<String> lines, String delimiter) { |
| 132 | + if (lines.isEmpty()) { |
| 133 | + return Collections.emptyList(); |
| 134 | + } |
| 135 | + return Collections.singletonList(String.join(delimiter, lines)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Patch the original text with given patch |
| 140 | + * |
| 141 | + * @param original the original text |
| 142 | + * @param patch the given patch |
| 143 | + * @return the revised text |
| 144 | + * @throws PatchFailedException if can't apply patch |
| 145 | + */ |
| 146 | + public static <T> List<T> patch(List<T> original, Patch<T> patch) |
| 147 | + throws PatchFailedException { |
| 148 | + return patch.applyTo(original); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Unpatch the revised text for a given patch |
| 153 | + * |
| 154 | + * @param revised the revised text |
| 155 | + * @param patch the given patch |
| 156 | + * @return the original text |
| 157 | + */ |
| 158 | + public static <T> List<T> unpatch(List<T> revised, Patch<T> patch) { |
| 159 | + return patch.restore(revised); |
| 160 | + } |
| 161 | + |
| 162 | + private DiffUtils() { |
| 163 | + } |
174 | 164 | }
|
0 commit comments