From 077af74671afdd22771656e45ef243f3dc526305 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sun, 24 Sep 2023 16:29:08 +0200 Subject: [PATCH] Connection::Buffer: Restore buffering for lines ending with CR With the turtle graphics, some very long lines might be returned by Poseidon just after the CR (\r). In this case, our new normalization will fix the mistake, but the next message received (just a LF `\n`) will be forwarded to the clients unchanged. This is not desired, so that we filter this case separately. --- lib/runner/connection/buffer.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/runner/connection/buffer.rb b/lib/runner/connection/buffer.rb index d6e15e48..f7567c8e 100644 --- a/lib/runner/connection/buffer.rb +++ b/lib/runner/connection/buffer.rb @@ -74,7 +74,16 @@ class Runner::Connection::Buffer # First, we ensure line endings are only represented by `\n`, regardless of the original line ending. # Then, we convert all line endings to `\r\n` to ensure we can identify the `\r` at the end of a line. # This "double conversion" is required to prevent line endings with \r\r\n. - string.encode(universal_newline: true).encode(crlf_newline: true) + normalized = string.encode(universal_newline: true).encode(crlf_newline: true) + + # If the original input string ends with `\r`, it is incomplete and needs buffering. + # However, through our above normalization, the string would not end with `\r` anymore (but `\r\n`). + # Hence, in this case, we just remove the last character, so that all other line endings within the string remain unchanged. + if string.ends_with?("\r") + normalized[...-1] + else + normalized + end end def add_to_line_buffer(message)