mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
introduced diffInline, some cleanup
This commit is contained in:
@@ -217,12 +217,12 @@ public class DiffRowGenerator {
|
||||
*/
|
||||
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch<String> patch) {
|
||||
// normalize the lines (expand tabs, escape html entities)
|
||||
original = StringUtills.normalize(original);
|
||||
revised = StringUtills.normalize(revised);
|
||||
original = StringUtils.normalize(original);
|
||||
revised = StringUtils.normalize(revised);
|
||||
|
||||
// wrap to the column width
|
||||
original = StringUtills.wrapText(original, this.columnWidth);
|
||||
revised = StringUtills.wrapText(revised, this.columnWidth);
|
||||
original = StringUtils.wrapText(original, this.columnWidth);
|
||||
revised = StringUtils.wrapText(revised, this.columnWidth);
|
||||
|
||||
List<DiffRow> diffRows = new ArrayList<>();
|
||||
int endPos = 0;
|
||||
@@ -233,11 +233,11 @@ public class DiffRowGenerator {
|
||||
Chunk<String> rev = delta.getRevised();
|
||||
|
||||
// We should normalize and wrap lines in deltas too.
|
||||
orig.setLines(StringUtills.normalize((List<String>) orig.getLines()));
|
||||
rev.setLines(StringUtills.normalize((List<String>) rev.getLines()));
|
||||
orig.setLines(StringUtils.normalize((List<String>) orig.getLines()));
|
||||
rev.setLines(StringUtils.normalize((List<String>) rev.getLines()));
|
||||
|
||||
orig.setLines(StringUtills.wrapText((List<String>) orig.getLines(), this.columnWidth));
|
||||
rev.setLines(StringUtills.wrapText((List<String>) rev.getLines(), this.columnWidth));
|
||||
orig.setLines(StringUtils.wrapText((List<String>) orig.getLines(), this.columnWidth));
|
||||
rev.setLines(StringUtils.wrapText((List<String>) rev.getLines(), this.columnWidth));
|
||||
|
||||
// catch the equal prefix for each chunk
|
||||
for (String line : original.subList(endPos, orig.getPosition())) {
|
||||
@@ -291,7 +291,7 @@ public class DiffRowGenerator {
|
||||
}
|
||||
return diffRows;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add the inline diffs for given delta
|
||||
*
|
||||
|
||||
@@ -18,6 +18,8 @@ package difflib;
|
||||
import difflib.myers.Equalizer;
|
||||
import difflib.myers.MyersDiff;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -92,6 +94,46 @@ public final class DiffUtils {
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the difference between the given texts inline. This one uses the "trick" to make out
|
||||
* of texts lists of characters, like DiffRowGenerator does and merges those changes at the end
|
||||
* together again.
|
||||
*
|
||||
* @param original
|
||||
* @param revised
|
||||
* @return
|
||||
*/
|
||||
public static Patch<String> diffInline(String original, String revised) {
|
||||
LinkedList<String> origList = new LinkedList<>();
|
||||
LinkedList<String> revList = new LinkedList<>();
|
||||
for (Character character : original.toCharArray()) {
|
||||
origList.add(character.toString());
|
||||
}
|
||||
for (Character character : revised.toCharArray()) {
|
||||
revList.add(character.toString());
|
||||
}
|
||||
Patch<String> patch = DiffUtils.diff(origList, revList);
|
||||
for (Delta<String> delta : patch.getDeltas()) {
|
||||
delta.getOriginal().setLines(compressLines(delta.getOriginal().getLines(), ""));
|
||||
delta.getRevised().setLines(compressLines(delta.getRevised().getLines(), ""));
|
||||
}
|
||||
return patch;
|
||||
}
|
||||
|
||||
private static List<String> compressLines(List<String> lines, String delimiter) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
b.append(line);
|
||||
b.append(delimiter);
|
||||
}
|
||||
if (b.length() > 0) {
|
||||
b.setLength(b.length() - delimiter.length());
|
||||
return Collections.singletonList(b.toString());
|
||||
} else {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch the original text with given patch
|
||||
*
|
||||
|
||||
@@ -28,7 +28,7 @@ import java.util.ListIterator;
|
||||
*/
|
||||
public class Patch<T> {
|
||||
|
||||
private List<Delta<T>> deltas = new LinkedList<>();
|
||||
private final List<Delta<T>> deltas = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Apply this patch to the given target
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public final class StringUtills {
|
||||
public final class StringUtils {
|
||||
|
||||
public static <T> String join(final Iterable<T> objs, final String delimiter) {
|
||||
Iterator<T> iter = objs.iterator();
|
||||
@@ -94,6 +94,6 @@ public final class StringUtills {
|
||||
return line;
|
||||
}
|
||||
|
||||
private StringUtills() {
|
||||
private StringUtils() {
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DiffTest {
|
||||
public class DiffUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testDiff_Insert() {
|
||||
@@ -65,4 +65,27 @@ public class DiffTest {
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertTrue(delta instanceof InsertDelta);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffInline() {
|
||||
final Patch<String> patch = DiffUtils.diffInline("", "test");
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
|
||||
assertEquals(0, patch.getDeltas().get(0).getOriginal().getPosition());
|
||||
assertEquals(0, patch.getDeltas().get(0).getOriginal().getLines().size());
|
||||
assertEquals("test", patch.getDeltas().get(0).getRevised().getLines().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffInline2() {
|
||||
final Patch<String> patch = DiffUtils.diffInline("es", "fest");
|
||||
assertEquals(2, patch.getDeltas().size());
|
||||
assertTrue(patch.getDeltas().get(0) instanceof InsertDelta);
|
||||
assertEquals(0, patch.getDeltas().get(0).getOriginal().getPosition());
|
||||
assertEquals(2, patch.getDeltas().get(1).getOriginal().getPosition());
|
||||
assertEquals(0, patch.getDeltas().get(0).getOriginal().getLines().size());
|
||||
assertEquals(0, patch.getDeltas().get(1).getOriginal().getLines().size());
|
||||
assertEquals("f", patch.getDeltas().get(0).getRevised().getLines().get(0));
|
||||
assertEquals("t", patch.getDeltas().get(1).getRevised().getLines().get(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user