shithub: mc

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

View raw version
use "extremum.use"
use "types.use"

pkg std =
	type order = union
		`Before
		`Equal
		`After
	;;

	generic numcmp	: (a : @a, b : @a -> order)
	const strcmp	: (a : byte[:], b : byte[:] -> order)
	const strncmp	: (a : byte[:], b : byte[:], n : size -> order)
;;

generic numcmp = {a, b
	if a < b
		-> `Before
	elif a == b
		-> `Equal
	else
		-> `After
	;;
}

const strcmp = {a, b
	var l
	var i

	l = min(a.len, b.len)
	for i = 0; i < l; i++
		if a[i] < b[i]
			-> `Before
		elif a[i] > b[i]
			-> `After
		;;
	;;

	if a.len < b.len
		-> `Before
	elif a.len > b.len
		-> `After
	else
		-> `Equal
	;;
		
}

const strncmp = {a, b, n
	a = a[:min(a.len, n)]
	b = b[:min(b.len, n)]
	-> strcmp(a, b)
}