mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Perf: simplify file equal
This commit is contained in:
@@ -57,15 +57,27 @@ describe('fileEqual', () => {
|
|||||||
false
|
false
|
||||||
));
|
));
|
||||||
|
|
||||||
it('eol more', () => test(
|
it('eol more #1', () => test(
|
||||||
['A', 'B'],
|
['A', 'B'],
|
||||||
['A', 'B', ''],
|
['A', 'B', ''],
|
||||||
false
|
false
|
||||||
));
|
));
|
||||||
|
|
||||||
it('eol less', () => test(
|
it('eol more #2', () => test(
|
||||||
|
['A', 'B', ''],
|
||||||
|
['A', 'B', '', ''],
|
||||||
|
false
|
||||||
|
));
|
||||||
|
|
||||||
|
it('eol less #1', () => test(
|
||||||
['A', 'B', ''],
|
['A', 'B', ''],
|
||||||
['A', 'B'],
|
['A', 'B'],
|
||||||
false
|
false
|
||||||
));
|
));
|
||||||
|
|
||||||
|
it('eol less #2', () => test(
|
||||||
|
['A', 'B', '', ''],
|
||||||
|
['A', 'B', ''],
|
||||||
|
false
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,22 +17,24 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
|
|||||||
for await (const lineB of source) {
|
for await (const lineB of source) {
|
||||||
index++;
|
index++;
|
||||||
|
|
||||||
|
// b become bigger
|
||||||
if (index > maxIndexA) {
|
if (index > maxIndexA) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lineA = linesA[index];
|
const lineA = linesA[index];
|
||||||
|
|
||||||
const lineAIsComment = isCommentLine(lineA);
|
const aFirstChar = lineA.charCodeAt(0);
|
||||||
const lineBIsComment = isCommentLine(lineB);
|
if (aFirstChar !== lineB.charCodeAt(0)) {
|
||||||
|
|
||||||
if (lineAIsComment !== lineBIsComment) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now both line are either both comment or both not comment
|
// Now both line has the same first char
|
||||||
// We only need to compare one of them
|
// We only need to compare one of them
|
||||||
if (lineAIsComment) {
|
if (
|
||||||
|
aFirstChar === 35 // #
|
||||||
|
|| aFirstChar === 33 // !
|
||||||
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,18 +43,10 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// b is not smaller than a
|
||||||
return index === maxIndexA;
|
return index === maxIndexA;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCommentLine(line: string): boolean {
|
|
||||||
const firstChar = line.charCodeAt(0);
|
|
||||||
return (
|
|
||||||
firstChar === 35 // #
|
|
||||||
|| firstChar === 33 // !
|
|
||||||
|| (firstChar === 47 && line[1] === '/' && line[3] === '#') // //##
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
||||||
const linesALen = linesA.length;
|
const linesALen = linesA.length;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user