summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--libstd/Makefile1
-rw-r--r--libstd/bldfile3
-rw-r--r--libstd/bytebuf.myr120
4 files changed, 124 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 43a4115..38c5834 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
+ ;;
+}
+