dont remove more lines than exist in the whole text

This commit is contained in:
webzwo0i 2014-12-04 16:01:39 +01:00
parent 89adbb9f12
commit 10c2f72720
1 changed files with 4 additions and 0 deletions

View File

@ -903,6 +903,8 @@ exports.pack = function (oldLen, newLen, opsStr, bank) {
* @params str {string} String to which a Changeset should be applied
*/
exports.applyToText = function (cs, str) {
var totalNrOfLines = str.split("\n").length;
var removedLines = 0;
var unpacked = exports.unpack(cs);
exports.assert(str.length == unpacked.oldLen, "mismatched apply: ", str.length, " / ", unpacked.oldLen);
var csIter = exports.opIterator(unpacked.ops);
@ -916,6 +918,7 @@ exports.applyToText = function (cs, str) {
assem.append(bankIter.take(op.chars));
break;
case '-':
removedLines += op.lines;
strIter.skip(op.chars);
break;
case '=':
@ -923,6 +926,7 @@ exports.applyToText = function (cs, str) {
break;
}
}
exports.assert(totalNrOfLines >= removedLines,"cannot remove ", removedLines, " lines from text with ", totalNrOfLines, " lines");
assem.append(strIter.take(strIter.remaining()));
return assem.toString();
};