From 9be66afce76bd50851411508579a69e8be94cfb2 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 19 Mar 2025 23:56:52 +0800 Subject: [PATCH] Perf: simplify file equal --- Build/lib/create-file.test.ts | 16 ++++++++++++++-- Build/lib/create-file.ts | 24 +++++++++--------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Build/lib/create-file.test.ts b/Build/lib/create-file.test.ts index 46cd42ef..7d1538aa 100644 --- a/Build/lib/create-file.test.ts +++ b/Build/lib/create-file.test.ts @@ -57,15 +57,27 @@ describe('fileEqual', () => { false )); - it('eol more', () => test( + it('eol more #1', () => test( ['A', 'B'], ['A', 'B', ''], false )); - it('eol less', () => test( + it('eol more #2', () => test( + ['A', 'B', ''], + ['A', 'B', '', ''], + false + )); + + it('eol less #1', () => test( ['A', 'B', ''], ['A', 'B'], false )); + + it('eol less #2', () => test( + ['A', 'B', '', ''], + ['A', 'B', ''], + false + )); }); diff --git a/Build/lib/create-file.ts b/Build/lib/create-file.ts index 7582dca2..28904b0c 100644 --- a/Build/lib/create-file.ts +++ b/Build/lib/create-file.ts @@ -17,22 +17,24 @@ export async function fileEqual(linesA: string[], source: AsyncIterable for await (const lineB of source) { index++; + // b become bigger if (index > maxIndexA) { return false; } const lineA = linesA[index]; - const lineAIsComment = isCommentLine(lineA); - const lineBIsComment = isCommentLine(lineB); - - if (lineAIsComment !== lineBIsComment) { + const aFirstChar = lineA.charCodeAt(0); + if (aFirstChar !== lineB.charCodeAt(0)) { 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 - if (lineAIsComment) { + if ( + aFirstChar === 35 // # + || aFirstChar === 33 // ! + ) { continue; } @@ -41,18 +43,10 @@ export async function fileEqual(linesA: string[], source: AsyncIterable } } + // b is not smaller than a 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) { const linesALen = linesA.length;