ref: 622d529211cbe9223fcbae00db289c0c2e92ab40
dir: /libstd/ipparse.myr/
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" 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 }