shithub: mc

ref: f2e5f2968fed0b698aa129a9b359c8593ae7cbfd
dir: /libstd/ipparse.myr/

View raw version
use "types.use"
use "intparse.use"
use "option.use"

 /* FIXME: needed for decls which should be pulled in as hidden */
use "hasprefix.use"
use "utf.use"

pkg std =
	const ip4parse	: (ip : byte[:] -> option(byte[4]))
	const ip6parse	: (ip : byte[:] -> option(byte[16]))
;;

const ip4parse = {ip
	var addr
	var last : size
	var x : option(int32)
	var val : int32 /* need int32 to check for overflow */
	var i
	var j : size

	i = 0
	for j = 0; j < ip.len; j++
		if ip[j] == '.' castto(byte)
			match intparsebase(ip[last:j], 10)
			| `Some v:
				val = v
				if val < 0 || val > 255
					-> `None
				;;
				addr[i++] = val castto(byte)
				last = j + 1
			| `None:
				-> `None
			;;
		;;
	;;
	match intparsebase(ip[last:j], 10)
	| `Some v:
		val = v
		if val < 0 || val > 255
			-> `None
		;;
		addr[i] = val castto(byte)
	| `None:
		-> `None
	;;
	if j != ip.len
		-> `None
	;;
	-> `Some addr
}