diff --git a/shared/runtime/stdout_helpers.c b/shared/runtime/stdout_helpers.c index e32aa342e4..6a24cc2eba 100644 --- a/shared/runtime/stdout_helpers.c +++ b/shared/runtime/stdout_helpers.c @@ -25,8 +25,6 @@ */ #include -#include -#include "py/mpconfig.h" #include "py/mphal.h" /* @@ -35,25 +33,26 @@ * 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. +// 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. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { - 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; + 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; } - // 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; + } + if (str > last) { + mp_hal_stdout_tx_strn(last, str - last); } }