shithub: mc

ref: 49f27bf2a929c681f40752da7329e377030198a3
dir: /lib/std/striter.myr/

View raw version
use "die"
use "types"
use "utf"
use "strfind"
use "option"

pkg std =
	type chariter = struct
		rest	: byte[:]
	;;

	type splititer = struct
		rest	: byte[:]
		split	: byte[:]
	;;

	impl iterable chariter -> char
	impl iterable splititer -> byte[:]

	const bychar	: (str : byte[:] -> chariter)
	const bysplit	: (str : byte[:], split : byte[:] -> splititer)
;;

impl iterable chariter -> char =
	__iternext__ = {ci, c
		if ci.rest.len == 0
			-> false
		;;
		(c#, ci.rest) = strstep(ci.rest)
		-> true
	}

	__iterfin__ = {ci, c
	}
;;

const bychar = {str
	-> [.rest = str]
}

impl iterable splititer -> byte[:] =
	__iternext__ = {si, sp
		match std.strfind(si.rest, si.split)
		| `Some off:
			sp# = si.rest[:off]
			si.rest = si.rest[off + si.split.len:]
			-> true
		| `None:
			if si.rest.len > 0
				sp# = si.rest
				si.rest = ""
				-> true
			;;
		;;
		-> false
	}

	__iterfin__ = {ci, c
	}
;;

const bysplit = {str, split
	-> [.rest = str, .split = split]
}