Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 81ba0ab

Browse files
committed
Avoid n^2 runtime on long lines
1 parent c8df99e commit 81ba0ab

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/arduino/arduino.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,18 @@ export class ArduinoApp {
756756
// Wrap line-oriented callbacks to accept arbitrary chunks of data.
757757
const wrapLineCallback = (callback: (line: string) => void) => {
758758
let buffer = "";
759+
let startIndex = 0;
759760
return (data: string) => {
760761
buffer += data;
761-
for (;;) {
762-
const pos = buffer.indexOf(os.EOL);
762+
while (true) {
763+
const pos = buffer.indexOf(os.EOL, startIndex);
763764
if (pos < 0) {
765+
startIndex = buffer.length;
764766
break;
765767
}
766768
const line = buffer.substring(0, pos + os.EOL.length);
767769
buffer = buffer.substring(pos + os.EOL.length);
770+
startIndex = 0;
768771
callback(line);
769772
}
770773
};

0 commit comments

Comments
 (0)