From 61ec5bd5b785f3ff45eb733bf388af7296ddd75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 14 Mar 2022 09:59:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20GCV:=20Fix=20pending=20file=20po?= =?UTF-8?q?sition=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we strip \r from \r\n, our line length and thus file position calculation will be off by one. We'll just leave it in as it doesn't influence our parsing and makes things way easier. Thanks to @JoveToo for the heads-up on Discord. --- .../gcodeviewer/static/js/viewer/reader.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/octoprint/plugins/gcodeviewer/static/js/viewer/reader.js b/src/octoprint/plugins/gcodeviewer/static/js/viewer/reader.js index de056f2f9..bb15ba3d7 100644 --- a/src/octoprint/plugins/gcodeviewer/static/js/viewer/reader.js +++ b/src/octoprint/plugins/gcodeviewer/static/js/viewer/reader.js @@ -54,7 +54,7 @@ GCODE.gCodeReader = (function () { byteCount = 0; for (i = 0; i < lines.length; i++) { - byteCount += lines[i].length + 1; // line length + line ending + byteCount += lines[i].length + 1; // line length + '\n' gcode.push({line: lines[i], percentage: (byteCount * 100) / totalSize}); } lines = []; @@ -188,8 +188,21 @@ GCODE.gCodeReader = (function () { this.clear(); var totalSize = reader.target.result.length; - // With a regex split below as previous, memory usage is huge & takes longer. - lines = reader.target.result.replace("\r\n", "\n").split("\n"); + + /* + * Split by line ending + * + * Note that for windows line endings \r\n this leaves the \r attached to + * the lines. That will not influence our parser, but makes file position + * calculation way easier (line length + 1), so we just leave it in. + * + * This cannot cope with old MacOS \r line endings, but those should + * really not be used anymore and thus we'll happily ignore them here. + * + * A simple string split uses up *much* less memory than regex. + */ + lines = reader.target.result.split("\n"); + reader.target.result = null; prepareGCode(totalSize);