Browse Source

add buffer_tosa check to marshal.c

failed attempt to improve scan_ulongn, results in essentially same code
but the C code looks slighly better so I'm keeping it
master
Felix von Leitner 8 years ago
parent
commit
69e6c3c6c6
  1. 6
      scan/scan_ulongn.c
  2. 10
      test/marshal.c

6
scan/scan_ulongn.c

@ -14,10 +14,8 @@ size_t scan_ulongn(const char* src,size_t n,unsigned long int* dest) {
* so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
* and check for overflow on all the intermediate steps */
n=l<<3; if ((n>>3)!=l) break;
if (n+(l<<1) < n) break;
n+=l<<1;
if (n+c < n) break;
l=n+c;
if (n+(l<<1)+c < n) break;
l=n+(l<<1)+c;
++tmp;
}
if (tmp-src) *dest=l;

10
test/marshal.c

@ -398,12 +398,22 @@ int main() {
{
buffer b;
stralloc s;
size_t i;
buffer_fromsa(&b,&sa);
stralloc_init(&s);
assert(buffer_getline_sa(&b,&s)==1 && stralloc_equals(&s,"foo\n"));
assert(buffer_getnewline_sa(&b,&s)==1 && stralloc_equals(&s,"bar\r\n"));
assert(buffer_getnewline_sa(&b,&s)==0 && stralloc_equals(&s,"baz"));
buffer_close(&b);
stralloc_free(&s);
buffer_tosa(&b,&s);
for (i=0; i<100; ++i)
buffer_puts(&b,"foo bar baz!\n");
buffer_flush(&b);
assert(s.len==100*sizeof("foo bar baz!"));
for (i=0; i<100; ++i)
assert(byte_equal(s.s+i*sizeof("foo bar baz!"),sizeof("foo bar baz!"),"foo bar baz!\n"));
}
return 0;

Loading…
Cancel
Save