diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2022-05-20 09:13:49 +0000 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2022-05-20 09:13:49 +0000 |
commit | 7bde6411b1b82cafd997df8ac222e82280a6e786 (patch) | |
tree | 8baebc91fab07c90962f7cfa0be254ac407bde7e | |
parent | 79610f324861f32fff88ac34c97e6bc5a7f3bda8 (diff) | |
download | compilertests-7bde6411b1b82cafd997df8ac222e82280a6e786.tar.gz compilertests-7bde6411b1b82cafd997df8ac222e82280a6e786.tar.bz2 |
using sprintf on host for itoa (avoids code duplication iwht libc-freestanding.c)
-rw-r--r-- | miniany/libc-hosted.c | 49 |
1 files changed, 10 insertions, 39 deletions
diff --git a/miniany/libc-hosted.c b/miniany/libc-hosted.c index e599b1d..f602de1 100644 --- a/miniany/libc-hosted.c +++ b/miniany/libc-hosted.c @@ -29,46 +29,17 @@ int putnl( void ) return puts( "" ); } -/* TODO: duplicate of functions in libc-freestanding.c */ - -static void strreverse( char *s ) -{ - char *end = s + strlen( s ) - 1; - - while( s < end ) { - *s ^= *end; - *end ^= *s; - *s ^= *end; - s++; - end--; - } -} - char *itoa( int v, char *s, int base ) { - static char digit[] = "0123456789ABCDEF"; - int sign = 0; - char *p = s; - - if( base < 2 || base > 16 ) { - return NULL; + switch( base ) { + case 10: + sprintf( s, "%d", v ); + return s; + case 16: + sprintf( s, "%x", v ); + return s; + + default: + return NULL; } - - if( v < 0 ) { - v = -v; - sign = 1; - } - - do { - *p++ = digit[v % base]; - } while( ( v /= base ) > 0 ); - - if( sign ) { - *p++ = '-'; - } - *p = '\0'; - - strreverse( s ); - - return s; } |