final StringBuilder builder = new StringBuilder();
// Iterate over all edits and all untouched line ends.
while (true) {
ReplaceEdit replaceEdit;
int nextEditPos = -1;
{ // Find next applicable edit. This is a potential cycle if we skip some changes.
if (editListPos < editList.length) {
if (editList[editListPos] instanceof ReplaceEdit == false) {
throw new RuntimeException();
}
replaceEdit = (ReplaceEdit) editList[editListPos];
nextEditPos = replaceEdit.getOffset();
} else {
replaceEdit = null;
}
}
// Choose what comes first: line end or edit.
boolean processLineEndNotEdit;
if (nextEditPos == -1) {
if (nextLineEndPos == -1) {
break;
} else {
processLineEndNotEdit = true;
}
} else {
if (nextLineEndPos == -1) {
processLineEndNotEdit = false;
} else {
processLineEndNotEdit = nextLineEndPos < nextEditPos;
}
}
if (processLineEndNotEdit) {
// Process next line end.
builder.append(sourceString.substring(sourceStringPos, nextLineEndPos + 1));
origPos.line++;
origPos.col = 0;
dstPos.line++;
dstPos.col = 0;
sourceStringPos = nextLineEndPos + 1;
nextLineEndPos = sourceString.indexOf(LINE_END_CHAR, sourceStringPos);
} else {
// Process next edit.
builder.append(sourceString.substring(sourceStringPos, nextEditPos));
origPos.col += nextEditPos - sourceStringPos;
dstPos.col += nextEditPos - sourceStringPos;
origPos.writeToArray(intBuffer);
dstPos.writeToArray(intBuffer);
// Count removed line ends.
if (replaceEdit.getLength() > 0) {
String removedString = sourceString.substring(replaceEdit.getOffset(),
replaceEdit.getOffset() + replaceEdit.getLength());
origPos.advanceToString(removedString);
}
// Count added line ends.
builder.append(replaceEdit.getText());
dstPos.advanceToString(replaceEdit.getText());
origPos.writeToArray(intBuffer);
dstPos.writeToArray(intBuffer);
sourceStringPos = nextEditPos + replaceEdit.getLength();
editListPos++;
if (nextLineEndPos != -1 && nextLineEndPos < sourceStringPos) {
nextLineEndPos = sourceString.indexOf(LINE_END_CHAR, sourceStringPos);
}