Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
add IPAddress __concat; minor style changes
(use some functions from Module:IPblock to start implementation)
(add IPAddress __concat; minor style changes)
Line 11:
 
--------------------------------------------------------------------------------
-- TODO Remove items from these notes when satisfied they are ok.
 
-- IPAddress does not need mt:__le(obj) as Lua evaluates (a <= b) as (not (b < a))
Line 25:
-- which might be different from the input. Example:
-- input "1:02:003:0004:0:0:0:0" is "1:2:3:4::" after normalization.
 
-- Should obj:getIPParts() be removed?
-- Instead, move the metamethods into IPAddress.new(ip) so they can
-- access the data upvalue?
 
------------------------------------------------------------------------
Line 54 ⟶ 58:
end
 
local function ipv6_stringipv6String(ip)
-- Return a string equivalent to the given IPv6 address.
local z1, z2 -- indices of run of zeros to be displayed as "::"
Line 98 ⟶ 102:
end
 
local function ip_stringipString(ip)
-- Return a string equivalent to given IP address (IPv4 or IPv6).
if ip.n == 2 then
Line 111 ⟶ 115:
return table.concat(parts, '.')
end
return ipv6_stringipv6String(ip)
end
 
local function ipv4_addressipv4Address(data, ip_stripStr)
-- If ip_stripStr is a valid IPv4 string, store its parts in data and
-- return true. Otherwise, return false.
-- This representation is for compatibility with IPv6 addresses.
local octets = collection()
local s = ip_stripStr:match('^%s*(.-)%s*$') .. '.'
for item in s:gmatch('(.-)%.') do
octets:add(item)
Line 149 ⟶ 153:
end
 
local function ipv6_addressipv6Address(data, ip_stripStr)
-- If ip_stripStr is a valid IPv6 string, store its parts in data and
-- return true. Otherwise, return false.
ip_stripStr = ip_stripStr:match('^%s*(.-)%s*$')
local _, n = ip_stripStr:gsub(':', ':')
if n < 7 then
ip_stripStr, n = ip_stripStr:gsub('::', string.rep(':', 9 - n))
end
local parts = collection()
for item in (ip_stripStr .. ':'):gmatch('(.-):') do
parts:add(item)
end
Line 245 ⟶ 249:
 
-- Metamethods
function mt:__concat(obj)
return tostring(self) .. tostring(obj)
end
 
function mt:__eq(obj)
local lhs, rhs = self:getIPParts(), obj:getIPParts()
Line 272 ⟶ 280:
 
function mt:__tostring()
return ip_stringipString(self:getIPParts())
end
end
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu