欢迎来到奇葩栖息地!欢迎加入Discord服务器:XfrfHCzfbW欢迎加入QQ频道:r01m9y3iz6请先至特殊:参数设置验证邮箱后再进行编辑。特殊:参数设置挑选自己想要使用的小工具!不会编辑?请至这里学习Wikitext语法。

模块:IPAddress

来自奇葩栖息地
SkyEye FAST讨论 | 贡献2021年11月13日 (六) 14:43的版本 (// Edit via InPageEdit)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
[创建 | 历史 | 清除缓存]文档页面
此模块没有文档页面。如果你知道此模块的使用方法,请帮助为其创建文档页面。
local p = {}
p.isIPv4 = function( f )
	local ip = mw.text.trim( f.args and f.args[1] or f )
	if ip:len() == 0 then return false end
	
    local legal = function( num )
		return num and tonumber( num ) < 256
	end
    local p1, p2, p3, p4 = ip:match( '^(%d+)%.(%d+)%.(%d+)%.(%d+)$' )
    return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 )
end

p.isIPv6 = function( f )
	local ip = mw.text.trim( f.args and f.args[1] or f )
    local dcolon, groups
    if ip:len() == 0
        or ip:find( '[^:%x]' ) -- only colon and hex digits are legal chars
        or ip:find( '^:[^:]' ) or ip:find( '[^:]:$' ) -- can begin or end with :: but not with single :
        or ip:find( ':::' )
    then
        return false
    end 
    ip, dcolon = ip:gsub( '::', ':' )
    if dcolon > 1 then return false end -- 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 == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) )
        and ( ip:len() == 0 or ( dcolon == 1 and ip == ':' ) ) -- might be one dangling : if original ended with ::
end

p.isIP = function( f )
	local ip = f.args and f.args[1] or f
	return p.isIPv4( ip ) and '4' or p.isIPv6( ip ) and '6'
end

return p