shithub: mc

ref: 515b8e89c1a6c0859eaefbf7835a4e6497a57e0b
dir: /libstd/dirname.myr/

View raw version
use "alloc.use"
use "slcp.use"
use "die.use"
use "sldup.use"
use "strfind.use"
use "option.use"

pkg std =
	const dirname	: (p : byte[:] -> byte[:])
	const basename	: (p : byte[:] -> byte[:])
;;

const dirname = {p
	match std.strrfind(p, "/")
	| `std.Some idx:
		-> std.sldup(p[:idx])
	| `std.None:
		-> std.sldup(".")
	;;
}

const basename = {p
:again
	if p.len == 0
		-> std.sldup(".")
	;;

	match std.strrfind(p, "/")
	| `std.Some idx:
		if idx == p.len - 1
			goto again
		;;
		-> std.sldup(p[idx+1:])
	| `std.None:
		-> std.sldup(p)
	;;
}