in src/client/common/editor.ts [273:351]
function patch_fromText(textline: string): Patch[] {
const patches: Patch[] = [];
if (!textline) {
return patches;
}
// Start Modification by Don Jayamanne 24/06/2016 Support for CRLF
const text = textline.split(/[\r\n]/);
// End Modification
let textPointer = 0;
const patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
while (textPointer < text.length) {
const m = text[textPointer].match(patchHeader);
if (!m) {
throw new Error(`Invalid patch string: ${text[textPointer]}`);
}
const patch = new (<any>diff_match_patch).patch_obj();
patches.push(patch);
patch.start1 = parseInt(m[1], 10);
if (m[2] === '') {
patch.start1 -= 1;
patch.length1 = 1;
} else if (m[2] === '0') {
patch.length1 = 0;
} else {
patch.start1 -= 1;
patch.length1 = parseInt(m[2], 10);
}
patch.start2 = parseInt(m[3], 10);
if (m[4] === '') {
patch.start2 -= 1;
patch.length2 = 1;
} else if (m[4] === '0') {
patch.length2 = 0;
} else {
patch.start2 -= 1;
patch.length2 = parseInt(m[4], 10);
}
textPointer += 1;
const dmp = require('diff-match-patch') as typeof import('diff-match-patch');
while (textPointer < text.length) {
const sign = text[textPointer].charAt(0);
let line: string;
try {
//var line = decodeURI(text[textPointer].substring(1));
// For some reason the patch generated by python files don't encode any characters
// And this patch module (code from Google) is expecting the text to be encoded!!
// Temporary solution, disable decoding
// Issue #188
line = text[textPointer].substring(1);
} catch (ex) {
// Malformed URI sequence.
throw new Error('Illegal escape in patch_fromText');
}
if (sign === '-') {
// Deletion.
patch.diffs.push([dmp.DIFF_DELETE, line]);
} else if (sign === '+') {
// Insertion.
patch.diffs.push([dmp.DIFF_INSERT, line]);
} else if (sign === ' ') {
// Minor equality.
patch.diffs.push([dmp.DIFF_EQUAL, line]);
} else if (sign === '@') {
// Start of next patch.
break;
} else if (sign === '') {
// Blank line? Whatever.
} else {
throw new Error(`Invalid patch mode '${sign}' in: ${line}`);
}
textPointer += 1;
}
}
return patches;
}