summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-04-20 19:51:00 -0700
committerOri Bernstein <ori@eigenstate.org>2015-04-20 19:51:00 -0700
commit1044e6f057ce380ec514738996693d762d59c3f8 (patch)
tree980f5a7720e8ff3e86335473b04fcc5d977fa662 /libstd
parent943525b9faedc0c645e0c12a7c65a5ca90028df4 (diff)
downloadmc-1044e6f057ce380ec514738996693d762d59c3f8.tar.gz
Add, enable, and test strbuf.myr
Diffstat (limited to 'libstd')
-rw-r--r--libstd/bld.sub8
-rw-r--r--libstd/bytebuf.myr120
-rw-r--r--libstd/strbuf.myr87
-rw-r--r--libstd/test/strbuf.myr24
4 files changed, 115 insertions, 124 deletions
diff --git a/libstd/bld.sub b/libstd/bld.sub
index f18cf4a..3df53b0 100644
--- a/libstd/bld.sub
+++ b/libstd/bld.sub
@@ -24,7 +24,6 @@ lib std {inc=.} =
bigint.myr
bitset.myr
blat.myr
- bytebuf.myr
chartype.myr
clear.myr
cmp.myr
@@ -32,8 +31,6 @@ lib std {inc=.} =
die.myr
dirname.myr
endian.myr
- env+posixy.myr
- env+plan9.myr
errno.myr
execvp.myr
extremum.myr
@@ -41,11 +38,11 @@ lib std {inc=.} =
fltfmt.myr
fmt.myr
getcwd.myr
+ getint.myr
hashfuncs.myr
hasprefix.myr
hassuffix.myr
htab.myr
- getint.myr
intparse.myr
ipparse.myr
mk.myr
@@ -68,6 +65,7 @@ lib std {inc=.} =
slurp.myr
sort.myr
spork.myr
+ strbuf.myr
strfind.myr
strjoin.myr
strsplit.myr
@@ -80,6 +78,8 @@ lib std {inc=.} =
varargs.myr
# platform specific files
+ env+plan9.myr
+ env+posixy.myr
dir+freebsd.myr
dir+linux.myr
dir+osx.myr
diff --git a/libstd/bytebuf.myr b/libstd/bytebuf.myr
deleted file mode 100644
index 0c51ba9..0000000
--- a/libstd/bytebuf.myr
+++ /dev/null
@@ -1,120 +0,0 @@
-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)
- ;;
-}
-
diff --git a/libstd/strbuf.myr b/libstd/strbuf.myr
new file mode 100644
index 0000000..88e24ec
--- /dev/null
+++ b/libstd/strbuf.myr
@@ -0,0 +1,87 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "slcp.use"
+use "types.use"
+use "utf.use"
+use "fmt.use"
+
+pkg std =
+ type strbuf = struct
+ buf : byte[:]
+ len : size
+ fixed : bool
+ ;;
+
+ const mksb : (-> strbuf#)
+ const sbfin : (sb : strbuf# -> byte[:])
+ const sbfree : (sb : strbuf# -> void)
+ const sbpeek : (sb : strbuf# -> byte[:])
+
+ const sbputc : (sb : strbuf#, v : char -> bool)
+ const sbputs : (sb : strbuf#, v : byte[:] -> bool)
+ const sbputb : (sb : strbuf#, v : byte -> bool)
+ const sbtrim : (sb : strbuf#, len : std.size -> void)
+;;
+
+const mksb = {
+ var sb
+ sb = std.zalloc()
+ sb.buf = std.slalloc(1)
+ -> sb
+}
+
+const sbfin = {sb : strbuf#
+ var sl
+
+ sl = sb.buf[:sb.len]
+ std.free(sb)
+ -> sl
+}
+
+const sbpeek = {sb : strbuf#
+ -> sb.buf[:sb.len]
+}
+
+const sbputc = {sb, v
+ if !ensure(sb, charlen(v))
+ -> false
+ ;;
+ sb.len += encode(sb.buf[sb.len:], v)
+ -> true
+}
+const sbputs = {sb, v
+ if !ensure(sb, v.len)
+ -> false
+ ;;
+ std.slcp(sb.buf[sb.len:sb.len + v.len], v)
+ sb.len += v.len
+ -> true
+}
+const sbputb = {sb, v
+ if !ensure(sb, 1)
+ -> false
+ ;;
+ sb.buf[sb.len++] = v
+ -> true
+}
+
+const sbtrim = {sb, len
+ std.assert(std.abs(len) <= sb.len, "trim out of range\n")
+ if len < 0
+ sb.len -= std.abs(len)
+ else
+ sb.len = len
+ ;;
+}
+
+const ensure = {sb, len
+ if sb.fixed && sb.len + len >= sb.buf.len
+ -> false
+ ;;
+ while sb.buf.len < sb.len + len
+ sb.buf = slgrow(sb.buf, 2*sb.buf.len)
+ ;;
+ -> true
+}
+
diff --git a/libstd/test/strbuf.myr b/libstd/test/strbuf.myr
new file mode 100644
index 0000000..92c5a7c
--- /dev/null
+++ b/libstd/test/strbuf.myr
@@ -0,0 +1,24 @@
+use std
+
+const main = {
+ var sb
+
+ sb = std.mksb()
+ std.assert(std.sleq(std.sbpeek(sb), ""), "mismatched empty str\n")
+ std.sbputs(sb, "hello")
+ std.assert(std.sleq(std.sbpeek(sb), "hello"), "mismatched hello\n")
+ std.sbputs(sb, ", hello")
+ std.assert(std.sleq(std.sbpeek(sb), "hello, hello"), "mismatched double hello\n")
+ std.sbtrim(sb, 7)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, "), "mismatched trim\n")
+ std.sbputs(sb, "world")
+ std.assert(std.sleq(std.sbpeek(sb), "hello, world"), "mismatched hello world\n")
+ std.sbtrim(sb, -5)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, "), "mismatched rtrim\n")
+ std.sbputc(sb, '世')
+ std.sbputc(sb, '界')
+ std.assert(std.sleq(std.sbpeek(sb), "hello, 世界"), "mismatched unicode\n")
+ std.sbputb(sb, 10)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, 世界\n"), "mismatched byte\n")
+}
+