Browse Source

add long long fmt routines

master
Felix von Leitner 18 years ago
parent
commit
0a989950f6
  1. 2
      CHANGES
  2. 4
      fmt.h
  3. 14
      fmt/fmt_8longlong.c
  4. 4
      fmt/fmt_long.c
  5. 9
      fmt/fmt_longlong.c
  6. 12
      fmt/fmt_ulonglong.c
  7. 10
      fmt/fmt_xlonglong.c
  8. 14
      test/fmt_long.c
  9. 21
      test/fmt_longlong.c

2
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

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

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

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

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

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

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

14
test/fmt_long.c

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

21
test/fmt_longlong.c

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