aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-16 19:17:50 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2019-07-16 19:17:50 +0000
commitabadb6492b65b18bb54968f8f86e5ca770443335 (patch)
treec34bdf7eeb026e6d93bdabcb9a914743820b3822
parentfc92617f54327914b037f150e27e68235798b8ae (diff)
downloadlua-inet-abadb6492b65b18bb54968f8f86e5ca770443335.tar.gz
lua-inet-abadb6492b65b18bb54968f8f86e5ca770443335.tar.xz
lua-inet-abadb6492b65b18bb54968f8f86e5ca770443335.zip
move __lt() aka. < to :contains(), to fix sortability
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r--lua/inet/init.lua66
1 files changed, 60 insertions, 6 deletions
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