Changeset

8431:a5a03d40a20c

util.ip: Make bit string function into a method
author Kim Alvefur <zash@zash.se>
date Fri, 01 Dec 2017 04:37:33 +0100
parents 8430:a58d560aa8d5
children 8432:da807f4f706c
files util/ip.lua
diffstat 1 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/util/ip.lua	Fri Dec 01 04:39:12 2017 +0100
+++ b/util/ip.lua	Fri Dec 01 04:37:33 2017 +0100
@@ -51,12 +51,7 @@
 	return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt);
 end
 
-local function toBits(ip)
-	local result = "";
-	local fields = {};
-	if ip.proto == "IPv4" then
-		ip = ip.toV4mapped;
-	end
+function ip_methods.bits(ip)
 	ip = (ip.addr):upper();
 	ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end);
 	if not ip:match(":$") then fields[#fields] = nil; end
@@ -77,8 +72,15 @@
 	return result;
 end
 
+function ip_methods.bits_full(ip)
+	if ip.proto == "IPv4" then
+		ip = ip.toV4mapped;
+	end
+	return ip.bits;
+end
+
 local function commonPrefixLength(ipA, ipB)
-	ipA, ipB = toBits(ipA), toBits(ipB);
+	ipA, ipB = ipA.bits_full, ipB.bits_full;
 	for i = 1, 128 do
 		if ipA:sub(i,i) ~= ipB:sub(i,i) then
 			return i-1;