Revert to micropython's version of mp_hal_stdout_tx_strn_cooked

Closes #8510
This commit is contained in:
Jeff Epler 2023-11-13 10:00:53 -06:00
parent 166a98fa34
commit 06ee8daab9
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
1 changed files with 17 additions and 18 deletions

View File

@ -25,8 +25,6 @@
*/ */
#include <string.h> #include <string.h>
#include <unistd.h>
#include "py/mpconfig.h"
#include "py/mphal.h" #include "py/mphal.h"
/* /*
@ -35,25 +33,26 @@
* implementation below can be used. * implementation below can be used.
*/ */
// CIRCUITPY-CHANGE: changes
// Send "cooked" string of given length, where every occurrence of // 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) { void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
bool last_cr = false; const char *last = str;
while (len > 0) { while (len--) {
size_t i = 0; if (*str == '\n') {
if (str[0] == '\n' && !last_cr) { if (str > last) {
mp_hal_stdout_tx_strn("\r", 1); mp_hal_stdout_tx_strn(last, str - last);
i = 1; }
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) { if (str > last) {
last_cr = str[i] == '\r'; mp_hal_stdout_tx_strn(last, str - last);
i++;
}
mp_hal_stdout_tx_strn(str, i);
str = &str[i];
len -= i;
} }
} }