mirror of /home/gitosis/repositories/libowfat.git
add array API (http://cr.yp.to/lib/array.html)master
parent
7a18b91761
commit
c4b0574495
68 changed files with 280 additions and 90 deletions
@ -0,0 +1,6 @@ |
||||
#include "array.h" |
||||
|
||||
int64 array_bytes(const array* const x) { |
||||
if (x->allocated<0) return -1; |
||||
return x->initialized; |
||||
} |
@ -0,0 +1,10 @@ |
||||
#include "array.h" |
||||
#include "byte.h" |
||||
|
||||
void array_cat(array* to,const array* const from) { |
||||
if (from->allocated<0) { |
||||
array_fail(to); |
||||
return; |
||||
} |
||||
return array_catb(to,from->p,from->initialized); |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "array.h" |
||||
|
||||
void array_cat0(array* to) { |
||||
static char zero; |
||||
array_catb(to,&zero,1); |
||||
} |
@ -0,0 +1,16 @@ |
||||
#include "array.h" |
||||
#include "byte.h" |
||||
|
||||
void array_catb(array* to,const char* from,uint64 len) { |
||||
long l; |
||||
if (to->allocated<0) return; |
||||
if (to->initialized+len<to->initialized) { |
||||
fail: |
||||
array_fail(to); |
||||
return; |
||||
} |
||||
l=to->initialized; |
||||
if (!array_allocate(to,1,to->initialized+len)) |
||||
goto fail; |
||||
byte_copy(to->p+l,to->initialized-l,from); |
||||
} |
@ -0,0 +1,9 @@ |
||||
#include "array.h" |
||||
|
||||
void array_cate(array* to,const array* const from,int64 pos,int64 stop) { |
||||
if (pos<0 || stop<pos) { |
||||
array_fail(to); |
||||
return; |
||||
} |
||||
array_catb(to,from->p+pos,stop-pos); |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "array.h" |
||||
#include "str.h" |
||||
|
||||
void array_cats(array* to,const char* from) { |
||||
array_catb(to,from,strlen(from)); |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "array.h" |
||||
#include "str.h" |
||||
|
||||
void array_cats0(array* to,const char* from) { |
||||
array_catb(to,from,strlen(from)+1); |
||||
} |
@ -0,0 +1,7 @@ |
||||
#include "byte.h" |
||||
#include "array.h" |
||||
|
||||
int array_equal(const array* const x,const array* const y) { |
||||
if (x->initialized!=y->initialized) return 0; |
||||
return byte_equal(x->p,x->initialized,y->p); |
||||
} |
@ -0,0 +1,9 @@ |
||||
#include <stdlib.h> |
||||
#include "array.h" |
||||
|
||||
void array_fail(array* x) { |
||||
if (x->p) free(x->p); |
||||
x->p=0; |
||||
x->initialized=0; |
||||
x->allocated=-1; |
||||
} |
@ -0,0 +1,30 @@ |
||||
#ifdef __dietlibc__ |
||||
#include <sys/cdefs.h> |
||||
#else |
||||
#define __likely(x) x |
||||
#define __unlikely(x) x |
||||
#endif |
||||
#include "safemult.h" |
||||
#include "array.h" |
||||
|
||||
#if 0 |
||||
static array x; |
||||
t *p; |
||||
int64 pos; |
||||
|
||||
p = array_get(&x,sizeof(t),pos); |
||||
|
||||
array_get is similar to array_allocate, but it does not allocate any |
||||
extra bytes, and it does not initialize any extra bytes. It |
||||
returns 0 if x is unallocated, for example, or if fewer than |
||||
(pos+1)*sizeof(t) bytes are initialized. |
||||
#endif |
||||
|
||||
void* array_get(array* x,uint64 membersize,int64 pos) { |
||||
uint64 wanted; |
||||
if (__unlikely(pos+1<1)) return 0; |
||||
if (__unlikely(umult64(membersize,pos,&wanted))) return 0; |
||||
|
||||
if (__unlikely(wanted > x->allocated)) return 0; |
||||
return x->p+pos*membersize; |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "array.h" |
||||
|
||||
int64 array_length(const array* const x,uint64 membersize) { |
||||
if (x->allocated<0) return -1; |
||||
return x->initialized/membersize; |
||||
} |
@ -0,0 +1,8 @@ |
||||
#include <stdlib.h> |
||||
#include "array.h" |
||||
|
||||
void array_reset(array* x) { |
||||
if (x->p) free(x->p); |
||||
x->p=0; |
||||
x->allocated=x->initialized=0; |
||||
} |
@ -0,0 +1,5 @@ |
||||
#include "array.h" |
||||
|
||||
void* array_start(const array* const x) { |
||||
return x->p; |
||||
} |
@ -0,0 +1,5 @@ |
||||
#include "array.h" |
||||
|
||||
void array_trunc(array* x) { |
||||
x->initialized=0; |
||||
} |
@ -0,0 +1,17 @@ |
||||
#ifdef __dietlibc__ |
||||
#include <sys/cdefs.h> |
||||
#else |
||||
#define __likely(x) x |
||||
#define __unlikely(x) x |
||||
#endif |
||||
#include "safemult.h" |
||||
#include "array.h" |
||||
|
||||
/* I'm not sure I understand what this function is good for */ |
||||
void array_truncate(array* x,uint64 membersize,int64 len) { |
||||
uint64 wanted; |
||||
if (__unlikely(len<0)) return; |
||||
if (__unlikely(umult64(membersize,len,&wanted))) return; |
||||
if (__unlikely(wanted > x->initialized)) return; |
||||
x->initialized=wanted; |
||||
} |
@ -0,0 +1,13 @@ |
||||
#include <assert.h> |
||||
#include "array.h" |
||||
#include "byte.h" |
||||
|
||||
main() { |
||||
static array x,y; |
||||
array_cats(&x,"fnord"); |
||||
array_cats(&y,"foobar"); |
||||
array_cat(&x,&y); |
||||
array_fail(&y); |
||||
array_cat(&y,&x); |
||||
assert(byte_equal(x.p,11,"fnordfoobar")); |
||||
} |
Loading…
Reference in new issue