Issues 10, 12 fixed

This commit is contained in:
shotwave
2010-12-09 19:09:21 +00:00
parent 1fbf668da0
commit ceb4c5427c
6 changed files with 136 additions and 56 deletions

View File

@@ -15,12 +15,15 @@
*/
package difflib;
import java.util.*;
import difflib.myers.MyersDiff;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import difflib.myers.*;
/**
* Implements the difference and patching engine
*
@@ -140,6 +143,8 @@ public class DiffUtils {
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
rawChunk.add(new Object[] { tag, rest });
}
} else {
rawChunk.add(new Object[] {" ", ""});
}
}
}
@@ -184,47 +189,48 @@ public class DiffUtils {
List<String> ret = new ArrayList<String>();
ret.add("--- " + original);
ret.add("+++ " + revised);
// Hmm, I thought the Deltas were sorted already... turns out they're not.
List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() );
Collections.sort( patchDeltas, new Comparator<Delta>() {
public int compare( Delta a, Delta b ) {
return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() );
}
});
// code outside the if block also works for single-delta issues.
List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process
Delta delta = patchDeltas.get(0);
deltas.add(delta); // add the first Delta to the current set
// if there's more than 1 Delta, we may need to output them together
if (patchDeltas.size() > 1) {
for (int i = 1; i < patchDeltas.size(); i++) {
int position = delta.getOriginal().getPosition(); // store the current position of
// the first Delta
// Check if the next Delta is too close to the current position.
// And if it is, add it to the current set
Delta nextDelta = patchDeltas.get(i);
if ((position + delta.getOriginal().getSize() + contextSize) >=
(nextDelta.getOriginal().getPosition() - contextSize)) {
deltas.add(nextDelta);
} else {
// if it isn't, output the current set,
// then create a new set and add the current Delta to it.
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
ret.addAll(curBlock);
deltas.clear();
deltas.add(nextDelta);
if (!patch.getDeltas().isEmpty()) {
// Hmm, I thought the Deltas were sorted already... turns out they're not.
List<Delta> patchDeltas = new ArrayList<Delta>( patch.getDeltas() );
Collections.sort( patchDeltas, new Comparator<Delta>() {
public int compare( Delta a, Delta b ) {
return new Integer(a.getOriginal().getPosition()).compareTo( b.getOriginal().getPosition() );
}
delta = nextDelta;
});
// code outside the if block also works for single-delta issues.
List<Delta> deltas = new ArrayList<Delta>(); // current list of Delta's to process
Delta delta = patchDeltas.get(0);
deltas.add(delta); // add the first Delta to the current set
// if there's more than 1 Delta, we may need to output them together
if (patchDeltas.size() > 1) {
for (int i = 1; i < patchDeltas.size(); i++) {
int position = delta.getOriginal().getPosition(); // store the current position of
// the first Delta
// Check if the next Delta is too close to the current position.
// And if it is, add it to the current set
Delta nextDelta = patchDeltas.get(i);
if ((position + delta.getOriginal().getSize() + contextSize) >=
(nextDelta.getOriginal().getPosition() - contextSize)) {
deltas.add(nextDelta);
} else {
// if it isn't, output the current set,
// then create a new set and add the current Delta to it.
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
ret.addAll(curBlock);
deltas.clear();
deltas.add(nextDelta);
}
delta = nextDelta;
}
}
// don't forget to process the last set of Deltas
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
ret.addAll(curBlock);
}
// don't forget to process the last set of Deltas
List<String> curBlock = processDeltas(originalLines, deltas, contextSize);
ret.addAll(curBlock);
return ret;
}