From 5aeeb219e3fc2f37d3996a3df3b660afcc5f7599 Mon Sep 17 00:00:00 2001
From: kwolekr <kwolekr@minetest.net>
Date: Thu, 15 Jan 2015 18:03:43 -0500
Subject: [PATCH] Simplify deleteblocks chat command argument parsing

Add optional core.pos_to_string decimal place rounding
Move core.string_to_pos to builtin/common/misc_helpers.lua for consistency
---
 builtin/common/misc_helpers.lua | 41 +++++++++++++++++++++++++++++++--
 builtin/game/chatcommands.lua   | 18 +++++----------
 builtin/game/misc.lua           | 24 -------------------
 3 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index a86631e1d..deeba788e 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -498,10 +498,47 @@ function core.explode_scrollbar_event(evt)
 end
 
 --------------------------------------------------------------------------------
-function core.pos_to_string(pos)
-	return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
+function core.pos_to_string(pos, decimal_places)
+	local x = pos.x
+	local y = pos.y
+	local z = pos.z
+	if decimal_places ~= nil then
+		x = string.format("%." .. decimal_places .. "f", x)
+		y = string.format("%." .. decimal_places .. "f", y)
+		z = string.format("%." .. decimal_places .. "f", z)
+	end
+	return "(" .. x .. "," .. y .. "," .. z .. ")"
+end
+
+--------------------------------------------------------------------------------
+function core.string_to_pos(value)
+	if value == nil then
+		return nil
+	end
+
+	local p = {}
+	p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
+	if p.x and p.y and p.z then
+		p.x = tonumber(p.x)
+		p.y = tonumber(p.y)
+		p.z = tonumber(p.z)
+		return p
+	end
+	local p = {}
+	p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
+	if p.x and p.y and p.z then
+		p.x = tonumber(p.x)
+		p.y = tonumber(p.y)
+		p.z = tonumber(p.z)
+		return p
+	end
+	return nil
 end
 
+assert(core.string_to_pos("10.0, 5, -2").x == 10)
+assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
+assert(core.string_to_pos("asd, 5, -2)") == nil)
+
 --------------------------------------------------------------------------------
 function table.copy(t, seen)
 	local n = {}
diff --git a/builtin/game/chatcommands.lua b/builtin/game/chatcommands.lua
index 18b5dbe72..8ce1cfef5 100644
--- a/builtin/game/chatcommands.lua
+++ b/builtin/game/chatcommands.lua
@@ -419,24 +419,18 @@ core.register_chatcommand("deleteblocks", {
 			p1 = player:getpos()
 			p2 = p1
 		else
-			p1.x, p1.y, p1.z, p2.x, p2.y, p2.z = string.match(param,
-				"^%(([%d.-]+), *([%d.-]+), *([%d.-]+)%) *%(([%d.-]+), *([%d.-]+), *([%d.-]+)%)$")
-			p1.x = tonumber(p1.x)
-			p1.y = tonumber(p1.y)
-			p1.z = tonumber(p1.z)
-			p2.x = tonumber(p2.x)
-			p2.y = tonumber(p2.y)
-			p2.z = tonumber(p2.z)
-
-			if p1.x == nil or p1.y == nil or p1.z == nil or
-				p2.x == nil or p2.y == nil or p2.z == nil then
+			local pos1, pos2 = unpack(param:split(") ("))
+			p1 = core.string_to_pos(pos1 .. ")")
+			p2 = core.string_to_pos("(" .. pos2)
+
+			if p1 == nil or p2 == nil then
 				return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
 			end
 		end
 
 		if core.delete_area(p1, p2) then
 			return true, "Successfully cleared area ranging from " ..
-				core.pos_to_string(p1) .. " to " .. core.pos_to_string(p2)
+				core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
 		else
 			return false, "Failed to clear one or more blocks in area"
 		end
diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua
index a392386f1..c31df541d 100644
--- a/builtin/game/misc.lua
+++ b/builtin/game/misc.lua
@@ -93,30 +93,6 @@ function core.get_node_group(name, group)
 	return core.get_item_group(name, group)
 end
 
-function core.string_to_pos(value)
-	local p = {}
-	p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
-	if p.x and p.y and p.z then
-		p.x = tonumber(p.x)
-		p.y = tonumber(p.y)
-		p.z = tonumber(p.z)
-		return p
-	end
-	local p = {}
-	p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
-	if p.x and p.y and p.z then
-		p.x = tonumber(p.x)
-		p.y = tonumber(p.y)
-		p.z = tonumber(p.z)
-		return p
-	end
-	return nil
-end
-
-assert(core.string_to_pos("10.0, 5, -2").x == 10)
-assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
-assert(core.string_to_pos("asd, 5, -2)") == nil)
-
 function core.setting_get_pos(name)
 	local value = core.setting_get(name)
 	if not value then
-- 
GitLab