From 25dc8c9951fe68c6d8ffe9035c7642969356d04b Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 15 Nov 2023 10:20:13 -0500 Subject: [PATCH] Revert "Revert to micropython's version of mp_hal_stdout_tx_strn_cooked" --- shared/runtime/stdout_helpers.c | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/shared/runtime/stdout_helpers.c b/shared/runtime/stdout_helpers.c index 6a24cc2eba..e32aa342e4 100644 --- a/shared/runtime/stdout_helpers.c +++ b/shared/runtime/stdout_helpers.c @@ -25,6 +25,8 @@ */ #include +#include +#include "py/mpconfig.h" #include "py/mphal.h" /* @@ -33,26 +35,25 @@ * implementation below can be used. */ +// CIRCUITPY-CHANGE: changes // Send "cooked" string of given length, where every occurrence of -// LF character is replaced with CR LF ("\n" is converted to "\r\n"). -// This is an optimised version to reduce the number of calls made -// to mp_hal_stdout_tx_strn. +// LF character is replaced with CR LF. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { - const char *last = str; - while (len--) { - if (*str == '\n') { - if (str > last) { - mp_hal_stdout_tx_strn(last, str - last); - } - mp_hal_stdout_tx_strn("\r\n", 2); - ++str; - last = str; - } else { - ++str; + bool last_cr = false; + while (len > 0) { + size_t i = 0; + if (str[0] == '\n' && !last_cr) { + mp_hal_stdout_tx_strn("\r", 1); + i = 1; } - } - if (str > last) { - mp_hal_stdout_tx_strn(last, str - last); + // Lump all characters on the next line together. + while ((last_cr || str[i] != '\n') && i < len) { + last_cr = str[i] == '\r'; + i++; + } + mp_hal_stdout_tx_strn(str, i); + str = &str[i]; + len -= i; } }