summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-12-27 17:36:43 -0500
committerOri Bernstein <ori@eigenstate.org>2013-12-27 17:36:43 -0500
commitafc7520151f5ea1c30be31e9eb85e58dd863d632 (patch)
treee93cc9cfc3320c2e156e5b0535f34017428f2c22
parentd752a67b5b88c1e6ae9e632a960dc9ef23827d44 (diff)
downloadmc-afc7520151f5ea1c30be31e9eb85e58dd863d632.tar.gz
Added 'strjoin' to libstd
-rw-r--r--libstd/Makefile1
-rw-r--r--libstd/optparse.myr9
-rw-r--r--libstd/strjoin.myr33
-rw-r--r--libstd/utf.myr5
-rw-r--r--test/data/strjoin-expected2
-rw-r--r--test/strjoin.myr12
-rw-r--r--test/tests1
7 files changed, 58 insertions, 5 deletions
diff --git a/libstd/Makefile b/libstd/Makefile
index ebec5a6..04edd2f 100644
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -26,6 +26,7 @@ MYRSRC= \
slpush.myr \
slurp.myr \
strfind.myr \
+ strjoin.myr \
strsplit.myr \
strstrip.myr \
strcmp.myr \
diff --git a/libstd/optparse.myr b/libstd/optparse.myr
index c176430..176314d 100644
--- a/libstd/optparse.myr
+++ b/libstd/optparse.myr
@@ -26,6 +26,7 @@ pkg std =
const optinit : (optstr: byte[:], optargs : byte[:][:] -> optctx#)
const optnext : (ctx : optctx# -> [char, byte[:]])
const optdone : (ctx : optctx# -> bool)
+ const optfin : (ctx : optctx# -> byte[:][:])
;;
const optinit = {optstr, optargs
@@ -46,6 +47,14 @@ const optinit = {optstr, optargs
-> ctx
}
+const optfin = {ctx
+ var a
+
+ a = ctx.args
+ free(ctx)
+ -> a
+}
+
const optnext = {ctx
var c
var arg
diff --git a/libstd/strjoin.myr b/libstd/strjoin.myr
new file mode 100644
index 0000000..e7ad4e6
--- /dev/null
+++ b/libstd/strjoin.myr
@@ -0,0 +1,33 @@
+use "alloc.use"
+use "die.use"
+use "slcp.use"
+
+pkg std =
+ const strcat : (a : byte[:], b : byte[:] -> byte[:])
+ const strjoin : (strings : byte[:][:] -> byte[:])
+;;
+
+const strcat = {a, b
+ -> strjoin([a, b][:])
+}
+
+const strjoin = {strings
+ var len, off
+ var i
+ var s
+
+ len = 0
+ for i = 0; i < strings.len; i++
+ len += strings[i].len
+ ;;
+
+ s = slalloc(len)
+ off = 0
+ for i = 0; i < strings.len; i++
+ slcp(s[off:off + strings[i].len], strings[i])
+ off += strings[i].len
+ ;;
+ -> s
+}
+
+
diff --git a/libstd/utf.myr b/libstd/utf.myr
index 0580cfa..672ca05 100644
--- a/libstd/utf.myr
+++ b/libstd/utf.myr
@@ -11,11 +11,6 @@ pkg std =
const encode : (buf : byte[:], chr : char -> size)
const decode : (buf : byte[:] -> char)
const striter : (str : byte[:] -> [char, byte[:]])
-
- const strjoin : (lst : byte[:][:], delim:byte[:] -> byte[:])
- const strsep : (str : byte[:], delim:byte[:] -> byte[:][:])
- const strbjoin : (lst : byte[:][:], delim:byte[:] -> byte[:])
- const strbsep : (str : byte[:], delim:byte[:] -> byte[:][:])
;;
const charlen = {c
diff --git a/test/data/strjoin-expected b/test/data/strjoin-expected
new file mode 100644
index 0000000..e09c5f4
--- /dev/null
+++ b/test/data/strjoin-expected
@@ -0,0 +1,2 @@
+foo;bar
+xyzw
diff --git a/test/strjoin.myr b/test/strjoin.myr
new file mode 100644
index 0000000..452b75a
--- /dev/null
+++ b/test/strjoin.myr
@@ -0,0 +1,12 @@
+use std
+
+const main = {
+ const strings = [
+ "x",
+ "y",
+ "z",
+ "w"
+ ]
+ std.put("%s\n", std.strcat("foo;", "bar"))
+ std.put("%s\n", std.strjoin(strings[:]))
+}
diff --git a/test/tests b/test/tests
index 4e2f2b0..1c49a71 100644
--- a/test/tests
+++ b/test/tests
@@ -110,6 +110,7 @@ B catfile C
B strstrip C
B strsplit C
B strfind C
+B strjoin C
# B local-labels E 10 ## BUGGERED
F declmismatch
F infermismatch