Skip to content

Commit 4d93849

Browse files
authored
Merge pull request #8614 from adafruit/revert-8599-use-utils-stdout-helpers
Revert "Revert to micropython's version of mp_hal_stdout_tx_strn_cooked"
2 parents d1ddce7 + 25dc8c9 commit 4d93849

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

shared/runtime/stdout_helpers.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626

2727
#include <string.h>
28+
#include <unistd.h>
29+
#include "py/mpconfig.h"
2830
#include "py/mphal.h"
2931

3032
/*
@@ -33,26 +35,25 @@
3335
* implementation below can be used.
3436
*/
3537

38+
// CIRCUITPY-CHANGE: changes
3639
// Send "cooked" string of given length, where every occurrence of
37-
// LF character is replaced with CR LF ("\n" is converted to "\r\n").
38-
// This is an optimised version to reduce the number of calls made
39-
// to mp_hal_stdout_tx_strn.
40+
// LF character is replaced with CR LF.
4041
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
41-
const char *last = str;
42-
while (len--) {
43-
if (*str == '\n') {
44-
if (str > last) {
45-
mp_hal_stdout_tx_strn(last, str - last);
46-
}
47-
mp_hal_stdout_tx_strn("\r\n", 2);
48-
++str;
49-
last = str;
50-
} else {
51-
++str;
42+
bool last_cr = false;
43+
while (len > 0) {
44+
size_t i = 0;
45+
if (str[0] == '\n' && !last_cr) {
46+
mp_hal_stdout_tx_strn("\r", 1);
47+
i = 1;
5248
}
53-
}
54-
if (str > last) {
55-
mp_hal_stdout_tx_strn(last, str - last);
49+
// Lump all characters on the next line together.
50+
while ((last_cr || str[i] != '\n') && i < len) {
51+
last_cr = str[i] == '\r';
52+
i++;
53+
}
54+
mp_hal_stdout_tx_strn(str, i);
55+
str = &str[i];
56+
len -= i;
5657
}
5758
}
5859

0 commit comments

Comments
 (0)