Modul:URLutil: Unterschied zwischen den Versionen
Modify to return object instead of table (convention, works with unit test framework)
w>קיפודנחש (test if a string is a valid ipV4 or ipV6 address) |
w>Dcoetzee (Modify to return object instead of table (convention, works with unit test framework)) |
||
| Zeile 5: | Zeile 5: | ||
]] | ]] | ||
function | function _isIpV6( s ) | ||
local dcolon, groups | local dcolon, groups | ||
if type( s ) ~= "string" | if type( s ) ~= "string" | ||
| Zeile 24: | Zeile 24: | ||
end | end | ||
function | function _isIpV4( s ) | ||
local function legal( n ) return ( tonumber( n ) or 256 ) < 256 end-- in lua 0 is true! | local function legal( n ) return ( tonumber( n ) or 256 ) < 256 end-- in lua 0 is true! | ||
| Zeile 32: | Zeile 32: | ||
end | end | ||
function | function _isIp( s ) | ||
return isIpV4( s ) and "4" or isIpV6( s ) and "6" | return isIpV4( s ) and "4" or isIpV6( s ) and "6" | ||
end | 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 ] ) and "1" or "0" end | |||
return p | |||