Skip to content

Commit d0f9f6c

Browse files
committed
py: Fix pfenv_print_strn to return correct number of chars printed.
With this fix, all tests in tests/basics pass on pyboard.
1 parent 71d3112 commit d0f9f6c

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

py/pfenv.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
2929
int right_pad = 0;
3030
int pad = width - len;
3131
int pad_size;
32+
int total_chars_printed = 0;
3233
const char *pad_chars;
3334

3435
if (!fill || fill == ' ' ) {
@@ -53,7 +54,8 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
5354
left_pad = pad;
5455
}
5556

56-
if (left_pad) {
57+
if (left_pad > 0) {
58+
total_chars_printed += left_pad;
5759
while (left_pad > 0) {
5860
int p = left_pad;
5961
if (p > pad_size) {
@@ -64,7 +66,9 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
6466
}
6567
}
6668
pfenv->print_strn(pfenv->data, str, len);
67-
if (right_pad) {
69+
total_chars_printed += len;
70+
if (right_pad > 0) {
71+
total_chars_printed += right_pad;
6872
while (right_pad > 0) {
6973
int p = right_pad;
7074
if (p > pad_size) {
@@ -74,7 +78,7 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
7478
right_pad -= p;
7579
}
7680
}
77-
return len;
81+
return total_chars_printed;
7882
}
7983

8084
// 32-bits is 10 digits, add 3 for commas, 1 for sign, 1 for terminating null

stmhal/printf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void strn_print_strn(void *data, const char *str, unsigned int len) {
244244
strn_pfenv->cur += len;
245245
strn_pfenv->remain -= len;
246246
}
247-
247+
248248
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
249249
strn_pfenv_t strn_pfenv;
250250
strn_pfenv.cur = str;

0 commit comments

Comments
 (0)