Skip to content

Commit 0330f0d

Browse files
committed
perf(util): ⚡️ cleanup line diff algorithm
1 parent 941dbd5 commit 0330f0d

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

src/util/diff/line.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -212,35 +212,30 @@ export const diff = (src: string[], dst: string[]): LinePatch => {
212212
const patch: LinePatch = [];
213213
let srcIdx = -1;
214214
let dstIdx = -1;
215+
const srcLength = src.length;
216+
const dstLength = dst.length;
215217
for (let i = 0; i < length; i++) {
216218
const line = lines[i];
217219
let lineLength = line.length;
218220
if (!lineLength) continue;
219221
const lastOp = line[lineLength - 1];
220222
const lastOpType = lastOp[0];
221223
const txt = lastOp[1];
222-
if (txt === "\n") {
223-
line.splice(lineLength - 1, 1);
224-
} else {
224+
if (txt === "\n") line.splice(lineLength - 1, 1);
225+
else {
225226
const strLength = txt.length;
226227
if (txt[strLength - 1] === "\n") {
227-
if (strLength === 1) {
228-
line.splice(lineLength - 1, 1);
229-
} else {
230-
lastOp[1] = txt.slice(0, strLength - 1);
231-
}
228+
if (strLength === 1) line.splice(lineLength - 1, 1);
229+
else lastOp[1] = txt.slice(0, strLength - 1);
232230
}
233231
}
234232
let lineType: LINE_PATCH_OP_TYPE = LINE_PATCH_OP_TYPE.EQL;
235233
lineLength = line.length;
236234
if (i + 1 === length) {
237-
if (srcIdx + 1 < src.length) {
238-
if (dstIdx + 1 < dst.length) {
239-
if (lineLength === 1 && line[0][0] === str.PATCH_OP_TYPE.EQL) {
240-
lineType = LINE_PATCH_OP_TYPE.EQL;
241-
} else {
242-
lineType = LINE_PATCH_OP_TYPE.MIX;
243-
}
235+
if (srcIdx + 1 < srcLength) {
236+
if (dstIdx + 1 < dstLength) {
237+
lineType = lineLength === 1 && line[0][0] === str.PATCH_OP_TYPE.EQL
238+
? LINE_PATCH_OP_TYPE.EQL : LINE_PATCH_OP_TYPE.MIX;
244239
srcIdx++;
245240
dstIdx++;
246241
} else {
@@ -254,29 +249,19 @@ export const diff = (src: string[], dst: string[]): LinePatch => {
254249
} else {
255250
const op = line[0];
256251
const type = op[0];
257-
if (lineLength === 1 && type === lastOpType) {
258-
if (type === str.PATCH_OP_TYPE.EQL) {
259-
srcIdx++;
260-
dstIdx++;
261-
} else if (type === str.PATCH_OP_TYPE.INS) {
262-
dstIdx++;
263-
lineType = LINE_PATCH_OP_TYPE.INS;
264-
} else if (type === str.PATCH_OP_TYPE.DEL) {
265-
srcIdx++;
266-
lineType = LINE_PATCH_OP_TYPE.DEL;
267-
}
268-
} else {
269-
if (lastOpType === str.PATCH_OP_TYPE.EQL) {
270-
lineType = LINE_PATCH_OP_TYPE.MIX;
271-
srcIdx++;
272-
dstIdx++;
273-
} else if (lastOpType === str.PATCH_OP_TYPE.INS) {
274-
lineType = LINE_PATCH_OP_TYPE.INS;
275-
dstIdx++;
276-
} else if (lastOpType === str.PATCH_OP_TYPE.DEL) {
277-
lineType = LINE_PATCH_OP_TYPE.DEL;
278-
srcIdx++;
279-
}
252+
if (lineLength === 1 && type === lastOpType && type === str.PATCH_OP_TYPE.EQL) {
253+
srcIdx++;
254+
dstIdx++;
255+
} else if (lastOpType === str.PATCH_OP_TYPE.EQL) {
256+
lineType = LINE_PATCH_OP_TYPE.MIX;
257+
srcIdx++;
258+
dstIdx++;
259+
} else if (lastOpType === str.PATCH_OP_TYPE.INS) {
260+
lineType = LINE_PATCH_OP_TYPE.INS;
261+
dstIdx++;
262+
} else if (lastOpType === str.PATCH_OP_TYPE.DEL) {
263+
lineType = LINE_PATCH_OP_TYPE.DEL;
264+
srcIdx++;
280265
}
281266
}
282267
patch.push([lineType, srcIdx, dstIdx, line]);

0 commit comments

Comments
 (0)