diff options
author | Ori Bernstein <ori@eigenstate.org> | 2015-02-21 16:16:11 -0800 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2015-02-21 16:16:11 -0800 |
commit | b93cc4922482a7d96724c022442a45e02085f0aa (patch) | |
tree | 9e50dcd6e663d689b423edac956367802b164a85 | |
parent | c1241053700dac8c921f292f1e1b346ce1c1a966 (diff) | |
download | mc-b93cc4922482a7d96724c022442a45e02085f0aa.tar.gz |
Add 'bytebuf' implementation.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libstd/Makefile | 1 | ||||
-rw-r--r-- | libstd/bldfile | 3 | ||||
-rw-r--r-- | libstd/bytebuf.myr | 120 |
4 files changed, 124 insertions, 2 deletions
@@ -11,8 +11,8 @@ include mk/c.mk include config.mk check: all - $(MAKE) -C libstd check $(MAKE) -C test check + $(MAKE) -C libstd check bench: all $(MAKE) -C bench bench diff --git a/libstd/Makefile b/libstd/Makefile index ad168bd..f3cd561 100644 --- a/libstd/Makefile +++ b/libstd/Makefile @@ -16,6 +16,7 @@ STDSRC= \ bigint.myr \ bitset.myr \ blat.myr \ + bytebuf.myr \ chartype.myr \ cmp.myr \ clear.myr \ diff --git a/libstd/bldfile b/libstd/bldfile index cc6cc1e..80b8db7 100644 --- a/libstd/bldfile +++ b/libstd/bldfile @@ -22,8 +22,9 @@ lib std {inc=.} = bigint.myr bitset.myr blat.myr + bytebuf.myr chartype.myr - clear.myr + clear.myr cmp.myr cstrconv.myr dial.myr diff --git a/libstd/bytebuf.myr b/libstd/bytebuf.myr new file mode 100644 index 0000000..0c51ba9 --- /dev/null +++ b/libstd/bytebuf.myr @@ -0,0 +1,120 @@ +use "alloc.use" +use "die.use" +use "extremum.use" +use "slcp.use" +use "types.use" +use "utf.use" + +pkg std = + type bytebuf = struct + buf : byte[:] + len : size + ;; + + const mkbytebuf : (-> bytebuf#) + const bytebuffin : (bb : bytebuf# -> byte[:]) + const bytebufpeek : (bb : bytebuf# -> byte[:]) + + const bytebufputc : (bb : bytebuf#, v : char -> bytebuf#) + const bytebufputs : (bb : bytebuf#, v : byte[:] -> bytebuf#) + const bytebufputb : (bb : bytebuf#, v : byte -> bytebuf#) + + generic bytebufputle8 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputle16 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputle32 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputle64 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + + generic bytebufputbe8 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputbe16 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputbe32 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) + generic bytebufputbe64 : (bb : bytebuf#, v : @a::(integral,numeric) -> bytebuf#) +;; + +const mkbytebuf = { + var bb + bb = std.zalloc() + bb.buf = std.slalloc(1) + -> bb +} + +const bytebuffin = {bb : bytebuf# + var sl + + sl = bb.buf[:bb.len] + std.free(bb) + -> sl +} + +const bytebufpeek = {bb : bytebuf# + -> bb.buf[:bb.len] +} + +const bytebufputc = {bb, v + ensure(bb, charlen(v)) + bb.len += encode(bb.buf[bb.len:], v) + -> bb +} +const bytebufputs = {bb, v + ensure(bb, v.len) + std.slcp(bb.buf[bb.len:bb.len + v.len], v) + bb.len += v.len + -> bb +} +const bytebufputb = {bb, v + ensure(bb, 1) + bb.buf[bb.len++] = v + -> bb +} + +generic bytebufputle8 = {bb, v; -> putintle(bb, v castto(uint64), 1)} +generic bytebufputle16 = {bb, v; -> putintle(bb, v castto(uint64), 2)} +generic bytebufputle32 = {bb, v; -> putintle(bb, v castto(uint64), 4)} +generic bytebufputle64 = {bb, v; -> putintle(bb, v castto(uint64), 8)} + +generic bytebufputbe8 = {bb, v; -> putintbe(bb, v castto(uint64), 1)} +generic bytebufputbe16 = {bb, v; -> putintbe(bb, v castto(uint64), 2)} +generic bytebufputbe32 = {bb, v; -> putintbe(bb, v castto(uint64), 4)} +generic bytebufputbe64 = {bb, v; -> putintbe(bb, v castto(uint64), 8)} + +const putintle = {bb, v, len + var buf : byte[8] + + ensure(bb, len) + buf[0] = (v >> 0) & 0xff castto(byte) + buf[1] = (v >> 8) & 0xff castto(byte) + buf[2] = (v >> 16) & 0xff castto(byte) + buf[3] = (v >> 24) & 0xff castto(byte) + buf[4] = (v >> 32) & 0xff castto(byte) + buf[5] = (v >> 40) & 0xff castto(byte) + buf[6] = (v >> 48) & 0xff castto(byte) + buf[7] = (v >> 56) & 0xff castto(byte) + + std.slcp(bb.buf[bb.len:bb.len + len], buf[:len]) + bb.len += len + -> bb +} + +const putintbe = {bb, v, len + var buf : byte[8] + + ensure(bb, len) + buf[0] = (v >> 56) & 0xff castto(byte) + buf[1] = (v >> 48) & 0xff castto(byte) + buf[2] = (v >> 40) & 0xff castto(byte) + buf[3] = (v >> 32) & 0xff castto(byte) + buf[4] = (v >> 24) & 0xff castto(byte) + buf[5] = (v >> 16) & 0xff castto(byte) + buf[6] = (v >> 8) & 0xff castto(byte) + buf[7] = (v >> 0) & 0xff castto(byte) + + std.slcp(bb.buf[bb.len:bb.len + len], buf[8-len:]) + bb.len += len + -> bb +} +const ensure = {bb, len + + while bb.buf.len < bb.len + len + bb.buf = slgrow(bb.buf, 2*bb.buf.len) + ;; +} + |