mirror of
https://github.com/rfivet/stm32bringup.git
synced 2024-12-18 06:46:23 -05:00
zero padding and uppercase/lowercase for hexadecimal.
This commit is contained in:
parent
788c5c5b3b
commit
d2617fbb0b
38
printf.c
38
printf.c
@ -5,18 +5,25 @@
|
||||
#include <stdio.h>
|
||||
#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 *p = &s[ sizeof s - 1] ; /* point to last byte */
|
||||
|
||||
*p = 0 ; /* null terminated string */
|
||||
fmt = fmt & 0x20 ; /* set uppercase bit */
|
||||
do {
|
||||
unsigned r = u % d ;
|
||||
u /= d ;
|
||||
*--p = "0123456789ABCDEF"[ r] ;
|
||||
*--p = "0123456789ABCDEF"[ r] | fmt ;
|
||||
padlen -= 1 ;
|
||||
} 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) {
|
||||
@ -26,7 +33,7 @@ static int kputi( int i) {
|
||||
kputc( '-') ;
|
||||
}
|
||||
|
||||
return flag + kputu( i, 10) ;
|
||||
return flag + kputu( i, 10, 0, 'u') ;
|
||||
}
|
||||
|
||||
int printf( const char *fmt, ...) {
|
||||
@ -38,23 +45,31 @@ int printf( const char *fmt, ...) {
|
||||
while( ( c = *fmt++) != 0)
|
||||
if( c != '%') {
|
||||
cnt += 1 ; kputc( c) ;
|
||||
} else if( ( c = *fmt++) == 0) {
|
||||
cnt += 1 ; kputc( '%') ;
|
||||
break ;
|
||||
} else
|
||||
} else {
|
||||
int len = 0 ;
|
||||
|
||||
c = *fmt++ ;
|
||||
while( c >= '0' && c <= '9') {
|
||||
len = len * 10 + ( c - '0') ;
|
||||
c = *fmt++ ;
|
||||
}
|
||||
|
||||
if( c == 0)
|
||||
break ;
|
||||
|
||||
switch( c) {
|
||||
case 'c':
|
||||
cnt += 1 ; kputc( va_arg( ap, int /* char */)) ;
|
||||
break ;
|
||||
case 'o':
|
||||
cnt += kputu( va_arg( ap, unsigned), 8) ;
|
||||
cnt += kputu( va_arg( ap, unsigned), 8, len, c) ;
|
||||
break ;
|
||||
case 'u':
|
||||
cnt += kputu( va_arg( ap, unsigned), 10) ;
|
||||
cnt += kputu( va_arg( ap, unsigned), 10, len, c) ;
|
||||
break ;
|
||||
case 'x':
|
||||
case 'X':
|
||||
cnt += kputu( va_arg( ap, unsigned), 16) ;
|
||||
cnt += kputu( va_arg( ap, unsigned), 16, len, c) ;
|
||||
break ;
|
||||
case 'i':
|
||||
case 'd':
|
||||
@ -69,6 +84,7 @@ int printf( const char *fmt, ...) {
|
||||
case '%':
|
||||
cnt += 1 ; kputc( c) ;
|
||||
}
|
||||
}
|
||||
|
||||
va_end( ap) ;
|
||||
return cnt ;
|
||||
|
Loading…
Reference in New Issue
Block a user