![]() |
VOOZH | about |
Adapted from wikipedia:Module:IPAddress.
localp={} p.isIPv4=function(f) localip=mw.text.trim(f.argsandf.args[1]orf) ifip:len()==0thenreturnfalseend locallegal=function(num) returnnumandtonumber(num)<256 end localp1,p2,p3,p4=ip:match('^(%d+)%.(%d+)%.(%d+)%.(%d+)$') returnlegal(p1)andlegal(p2)andlegal(p3)andlegal(p4) end p.isIPv6=function(f) localip=mw.text.trim(f.argsandf.args[1]orf) localdcolon,groups ifip:len()==0 orip:find('[^:%x]')-- only colon and hex digits are legal chars orip:find('^:[^:]')orip:find('[^:]:$')-- can begin or end with :: but not with single : orip:find(':::') then returnfalse end ip,dcolon=ip:gsub('::',':') ifdcolon>1thenreturnfalseend-- at most one :: ip=ip:gsub('^:?',':')-- prepend : if needed, upper ip,groups=ip:gsub(':%x%x?%x?%x?','')-- remove valid groups, and count them return((dcolon==1andgroups<8)or(dcolon==0andgroups==8)) and(ip:len()==0or(dcolon==1andip==':'))-- might be one dangling : if original ended with :: end p.isIP=function(f) localip=f.argsandf.args[1]orf returnp.isIPv4(ip)and'4'orp.isIPv6(ip)and'6' end returnp