aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-02-17 18:24:58 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-02-17 18:24:58 +0000
commit46676a465037c20b761b02d716fe30fdfd0a3f14 (patch)
tree91579169dc0d9be0d45fdacc4af07b1ecf083ea7
parenta1ba1999ff0b6b9b1b2c756d678e18e9b405dec5 (diff)
downloadlua-inet-46676a465037c20b761b02d716fe30fdfd0a3f14.tar.gz
lua-inet-46676a465037c20b761b02d716fe30fdfd0a3f14.tar.xz
lua-inet-46676a465037c20b761b02d716fe30fdfd0a3f14.zip
fix cross-family sorting
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r--README.rst3
-rw-r--r--lua/inet/core.lua26
2 files changed, 28 insertions, 1 deletions
diff --git a/README.rst b/README.rst
index 381ce9b..c7c0b0e 100644
--- a/README.rst
+++ b/README.rst
@@ -279,7 +279,8 @@ Compares ``inet`` instances according to the sort order.
inet('192.0.2.0/26') < inet('192.0.2.64/26') -- returns true
inet('192.0.2.0/24') < inet('192.0.2.0/26') -- returns true
inet('192.0.2.0/26') < inet('192.0.2.1/26') -- returns true
-
+ inet('192.0.2.0/26') < inet('2001:db8::1/64') -- returns true
+ inet('2001:db8::1/64') < inet('192.0.2.0/26') -- returns false
``==`` and ``~=``
~~~~~~~~~~~~~~~~~
diff --git a/lua/inet/core.lua b/lua/inet/core.lua
index 7037885..5008da9 100644
--- a/lua/inet/core.lua
+++ b/lua/inet/core.lua
@@ -360,6 +360,7 @@ function inet4:clone()
end
function inet4:contains(other)
+ if is_inet6(other) then return false end
if self.mask >= other.mask then
return false
end
@@ -370,6 +371,12 @@ function inet4:contains(other)
end
function inet4:__lt(other)
+ if not is_inet4(other) then
+ if is_inet6(other) then
+ return true
+ end
+ return nil
+ end
if self.bip == other.bip then
return self.mask < other.mask
end
@@ -377,6 +384,12 @@ function inet4:__lt(other)
end
function inet4:__le(other)
+ if not is_inet4(other) then
+ if is_inet6(other) then
+ return true
+ end
+ return nil
+ end
if self.mask < other.mask then
return false
end
@@ -391,6 +404,7 @@ function inet4:__le(other)
end
function inet4:__eq(other)
+ if getmetatable(other) ~= inet4 then return false end
return self.bip == other.bip and self.mask == other.mask
end
@@ -603,6 +617,12 @@ function inet6:contains(other)
end
function inet6:__lt(other)
+ if not is_inet6(other) then
+ if is_inet4(other) then
+ return false
+ end
+ return nil
+ end
-- self < other
local spcs = self.pcs
local opcs = other.pcs
@@ -620,6 +640,12 @@ function inet6:__lt(other)
end
function inet6:__le(other)
+ if not is_inet6(other) then
+ if is_inet4(other) then
+ return false
+ end
+ return nil
+ end
-- self <= other
local spcs = self.pcs
local opcs = other.pcs