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: problem with multibyte characters in julia engine #9740

Merged
merged 6 commits into from
Jun 4, 2024
Merged
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
50 changes: 35 additions & 15 deletions src/execute/julia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,16 +600,26 @@ async function writeJuliaCommand(
// but they could also end in a partial one.
// so to read and process them all correctly, we read in a fixed number of bytes, if there's a newline, we process
// the string up to that part and save the rest for the next round.
let restOfPreviousResponse = "";
let restOfPreviousResponse = new Uint8Array(512);
let restLength = 0; // number of valid bytes in restOfPreviousResponse
while (true) {
let response = restOfPreviousResponse;
const newlineAt = response.indexOf("\n");
const respArray: Uint8Array[] = [];
let respLength = 0;
let response = "";
const newlineAt = restOfPreviousResponse.indexOf(10);
cscheid marked this conversation as resolved.
Show resolved Hide resolved
// if we already have a newline, we don't need to read from conn
if (newlineAt !== -1) {
restOfPreviousResponse = response.substring(newlineAt + 1);
response = response.substring(0, newlineAt);
if (newlineAt !== -1 && newlineAt < restLength) {
response = new TextDecoder().decode(
restOfPreviousResponse.slice(0, newlineAt),
);
restOfPreviousResponse.set(
restOfPreviousResponse.slice(newlineAt + 1, restLength),
);
restLength -= newlineAt + 1;
} // but if we don't have a newline, we read in more until we get one
else {
respArray.push(restOfPreviousResponse.slice(0, restLength));
respLength += restLength;
while (true) {
const buffer = new Uint8Array(512);
const bytesRead = await conn.read(buffer);
Expand All @@ -618,18 +628,28 @@ async function writeJuliaCommand(
}

if (bytesRead > 0) {
const payload = new TextDecoder().decode(
buffer.slice(0, bytesRead),
);
const payloadNewlineAt = payload.indexOf("\n");
if (payloadNewlineAt === -1) {
response += payload;
restOfPreviousResponse = "";
const bufferNewlineAt = buffer.indexOf(10);
if (bufferNewlineAt === -1 || bufferNewlineAt >= bytesRead) {
respArray.push(buffer.slice(0, bytesRead));
restLength = 0;
respLength += bytesRead;
} else {
response += payload.substring(0, payloadNewlineAt);
restOfPreviousResponse = payload.substring(payloadNewlineAt + 1);
respArray.push(buffer.slice(0, bufferNewlineAt));
respLength += bufferNewlineAt;
restOfPreviousResponse.set(
buffer.slice(bufferNewlineAt + 1, bytesRead),
);
restLength = bytesRead - bufferNewlineAt - 1;
// when we have found a newline in a payload, we can stop reading in more data and continue
// with the response first

let respBuffer = new Uint8Array(respLength);
let offset = 0;
respArray.forEach((item) => {
respBuffer.set(item, offset);
offset += item.length;
});
response = new TextDecoder().decode(respBuffer);
break;
}
}
Expand Down
Loading