ref: 7596ee5f651a67a62de4b009d8647f5b2cf729fb
parent: a44d4b5e4d8e265df68194dadc0fa8410a77e7f8
author: Ori Bernstein <[email protected]>
date: Mon Apr 20 15:51:00 EDT 2015
Add, enable, and test strbuf.myr
--- a/libstd/bld.sub
+++ b/libstd/bld.sub
@@ -24,7 +24,6 @@
bigint.myr
bitset.myr
blat.myr
- bytebuf.myr
chartype.myr
clear.myr
cmp.myr
@@ -32,8 +31,6 @@
die.myr
dirname.myr
endian.myr
- env+posixy.myr
- env+plan9.myr
errno.myr
execvp.myr
extremum.myr
@@ -41,11 +38,11 @@
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 @@
slurp.myr
sort.myr
spork.myr
+ strbuf.myr
strfind.myr
strjoin.myr
strsplit.myr
@@ -80,6 +78,8 @@
varargs.myr
# platform specific files
+ env+plan9.myr
+ env+posixy.myr
dir+freebsd.myr
dir+linux.myr
dir+osx.myr
--- a/libstd/bytebuf.myr
+++ /dev/null
@@ -1,120 +1,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)
- ;;
-}
-
--- /dev/null
+++ b/libstd/strbuf.myr
@@ -1,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
+}
+
--- /dev/null
+++ b/libstd/test/strbuf.myr
@@ -1,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")
+}
+