summaryrefslogtreecommitdiff
path: root/lib/std/strsplit.myr
blob: e64d90713d7c23a77638bdb8e6353d7ae86cef14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
}