summaryrefslogtreecommitdiff
path: root/lib/std/strsplit.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/strsplit.myr')
-rw-r--r--lib/std/strsplit.myr35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/std/strsplit.myr b/lib/std/strsplit.myr
new file mode 100644
index 0000000..e64d907
--- /dev/null
+++ b/lib/std/strsplit.myr
@@ -0,0 +1,35 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "option.use"
+use "slpush.use"
+use "strfind.use"
+use "types.use"
+
+pkg std =
+ const strsplit : (s : byte[:], delim : byte[:] -> byte[:][:])
+;;
+
+const strsplit = {s, delim
+ var last
+ var sp
+
+ sp = [][:]
+ if s.len == 0
+ -> sp
+ ;;
+ last = 0
+ while true
+ match strfind(s, delim)
+ | `Some i:
+ sp = slpush(sp, s[:i])
+ s = s[i + delim.len:]
+ | `None:
+ goto donesplit
+ ;;
+ ;;
+:donesplit
+ sp = slpush(sp, s[:])
+ -> sp
+}
+