Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/input/line-buffer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {StringDecoder} from 'node:string_decoder';

export class LineBuffer {
readonly #decoder = new StringDecoder('utf8');
#remainder = '';

push(chunk: string | Buffer): string[] {
const text = this.#remainder + chunk.toString('utf8');
const decoded = typeof chunk === 'string' ? chunk : this.#decoder.write(chunk);
const text = this.#remainder + decoded;
const parts = text.split(/\r?\n/);
this.#remainder = parts.pop() ?? '';
return parts;
}

flush(): string[] {
if (this.#remainder.length === 0) {
const finalLine = this.#remainder + this.#decoder.end();
this.#remainder = '';
if (finalLine.length === 0) {
return [];
}

const finalLine = this.#remainder;
this.#remainder = '';
return [finalLine];
}
}
8 changes: 8 additions & 0 deletions tests/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ describe('LineBuffer', () => {
expect(buffer.flush()).toEqual(['three']);
});

it('keeps multibyte characters that are split across chunks', () => {
const buffer = new LineBuffer();
const bytes = Buffer.from('café ✓\n', 'utf8');
expect(buffer.push(bytes.subarray(0, 4))).toEqual([]);
expect(buffer.push(bytes.subarray(4, 7))).toEqual([]);
expect(buffer.push(bytes.subarray(7))).toEqual(['café ✓']);
});

it('does not invent a line when the buffer is empty', () => {
expect(new LineBuffer().flush()).toEqual([]);
});
Expand Down
Loading