mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Refactor: improve fileEqual impl
This commit is contained in:
parent
5f96fd024e
commit
e13cee4e46
@ -39,6 +39,12 @@ describe('fileEqual', () => {
|
|||||||
false
|
false
|
||||||
));
|
));
|
||||||
|
|
||||||
|
it('comment less', () => test(
|
||||||
|
['# A', '# B', 'B'],
|
||||||
|
['# A', 'B'],
|
||||||
|
false
|
||||||
|
));
|
||||||
|
|
||||||
it('larger', () => test(
|
it('larger', () => test(
|
||||||
['A', 'B'],
|
['A', 'B'],
|
||||||
['A', 'B', 'C'],
|
['A', 'B', 'C'],
|
||||||
@ -51,9 +57,15 @@ describe('fileEqual', () => {
|
|||||||
false
|
false
|
||||||
));
|
));
|
||||||
|
|
||||||
it('eol', () => test(
|
it('eol more', () => test(
|
||||||
['A', 'B'],
|
['A', 'B'],
|
||||||
['A', 'B', ''],
|
['A', 'B', ''],
|
||||||
true
|
false
|
||||||
|
));
|
||||||
|
|
||||||
|
it('eol less', () => test(
|
||||||
|
['A', 'B', ''],
|
||||||
|
['A', 'B'],
|
||||||
|
false
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|||||||
@ -11,75 +11,46 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const linesABound = linesA.length - 1;
|
const maxIndexA = linesA.length - 1;
|
||||||
|
|
||||||
let index = -1;
|
let index = -1;
|
||||||
|
|
||||||
let aLen = 0;
|
|
||||||
let bLen = 0;
|
|
||||||
|
|
||||||
for await (const lineB of source) {
|
for await (const lineB of source) {
|
||||||
index++;
|
index++;
|
||||||
|
|
||||||
if (index > linesABound) {
|
if (index > maxIndexA) {
|
||||||
return (index === linesA.length && lineB.length === 0);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lineA = linesA[index];
|
const lineA = linesA[index];
|
||||||
aLen = lineA.length;
|
|
||||||
bLen = lineB.length;
|
|
||||||
|
|
||||||
if (aLen === 0) {
|
const lineAIsComment = isCommentLine(lineA);
|
||||||
if (bLen === 0) {
|
const lineBIsComment = isCommentLine(lineB);
|
||||||
// both lines are empty, check next line
|
|
||||||
continue;
|
if (lineAIsComment !== lineBIsComment) {
|
||||||
}
|
|
||||||
// lineA is empty but lineB is not
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// now lineA can not be empty
|
|
||||||
if (bLen === 0) {
|
|
||||||
// lineB is empty but lineA is not
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now both lines can not be empty
|
// Now both line are either both comment or both not comment
|
||||||
|
// We only need to compare one of them
|
||||||
const firstCharA = lineA.charCodeAt(0);
|
if (lineAIsComment) {
|
||||||
const firstCharB = lineB.charCodeAt(0);
|
|
||||||
|
|
||||||
if (firstCharA !== firstCharB) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// now firstCharA is equal to firstCharB, we only need to check the first char
|
|
||||||
if (firstCharA === 35 /* # */) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// adguard conf
|
|
||||||
if (firstCharA === 33 /* ! */) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
firstCharA === 47 /* / */
|
|
||||||
&& lineA[1] === '/' && lineB[1] === '/'
|
|
||||||
&& lineA[3] === '#' && lineB[3] === '#'
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (aLen !== bLen) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lineA !== lineB) {
|
if (lineA !== lineB) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The file becomes larger
|
return index === maxIndexA;
|
||||||
return !(index < linesABound);
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user