Modul:Homokozó/Pepo41/IPAddress
Homokozó/Pepo41/IPAddress[mi ez?] • [dokumentáció: mutat, ] • [tesztek: létrehozás]
--[=[
Ezek a funkciók (mivel nem local-ként definiáltak) emiatt a require alkalmazásával más modulok direkt módon hívhatják őket.
Ebből a modulból visszatérési értékként kaphatunk 3 kissebb globális funckiót, melyek akár még sablonokból is hívhatóak.
Kizárólag kettőspontokkal elválasztott hexadecimális számokat támogatja a modul. Az elválasztó kettőspontok kötelezőek.
Unit teszt : Modul:IPAddress/tesztdoboz
== _isIpV6( string ) ==
Valódi IPv6-cím van-e a stringbe írva? Visszaadott értéke logikai típus (true, vagy false).
]=]
function _isIpV6( s )
local dcolon, groups
if type( s ) ~= "string" -- a paraméter nem string, hiba
or s:len() == 0 -- zérus hossz, hiba
or s:find( "[^:%x]" ) -- csak kettőspont és hexadecimális jegyek legálisak
or s:find( "^:[^:]" ) -- kezdődhet így is :: , de nem egyetlen kettősponttal
or s:find( "[^:]:$" ) --
or s:find( ":::" ) -- hiba, ha három : egymás után áll
then
return false
end
s, dcolon = s:gsub( "::", ":" )
if dcolon > 1 then return false end -- at most one ::
s = s:gsub( "^:?", ":" ) -- prepend : if needed, upper
s, groups = s:gsub( ":%x%x?%x?%x?", "" ) -- remove valid groups, and count them
return ( ( dcolon == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) )
and ( s:len() == 0 or ( dcolon == 1 and s == ":" ) ) -- might be one dangling : if original ended with ::
end
function _isIpV4( s )
local function legal( n ) return ( tonumber( n ) or 256 ) < 256 and not n:match("^0%d") end-- in lua 0 is true!
if type( s ) ~= "string" then return false end
local p1, p2, p3, p4 = s:match( "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" )
return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 )
end
function _isIp( s )
return _isIpV4( s ) and "4" or _isIpV6( s ) and "6"
end
local p = {}
function p.isIpV6(frame) return _isIpV6( frame.args[ 1 ] ) and "1" or "0" end
function p.isIpV4(frame) return _isIpV4( frame.args[ 1 ] ) and "1" or "0" end
function p.isIp(frame) return _isIp( frame.args[ 1 ] ) or "" end
return p