From 6ff14d6fc7dc4d2966ea5af4855db5127ba898d2 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sun, 31 Oct 2021 14:34:55 +0100 Subject: [PATCH] Connection Buffer: Replace \r in run and score output with \n --- app/assets/javascripts/editor/evaluation.js | 1 - lib/runner/connection/buffer.rb | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/editor/evaluation.js b/app/assets/javascripts/editor/evaluation.js index 8f79ccf6..d3f8ed14 100644 --- a/app/assets/javascripts/editor/evaluation.js +++ b/app/assets/javascripts/editor/evaluation.js @@ -175,7 +175,6 @@ CodeOceanEditorEvaluation = { if (!msg.data || msg.data === "\r") { return; } - msg.data = msg.data.replace(/(\r)/gm, "\n"); var stream = {}; stream[msg.stream] = msg.data; this.printOutput(stream, true, 0); diff --git a/lib/runner/connection/buffer.rb b/lib/runner/connection/buffer.rb index c7dcba87..cbc8b4c1 100644 --- a/lib/runner/connection/buffer.rb +++ b/lib/runner/connection/buffer.rb @@ -3,7 +3,8 @@ class Runner::Connection::Buffer # The WebSocket connection might group multiple lines. For further processing, we require all lines # to be processed separately. Therefore, we split the lines by each newline character not part of an enclosed - # substring either in single or double quotes (e.g., within a JSON) + # substring either in single or double quotes (e.g., within a JSON). Originally, each line break consists of `\r\n`. + # We keep the `\r` at the end of the line (keeping "empty" lines) and replace it after buffering. # Inspired by https://stackoverflow.com/questions/13040585/split-string-by-spaces-properly-accounting-for-quotes-and-backslashes-ruby SPLIT_INDIVIDUAL_LINES = Regexp.compile(/(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*'|[^\n])+/) @@ -69,6 +70,9 @@ class Runner::Connection::Buffer def add_to_line_buffer(message) @buffering = false @global_buffer = +'' + # For our buffering, we identified line breaks with the `\n` and removed those temporarily. + # Thus, we now re-add the `\n` at the end of the string and remove the `\r` in the same time. + message = message.gsub(/\r$/, "\n") @line_buffer.push message end