Diff

util/ip.lua @ 8432:da807f4f706c

util.ip: Simplify bitstring method
author Kim Alvefur <zash@zash.se>
date Fri, 01 Dec 2017 04:32:20 +0100
parent 8431:a5a03d40a20c
child 8433:ca7f8e60410a
line wrap: on
line diff
--- a/util/ip.lua	Fri Dec 01 04:37:33 2017 +0100
+++ b/util/ip.lua	Fri Dec 01 04:32:20 2017 +0100
@@ -6,6 +6,7 @@
 --
 
 local net = require "util.net";
+local hex = require "util.hex";
 
 local ip_methods = {};
 
@@ -52,24 +53,7 @@
 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
-	for i, field in ipairs(fields) do
-		if field:len() == 0 and i ~= 1 and i ~= #fields then
-			for _ = 1, 16 * (9 - #fields) do
-				result = result .. "0";
-			end
-		else
-			for _ = 1, 4 - field:len() do
-				result = result .. "0000";
-			end
-			for j = 1, field:len() do
-				result = result .. hex2bits[field:sub(j, j)];
-			end
-		end
-	end
-	return result;
+	return hex.to(ip.packed):upper():gsub(".", hex2bits);
 end
 
 function ip_methods.bits_full(ip)