add long long fmt routines

master
leitner 22 years ago
parent 734b248bdc
commit 5674c5186d

@ -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

@ -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)

@ -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;
}

@ -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);
}

@ -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);
}

@ -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;
}

@ -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);
}

@ -0,0 +1,14 @@
#include <fmt.h>
#include <str.h>
#include <assert.h>
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"));
}

@ -0,0 +1,21 @@
#include <fmt.h>
#include <str.h>
#include <assert.h>
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"));
}
Loading…
Cancel
Save