MORE CODE REVIEW

This commit is contained in:
Vanilagy
2026-04-16 18:27:51 +02:00
parent b85ecd3109
commit 95e9dc5210
8 changed files with 72 additions and 93 deletions
-88
View File
@@ -388,91 +388,3 @@ export const readAllLines = (slice: FileSlice, length: number, options?: {
return lines;
};
export class LineReader {
getReader: () => MaybePromise<Reader>;
ignore?: (line: string) => boolean;
reader: Reader | null = null;
textDecoder = new TextDecoder();
currentLineNumber = 0; // 1-based
readPos = 0;
reachedEnd = false;
lineBuffer = '';
constructor(getReader: () => MaybePromise<Reader>, ignore?: (line: string) => boolean) {
this.getReader = getReader;
this.ignore = ignore;
}
readNextLine(): MaybePromise<string | null> {
if (this.reachedEnd) {
return null;
}
const line = this.extractLineFromBuffer();
if (line !== null) {
return line;
}
return (async () => {
if (!this.reader) {
let reader = this.getReader();
if (reader instanceof Promise) reader = await reader;
this.reader = reader;
}
while (true) {
let slice = this.reader.requestSliceRange(this.readPos, 0, 1024);
if (slice instanceof Promise) slice = await slice;
if (!slice || slice.length === 0) {
this.reachedEnd = true;
const line = this.lineBuffer.trim();
this.lineBuffer = '';
if (line) {
this.currentLineNumber++;
}
if (!line || this.ignore?.(line)) {
return null;
}
return line;
}
const bytes = readBytes(slice, slice.length);
this.readPos += bytes.length;
this.lineBuffer += this.textDecoder.decode(bytes, { stream: true });
const line = this.extractLineFromBuffer();
if (line !== null) {
return line;
}
}
})();
}
extractLineFromBuffer() {
assert(!this.reachedEnd);
while (true) {
const newlineIndex = this.lineBuffer.indexOf('\n');
if (newlineIndex === -1) {
return null;
}
const line = this.lineBuffer.slice(0, newlineIndex).trim();
this.lineBuffer = this.lineBuffer.slice(newlineIndex + 1);
this.currentLineNumber++;
if (this.ignore?.(line)) {
continue;
}
return line;
}
}
}