unified fmt macro!
Nikola Vladov <[email protected]>
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <[email protected]> |
I suggest to write all fmt_xxx(char *dest, unsigned type u)
usings a macro. Bellow this is the file fmt_number.h
Seems that this macro produces shorter text code. Maybe the
key is the line: do { tmp /=base; ++len; } while(tmp);\
The compiler like len=0 instead of len=1.
The compiler
if (base <= 10) *--s = c+'0';\
else *--s = (c<10) ? c+'0' : c-10+'a';\
optimize this code good.
Since all is in one file, we could change easy fmt_number.h
and this updates many fmt_*.c files automatic.
Felix, if you accept this, I have also a similar file scan_number.h
Libowfat has manpage for fmt_uint and fmt_uint.c is missing.
If you like this macro, please include it also in fmt.h.
Then we could write similar functions easy. I didn't test
the functions below for speed.
/nv
----------------------- fmt_number.h -------------------
#include "fmt.h"
#define fmt_number_macro(f,type,base) \
size_t f(char *s, type u) {\
type tmp=u;\
unsigned int len=0;\
do { tmp /=base; ++len; } while(tmp);\
if (s) {\
s +=len;\
do {\
unsigned char c = u%base;\
if (base <= 10) *--s = c+'0';\
else *--s = (c<10) ? c+'0' : c-10+'a';\
u /=base;\
} while (u);\
}\
return len;\
}
--------------------------------------------------------
some applications:
------------------------- fmt_ulong.c ------------------
#include "fmt_number.h"
fmt_number_macro(fmt_ulong, unsigned long, 10)
--------------------------------------------------------
------------------------- fmt_xlong.c ------------------
#include "fmt_number.h"
fmt_number_macro(fmt_xlong, unsigned long, 16)
--------------------------------------------------------
------------------------- fmt_8long.c ------------------
#include "fmt_number.h"
fmt_number_macro(fmt_8long, unsigned long, 8)
--------------------------------------------------------
------------------------- fmt_ulonglong.c --------------
#include "fmt_number.h"
fmt_number_macro(fmt_ulonglong, unsigned long long, 10)
--------------------------------------------------------
------------------------- fmt_xlonglong.c --------------
#include "fmt_number.h"
fmt_number_macro(fmt_xlonglong, unsigned long long, 16)
--------------------------------------------------------
------------------------- fmt_uint.c -------------------
#include "fmt_number.h"
fmt_number_macro(fmt_uint, unsigned int, 10)
--------------------------------------------------------