diff --git a/CHANGES b/CHANGES index e4a0f5b..69592ab 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,8 @@ add fmt_human and fmt_humank (format numbers ala ls -H/-h) add fmt_httpdate and scan_httpdate fix typo breaking buffer_GETC in buffer (Marcus Winkler) + fix typo breaking fmt_long for dest==NULL + add fmt_*longlong() 0.14: avoid bus errors in byte_copy diff --git a/fmt.h b/fmt.h index e50b3b6..ac1fbc8 100644 --- a/fmt.h +++ b/fmt.h @@ -29,6 +29,10 @@ unsigned int fmt_xlong(char *dest,unsigned long src); * If dest is not NULL, write result to dest */ unsigned int fmt_8long(char *dest,unsigned long src); +unsigned int fmt_longlong(char *dest,signed long long src); +unsigned int fmt_ulonglong(char *dest,unsigned long long src); +unsigned int fmt_xlonglong(char *dest,unsigned long long src); + #define fmt_uint(dest,src) fmt_ulong(dest,src) #define fmt_int(dest,src) fmt_long(dest,src) #define fmt_xint(dest,src) fmt_xlong(dest,src) diff --git a/fmt/fmt_8longlong.c b/fmt/fmt_8longlong.c new file mode 100644 index 0000000..b350242 --- /dev/null +++ b/fmt/fmt_8longlong.c @@ -0,0 +1,14 @@ +#include "fmt.h" + +unsigned int fmt_8longlong(char *dest,unsigned long long i) { + register unsigned long len; + unsigned long long tmp; + /* first count the number of bytes needed */ + for (len=1, tmp=i; tmp>7; ++len) tmp>>=3; + if (dest) + for (tmp=i, dest+=len; ; ) { + *--dest = (tmp&7)+'0'; + if (!(tmp>>=3)) break; + } + return len; +} diff --git a/fmt/fmt_long.c b/fmt/fmt_long.c index b521142..d7ff0f7 100644 --- a/fmt/fmt_long.c +++ b/fmt/fmt_long.c @@ -2,8 +2,8 @@ unsigned int fmt_long(char *dest,long int i) { if (i<0) { - if (dest) *dest='-'; - return fmt_ulong(dest+1,-i)+1; + if (dest) *dest++='-'; + return fmt_ulong(dest,-i)+1; } else return fmt_ulong(dest,i); } diff --git a/fmt/fmt_longlong.c b/fmt/fmt_longlong.c new file mode 100644 index 0000000..543afc4 --- /dev/null +++ b/fmt/fmt_longlong.c @@ -0,0 +1,9 @@ +#include "fmt.h" + +unsigned int fmt_longlong(char *dest,signed long long int i) { + if (i<0) { + if (dest) *dest++='-'; + return fmt_ulonglong(dest,-i)+1; + } else + return fmt_ulonglong(dest,i); +} diff --git a/fmt/fmt_ulonglong.c b/fmt/fmt_ulonglong.c new file mode 100644 index 0000000..c06661d --- /dev/null +++ b/fmt/fmt_ulonglong.c @@ -0,0 +1,12 @@ +#include "fmt.h" + +unsigned int fmt_ulonglong(char *dest,unsigned long long int i) { + register unsigned long len; + unsigned long long tmp,len2; + /* first count the number of bytes needed */ + for (len=1, tmp=i; tmp>9; ++len) tmp/=10; + if (dest) + for (tmp=i, dest+=len, len2=len+1; --len2; tmp/=10) + *--dest = (tmp%10)+'0'; + return len; +} diff --git a/fmt/fmt_xlonglong.c b/fmt/fmt_xlonglong.c new file mode 100644 index 0000000..e925c5a --- /dev/null +++ b/fmt/fmt_xlonglong.c @@ -0,0 +1,10 @@ +#include "fmt.h" + +unsigned int fmt_xlonglong(char *dest,unsigned long long i) { + int tmp=0; + if (i>>32) { + tmp=fmt_xlong(dest,i>>32); + if (dest) dest+=tmp; + } + return tmp+fmt_xlong(dest,i&0xffffffff); +} diff --git a/test/fmt_long.c b/test/fmt_long.c new file mode 100644 index 0000000..4addb95 --- /dev/null +++ b/test/fmt_long.c @@ -0,0 +1,14 @@ +#include +#include +#include + +main() { + char buf[1024]; + + assert(fmt_long(0,12345)==5); + assert(fmt_long(0,-12345)==6); + assert(fmt_long(buf,12345)==5); buf[5]=0; + assert(str_equal(buf,"12345")); + assert(fmt_long(buf,-12345)==6); buf[6]=0; + assert(str_equal(buf,"-12345")); +} diff --git a/test/fmt_longlong.c b/test/fmt_longlong.c new file mode 100644 index 0000000..7a4014e --- /dev/null +++ b/test/fmt_longlong.c @@ -0,0 +1,21 @@ +#include +#include +#include + +main() { + char buf[1024]; + + assert(fmt_longlong(0,12345)==5); + assert(fmt_longlong(0,-12345)==6); + assert(fmt_longlong(buf,12345)==5); buf[5]=0; + assert(str_equal(buf,"12345")); + assert(fmt_longlong(buf,-12345)==6); buf[6]=0; + assert(str_equal(buf,"-12345")); + + assert(fmt_longlong(0,1234567890)==10); + assert(fmt_longlong(0,-1234567890)==11); + assert(fmt_longlong(buf,1234567890)==10); buf[10]=0; + assert(str_equal(buf,"1234567890")); + assert(fmt_longlong(buf,-1234567890)==11); buf[11]=0; + assert(str_equal(buf,"-1234567890")); +}