From abadb6492b65b18bb54968f8f86e5ca770443335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Tue, 16 Jul 2019 19:17:50 +0000 Subject: move __lt() aka. < to :contains(), to fix sortability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- lua/inet/init.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) (limited to 'lua') diff --git a/lua/inet/init.lua b/lua/inet/init.lua index 836e94b..328032f 100644 --- a/lua/inet/init.lua +++ b/lua/inet/init.lua @@ -321,14 +321,21 @@ function inet4:__pow(n) return new_inet4(self.bip, self.mask + n) end -function inet4:__lt(other) - if self.mask <= other.mask then +function inet4:contains(other) + if self.mask >= other.mask then return false end - local mask = other.mask - local selfnet = replace(self.bip, 0, 0, 32-mask) - local othernet = replace(other.bip, 0, 0, 32-mask) - return selfnet == othernet + local mask = self.mask -- make test + local self_netbits = replace(self.bip, 0, 0, 32-mask) + local other_netbits = replace(other.bip, 0, 0, 32-mask) + return self_netbits == other_netbits +end + +function inet4:__lt(other) + if self.bip == other.bip then + return self.mask < other.mask + end + return self.bip < other.bip end function inet4:__le(other) @@ -488,6 +495,53 @@ function inet6:clone() return new_inet6(self.pcs, self.mask) end +function inet6:contains(other) + -- self contains other + local mask = self.mask + + if mask > other.mask then + return false + end + + local snet = self:network() + local onet = (other / mask):network() + + return snet == onet +end + +function inet6:__lt(other) + -- self < other + local spcs = self.pcs + local opcs = other.pcs + + for i=1,8 do + if spcs[i] < opcs[i] then + return true + end + if spcs[i] > opcs[i] then + return false + end + end + + return self.mask < other.mask +end + +function inet6:__le(other) + -- self <= other + local spcs = self.pcs + local opcs = other.pcs + for i=1,8 do + if spcs[i] < opcs[i] then + return true + end + if spcs[i] > opcs[i] then + return false + end + end + + return self.mask <= other.mask +end + function inet6:__eq(other) if self.mask ~= other.mask then return false -- cgit v1.2.1