8000 py/mpprint: Support "%lx" format on 64-bit systems. · lvgl/lv_micropython@5a10e63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a10e63

Browse files
committed
py/mpprint: Support "%lx" format on 64-bit systems.
Before that, the output was truncated to 32 bits. Only "%x" format is handled, because a typical use is for addresses. This refactor actually decreased x86_64 code size by 30 bytes.
1 parent 5f8ad28 commit 5a10e63

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

py/mpprint.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,16 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
446446
}
447447
}
448448

449-
// parse long specifiers (current not used)
450-
//bool long_arg = false;
449+
// parse long specifiers (only for LP64 model where they make a difference)
450+
#ifndef __LP64__
451+
const
452+
#endif
453+
bool long_arg = false;
451454
if (*fmt == 'l') {
452455
++fmt;
453-
//long_arg = true;
456+
#ifdef __LP64__
457+
long_arg = true;
458+
#endif
454459
}
455460

456461
if (*fmt == '\0') {
@@ -505,11 +510,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
505510
chrs += mp_print_int(print, va_arg(args, int), 1, 10, 'a', flags, fi AC5F ll, width);
506511
break;
507512
case 'x':
508-
chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'a', flags, fill, width);
509-
break;
510-
case 'X':
511-
chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'A', flags, fill, width);
513+
case 'X': {
514+
char fmt_c = 'x' - *fmt + 'A';
515+
mp_uint_t val;
516+
if (long_arg) {
517+
val = va_arg(args, unsigned long int);
518+
} else {
519+
val = va_arg(args, unsigned int);
520+
}
521+
chrs += mp_print_int(print, val, 0, 16, fmt_c, flags, fill, width);
512522
break;
523+
}
513524
case 'p':
514525
case 'P': // don't bother to handle upcase for 'P'
515526
// Use unsigned long int to work on both ILP32 and LP64 systems

0 commit comments

Comments
 (0)
0