Modulo:String/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
Nessun oggetto della modifica
Moroboshi (discussione | contributi)
funzione checkIP
Riga 823:
return pre .. mw.text.listToText(elements, separatore, congiunzione) .. post
end
 
--[[
Identifica se il valore passato è un IP
source: https://stackoverflow.com/questions/10975935/lua-function-check-if-ipv4-or-ipv6-or-string
 
--]]
function str.checkIP(frame)
 
local IP = frame.args[1]
 
-- check for format 1.11.111.111 for ipv4
local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
if #chunks == 4 then
for _,v in pairs(chunks) do
if tonumber(v) > 255 then return '' end
end
return IP
end
 
-- check for ipv6 format, should be 8 'chunks' of numbers/letters
-- without leading/trailing chars
-- or fewer than 8 chunks, but with only one `::` group
local chunks = {ip:match("^"..(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$")))}
if #chunks == 8 or #chunks < 8 and ip:match('::') and not ip:gsub("::","",1):match('::') then
for _,v in pairs(chunks) do
if #v > 0 and tonumber(v, 16) > 65535 then
return ''
end
end
return IP
end
return ''
end
 
 
 
Line 905 ⟶ 939:
-- 07/05/2013 aggiunta funzione rep da en:module:String versione 552254999 del 26 aprile 2013
-- 19/08/2013 aggiunta funzione arraytostring
-- 14/6/2019 aggiunta funzione checkIP