1
0
mirror of https://github.com/rfivet/stm32bringup.git synced 2024-12-23 17:16:22 -05:00

zero padding and uppercase/lowercase for hexadecimal.

This commit is contained in:
Renaud 2020-12-17 11:03:48 +08:00
parent 788c5c5b3b
commit d2617fbb0b

View File

@ -5,18 +5,25 @@
#include <stdio.h> #include <stdio.h>
#include "system.h" /* kputc(), kputs() */ #include "system.h" /* kputc(), kputs() */
static int kputu( unsigned u, unsigned d) { static int kputu( unsigned u, unsigned d, int padlen, char fmt) {
char s[ 12] ; /* room for 11 octal digit + EOS */ char s[ 12] ; /* room for 11 octal digit + EOS */
char *p = &s[ sizeof s - 1] ; /* point to last byte */ char *p = &s[ sizeof s - 1] ; /* point to last byte */
*p = 0 ; /* null terminated string */ *p = 0 ; /* null terminated string */
fmt = fmt & 0x20 ; /* set uppercase bit */
do { do {
unsigned r = u % d ; unsigned r = u % d ;
u /= d ; u /= d ;
*--p = "0123456789ABCDEF"[ r] ; *--p = "0123456789ABCDEF"[ r] | fmt ;
padlen -= 1 ;
} while( u) ; } while( u) ;
return kputs( p) ; while( padlen-- > 0) {
kputc( '0') ;
u += 1 ; /* use u to calculate output length */
}
return u + kputs( p) ;
} }
static int kputi( int i) { static int kputi( int i) {
@ -26,7 +33,7 @@ static int kputi( int i) {
kputc( '-') ; kputc( '-') ;
} }
return flag + kputu( i, 10) ; return flag + kputu( i, 10, 0, 'u') ;
} }
int printf( const char *fmt, ...) { int printf( const char *fmt, ...) {
@ -38,23 +45,31 @@ int printf( const char *fmt, ...) {
while( ( c = *fmt++) != 0) while( ( c = *fmt++) != 0)
if( c != '%') { if( c != '%') {
cnt += 1 ; kputc( c) ; cnt += 1 ; kputc( c) ;
} else if( ( c = *fmt++) == 0) { } else {
cnt += 1 ; kputc( '%') ; int len = 0 ;
c = *fmt++ ;
while( c >= '0' && c <= '9') {
len = len * 10 + ( c - '0') ;
c = *fmt++ ;
}
if( c == 0)
break ; break ;
} else
switch( c) { switch( c) {
case 'c': case 'c':
cnt += 1 ; kputc( va_arg( ap, int /* char */)) ; cnt += 1 ; kputc( va_arg( ap, int /* char */)) ;
break ; break ;
case 'o': case 'o':
cnt += kputu( va_arg( ap, unsigned), 8) ; cnt += kputu( va_arg( ap, unsigned), 8, len, c) ;
break ; break ;
case 'u': case 'u':
cnt += kputu( va_arg( ap, unsigned), 10) ; cnt += kputu( va_arg( ap, unsigned), 10, len, c) ;
break ; break ;
case 'x': case 'x':
case 'X': case 'X':
cnt += kputu( va_arg( ap, unsigned), 16) ; cnt += kputu( va_arg( ap, unsigned), 16, len, c) ;
break ; break ;
case 'i': case 'i':
case 'd': case 'd':
@ -69,6 +84,7 @@ int printf( const char *fmt, ...) {
case '%': case '%':
cnt += 1 ; kputc( c) ; cnt += 1 ; kputc( c) ;
} }
}
va_end( ap) ; va_end( ap) ;
return cnt ; return cnt ;