Revert "Revert to micropython's version of mp_hal_stdout_tx_strn_cooked"

This commit is contained in:
Dan Halbert 2023-11-15 10:20:13 -05:00 committed by GitHub
parent 58b111d428
commit 25dc8c9951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 17 deletions

View File

@ -25,6 +25,8 @@
*/ */
#include <string.h> #include <string.h>
#include <unistd.h>
#include "py/mpconfig.h"
#include "py/mphal.h" #include "py/mphal.h"
/* /*
@ -33,26 +35,25 @@
* 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 ("\n" is converted to "\r\n"). // LF character is replaced with CR LF.
// 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) {
const char *last = str; bool last_cr = false;
while (len--) { while (len > 0) {
if (*str == '\n') { size_t i = 0;
if (str > last) { if (str[0] == '\n' && !last_cr) {
mp_hal_stdout_tx_strn(last, str - last); mp_hal_stdout_tx_strn("\r", 1);
} i = 1;
mp_hal_stdout_tx_strn("\r\n", 2);
++str;
last = str;
} else {
++str;
} }
} // Lump all characters on the next line together.
if (str > last) { while ((last_cr || str[i] != '\n') && i < len) {
mp_hal_stdout_tx_strn(last, str - last); last_cr = str[i] == '\r';
i++;
}
mp_hal_stdout_tx_strn(str, i);
str = &str[i];
len -= i;
} }
} }