diff --git a/io/bufio.ts b/io/bufio.ts index d4d13ebf6c48..e47023029d13 100644 --- a/io/bufio.ts +++ b/io/bufio.ts @@ -621,7 +621,7 @@ function createLPS(pat: Uint8Array): Uint8Array { lps[i] = 0; i++; } else { - prefixEnd = pat[prefixEnd - 1]; + prefixEnd = lps[prefixEnd - 1]; } } return lps; @@ -641,8 +641,6 @@ export async function* readDelim( // Modified KMP let inspectIndex = 0; let matchIndex = 0; - let itr = chunks.iterator(); - let curr = -1; while (true) { const inspectArr = new Uint8Array(bufSize); const result = await reader.read(inspectArr); @@ -655,13 +653,11 @@ export async function* readDelim( return; } chunks.add(inspectArr, 0, result); - if (inspectIndex === 0 && chunks.size() > 0) { - curr = itr.next().value; - } + let localIndex = 0; while (inspectIndex < chunks.size()) { - if (curr === delim[matchIndex]) { - curr = itr.next().value; + if (inspectArr[localIndex] === delim[matchIndex]) { inspectIndex++; + localIndex++; matchIndex++; if (matchIndex === delimLen) { // Full match @@ -672,13 +668,11 @@ export async function* readDelim( chunks.shift(inspectIndex); inspectIndex = 0; matchIndex = 0; - itr = chunks.iterator(); - curr = itr.next().value; } } else { if (matchIndex === 0) { inspectIndex++; - curr = itr.next().value; + localIndex++; } else { matchIndex = delimLPS[matchIndex - 1]; } diff --git a/io/bufio_test.ts b/io/bufio_test.ts index 927ca09bf65f..e2d057ba9173 100644 --- a/io/bufio_test.ts +++ b/io/bufio_test.ts @@ -243,6 +243,52 @@ Deno.test("bufioReadLine", async function () { await testReadLine(testInputrn); }); +Deno.test("[io] readStringDelim basic", async () => { + const delim = "!#$%&()=~"; + const exp = [ + "", + "a", + "bc", + "def", + "", + "!", + "!#", + "!#$%&()=", + "#$%&()=~", + "", + "", + ]; + const str = exp.join(delim); + const arr: string[] = []; + for await (const v of readStringDelim(new StringReader(str), delim)) { + arr.push(v); + } + assertEquals(arr, exp); +}); + +Deno.test("[io] readStringDelim bigger delim than buf size", async () => { + // 0123456789... + const delim = Array.from({ length: 1025 }).map((_, i) => i % 10).join(""); + const exp = ["", "a", "bc", "def", "01", "012345678", "123456789", "", ""]; + const str = exp.join(delim); + const arr: string[] = []; + for await (const v of readStringDelim(new StringReader(str), delim)) { + arr.push(v); + } + assertEquals(arr, exp); +}); + +Deno.test("[io] readStringDelim delim=1213", async () => { + const delim = "1213"; + const exp = ["", "a", "bc", "def", "01", "012345678", "123456789", "", ""]; + const str = exp.join(delim); + const arr: string[] = []; + for await (const v of readStringDelim(new StringReader(str), "1213")) { + arr.push(v); + } + assertEquals(arr, exp); +}); + Deno.test("bufioPeek", async function () { const decoder = new TextDecoder(); const p = new Uint8Array(10);