summaryrefslogtreecommitdiff
path: root/lib/std/strjoin.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/strjoin.myr')
-rw-r--r--lib/std/strjoin.myr41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/std/strjoin.myr b/lib/std/strjoin.myr
new file mode 100644
index 0000000..518a4dc
--- /dev/null
+++ b/lib/std/strjoin.myr
@@ -0,0 +1,41 @@
+use "alloc.use"
+use "die.use"
+use "slcp.use"
+
+pkg std =
+ const strcat : (a : byte[:], b : byte[:] -> byte[:])
+ const strjoin : (strings : byte[:][:], delim : byte[:] -> byte[:])
+;;
+
+const strcat = {a, b
+ -> strjoin([a, b][:], "")
+}
+
+const strjoin = {strings, delim
+ var len, off
+ var i
+ var s
+
+ len = 0
+ for i = 0; i < strings.len; i++
+ len += strings[i].len
+ ;;
+ if strings.len > 0
+ len += (strings.len - 1)*delim.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
+ /* we don't want to terminate the last string with delim */
+ if i != strings.len - 1
+ slcp(s[off:off + delim.len], delim)
+ off += delim.len
+ ;;
+ ;;
+ -> s
+}
+
+