Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
tidy various things up
(add more methods to RawIP and rework IPAddress and Subnet to use them)
(tidy various things up)
Line 46:
-- Constructors
function RawIP.newFromIPv4(ipStr)
-- ReturnsReturn a RawIP object if ipStr is a valid IPv4 string. Otherwise,
-- returnsreturn nil.
-- This representation is for compatibility with IPv6 addresses.
local octets = Collection()
Line 61:
if num > 0 and s:match('^0') then
-- A redundant leading zero is for an IP in octal.
return falsenil
end
octets[i] = num
else
return falsenil
end
else
return falsenil
end
end
Line 81:
function RawIP.newFromIPv6(ipStr)
-- IfReturn a RawIP object if ipStr is a valid IPv6 string. Otherwise, return a collection of its parts.
-- Otherwise, return nil.
ipStr = ipStr:match('^%s*(.-)%s*$')
local _, n = ipStr:gsub(':', ':')
Line 119:
-- Methods
function RawIP:getVersion()
-- Return a string with the version of the IP protocol we are using.
return self.n == 2 and V4 or V6
end
 
function RawIP:isIPv4()
-- Return true if this is an IPv4 representation, and false otherwise.
return self.n == 2
end
 
function RawIP:isIPv6()
-- Return true if this is an IPv6 representation, and false otherwise.
return self.n == 8
end
Line 209 ⟶ 212:
 
function RawIP:_makeIPv6String()
-- Return an IPv6 string representation of the object. Behavior is undefined
-- undefined if the current object is IPv4.
local z1, z2 -- indices of run of zeros to be displayed as "::"
local zstart, zcount
Line 254 ⟶ 257:
 
function RawIP:_makeIPv4String()
-- Return an IPv4 string representation of the object. Behavior is undefined
-- undefined if the current object is IPv6.
local parts = Collection()
for i = 1, 2 do
Line 321 ⟶ 324:
 
do
local-- dataKey =is {} -- Aa unique key to access objects' internal data. This is needed
-- to access the RawIP objects contained in other IPAddress objects so that
-- they can be compared with the current object's RawIP object. This data
-- is not available to other classes or other modules.
local dataKey = {}
 
-- Metamethods that don't need upvalues
Line 389 ⟶ 396:
__tostring = ipToString,
__index = function (self, key)
-- If any code knows the unique data key, allow it to access
-- the data table.
if key == dataKey then
return data
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu