Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(io): readDelim returns wrong result if delim strides over chunks #877

Merged
merged 7 commits into from
May 14, 2021
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
16 changes: 5 additions & 11 deletions io/bufio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ function createLPS(pat: Uint8Array): Uint8Array {
lps[i] = 0;
i++;
} else {
prefixEnd = pat[prefixEnd - 1];
prefixEnd = lps[prefixEnd - 1];
}
}
return lps;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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];
}
Expand Down
46 changes: 46 additions & 0 deletions io/bufio_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down