Skip to content

Commit

Permalink
ptimize %k output
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 29, 2020
1 parent 1987ac4 commit 703527a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/additional_format_specifiers.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ additional_format_specifiers(void) {
lwprintf_printf("%*k\r\n", sizeof(my_array) / sizeof(my_array[0]), my_array);
/* Variable length with uppercase letters and spaces, outputs "01 02 A4 B5 C6" */
lwprintf_printf("% *K\r\n", sizeof(my_array) / sizeof(my_array[0]), my_array);
/* Variable length with uppercase letters and spaces, outputs "01 02 a4 b5 c6" */
lwprintf_printf("% *k\r\n", sizeof(my_array) / sizeof(my_array[0]), my_array);
}
8 changes: 5 additions & 3 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,12 @@ prv_format(lwprintf_int_t* p, va_list arg) {
unsigned char* ptr = (void *)va_arg(arg, unsigned char *); /* Get input parameter as unsigned char pointer */
int len = p->m.width;

if (ptr == NULL || len == 0) {
break;
}

p->m.flags.uc = *fmt == 'K'; /* Set if uppercase */
p->m.flags.precision = 1; /* We use precision flag */
p->m.precision = 2; /* 2 bytes output, 0 prepend if necessary */
p->m.flags.zero = 1; /* Prepend with zeros */
p->m.flags.zero = 1; /* Prepend with zeros if necessary */
p->m.width = 2; /* Each number is 2 chars min/max */
p->m.base = 16; /* Hex format */

Expand Down

0 comments on commit 703527a

Please sign in to comment.