Anonymous user
Module:IP: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
try different format with underscores instead of private data
(test new IPAddress class structure to make the objects more memory-efficient) |
(try different format with underscores instead of private data) |
||
Line 163:
end
return ipv6String(ip)
end
Line 237 ⟶ 171:
local IPAddress = {}
IPAddress.__index = IPAddress
do
-- Private static methods
local function parseIPv4(ipStr)
-- If ipStr is a valid IPv4 string, return a collection of its parts.
-- Otherwise, return nil.
-- This representation is for compatibility with IPv6 addresses.
local
local s = ipStr:match('^%s*(.-)%s*$') .. '.'
octets:add(item)
end
if octets.n == 4 then
for i, s in ipairs(octets) do
if s:match('^%d+$') then
local num = tonumber(s)
if 0 <= num and num <= 255 then
if num > 0 and s:match('^0') then
-- A redundant leading zero is for an IP in octal.
return false
end
octets[i] = num
else
return false
end
else
return false
end
end
local parts = collection()
for i = 1, 3, 2 do
parts:add(octets[i] * 256 + octets[i+1])
end
return parts
end
return
end
local function
-- If ipStr is a valid IPv6 string, return a collection of its parts.
-- Otherwise, return nil.
ipStr = ipStr:match('^%s*(.-)%s*$')
ipStr, n = ipStr:gsub('::', string.rep(':', 9 - n))
end
local parts = collection()
for item in (ipStr .. ':'):gmatch('(.-):') do
parts:add(item)
end
if parts.n == 8 then
for i, s in ipairs(parts) do
if s == '' then
parts[i] = 0
else
local num = tonumber('0x' .. s)
if num and 0 <= num and num <= 65535 then
parts[i] = num
else
return false
end
end
end
return
end
return
end
Line 281 ⟶ 243:
function IPAddress.new(ip)
checkType('IPAddress.new', 1, ip, 'string')
local self = setmetatable({}, IPAddress)
-- Set initial values
self._parts = parseIPv4(ip) or parseIPv6(ip)
if not self._parts then
error('invalid IP', 2)
end
return self
end
-- Public methods
function IPAddress:getIP()
return ipString(self
end
function IPAddress:getVersion()
return self
end
function IPAddress:getHighestIP(bitLength)
return IPAddress.new(uniqueId, setHostBits(self
end
function IPAddress:getPrefix(bitLength)
return IPAddress.new(uniqueId, copyPrefix(self
end
function IPAddress:isIPv4()
return self
end
function IPAddress:isIPv6()
return self
end
Line 344 ⟶ 291:
function IPAddress:getNextIP()
return IPAddress.new(uniqueId, copyChanged(self
end
function IPAddress:getPreviousIP()
return IPAddress.new(uniqueId, copyChanged(self
end
function IPAddress:__eq(obj)
local lhs = self._parts
local rhs = obj._parts
if lhs.n == rhs.n then
for i = 1, lhs.n do
if lhs[i] ~= rhs[i] then
return false
end
end
return true
end
return false
end
function IPAddress:__lt(ip1, ip2)
local lhs = self._parts
local rhs = obj._parts
if lhs.n == rhs.n then
for i = 1, lhs.n do
if lhs[i] ~= rhs[i] then
return lhs[i] < rhs[i]
end
end
return false
end
return lhs.n < rhs.n
end
function IPAddress:__concat(obj)
return tostring(self) .. tostring(obj)
end
function IPAddress:__tostring()
return ipString(self._parts)
end
end
|