shithub: mc

ref: 5608cdaf56ac1e842f6bae691d22ca6800e5b363
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 "strcmp.use"
use "utf.use"
use "fmt.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)
			put("seg[%z..%z] = %s\n", last, j, ip[last:j])
			val = intparsebase(ip[last:j], 10)
			if val < 0 || val > 255
				-> `None
			;;
			addr[i++] = val castto(byte)
			last = j + 1
		;;
	;;
			put("seg[%z..%z] = %s\n", last, j, ip[last:j])
	val = intparsebase(ip[last:j], 10)
	if val < 0 || val > 255
		-> `None
	;;
	addr[i] = val castto(byte)
	if j != ip.len
		-> `None
	;;
	-> `Some addr
}