Anonymous user
Module:IP: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
test new IPAddress class structure to make the objects more memory-efficient
(parts variable no longer needed either) |
(test new IPAddress class structure to make the objects more memory-efficient) |
||
Line 268:
end
return lhs.n < rhs.n
end
local function concatIPs(ip1, ip2)
return tostring(ip1) .. tostring(ip2)
end
local function ipToString(ip)
return ipString(ip[dataKey].parts)
end
Line 283 ⟶ 291:
end
data.version = data.parts.n == 2 and V4 or V6
-- Metamethods
Line 331 ⟶ 296:
__eq = ipEquals,
__lt = ipLessThan,
__concat = concatIPs,
__tostring = ipToString,
__index = function (self, key)
if key == dataKey then
return data
else
return IPAddress[key]
end
end,
})
end
-- Public methods
function IPAddress:getIP()
return ipString(self[dataKey].parts)
end
function IPAddress:getVersion()
return self[dataKey].version
end
function IPAddress:getHighestIP(bitLength)
return IPAddress.new(uniqueId, setHostBits(self[dataKey].parts, bitLength))
end
function IPAddress:getPrefix(bitLength)
return IPAddress.new(uniqueId, copyPrefix(self[dataKey].parts, bitLength))
end
function IPAddress:isIPv4()
return self[dataKey].version == V4
end
function IPAddress:isIPv6()
return self[dataKey].version == V6
end
function IPAddress:isInSubnet(subnet)
-- TODO Consider alternative of checking:
-- (ipFirst <= self and self <= ipLast)
if self:getVersion() == subnet:getVersion() then
local prefix = self:getPrefix(subnet:getBitLength())
return prefix == subnet:getPrefix()
end
return false
end
function IPAddress:getNextIP()
return IPAddress.new(uniqueId, copyChanged(self[dataKey].parts))
end
function IPAddress:getPreviousIP()
return IPAddress.new(uniqueId, copyChanged(self[dataKey].parts, true))
end
end
|