aboutsummaryrefslogtreecommitdiff
path: root/builtin/common
diff options
context:
space:
mode:
authorLars Müller <34514239+appgurueu@users.noreply.github.com>2022-05-27 21:40:38 +0200
committerGitHub <noreply@github.com>2022-05-27 21:40:38 +0200
commite8b2954586ebd9e35e3f9f7230ff6713b65c4967 (patch)
tree11b7af3b40340ba499b3523f7ff0e585cde6c6c2 /builtin/common
parentfe299e24d6ad5bb58e077ec372737b117ac6cf94 (diff)
downloadminetest-e8b2954586ebd9e35e3f9f7230ff6713b65c4967.tar.gz
minetest-e8b2954586ebd9e35e3f9f7230ff6713b65c4967.tar.bz2
minetest-e8b2954586ebd9e35e3f9f7230ff6713b65c4967.zip
Builtin: Optimize misc helpers (#12377)
Also add formspec_escape unit test
Diffstat (limited to 'builtin/common')
-rw-r--r--builtin/common/information_formspecs.lua4
-rw-r--r--builtin/common/misc_helpers.lua39
-rw-r--r--builtin/common/tests/misc_helpers_spec.lua8
3 files changed, 31 insertions, 20 deletions
diff --git a/builtin/common/information_formspecs.lua b/builtin/common/information_formspecs.lua
index 3405263bf..1445a017c 100644
--- a/builtin/common/information_formspecs.lua
+++ b/builtin/common/information_formspecs.lua
@@ -22,7 +22,6 @@ local LIST_FORMSPEC_DESCRIPTION = [[
local F = core.formspec_escape
local S = core.get_translator("__builtin")
-local check_player_privs = core.check_player_privs
-- CHAT COMMANDS FORMSPEC
@@ -58,10 +57,11 @@ local function build_chatcommands_formspec(name, sel, copy)
.. "any entry in the list.").. "\n" ..
S("Double-click to copy the entry to the chat history.")
+ local privs = core.get_player_privs(name)
for i, data in ipairs(mod_cmds) do
rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
for j, cmds in ipairs(data[2]) do
- local has_priv = check_player_privs(name, cmds[2].privs)
+ local has_priv = privs[cmds[2].privs]
rows[#rows + 1] = ("%s,1,%s,%s"):format(
has_priv and COLOR_GREEN or COLOR_GRAY,
cmds[1], F(cmds[2].params))
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index 83f17da7b..d2356b505 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -204,7 +204,7 @@ end
--------------------------------------------------------------------------------
function string:trim()
- return (self:gsub("^%s*(.-)%s*$", "%1"))
+ return self:match("^%s*(.-)%s*$")
end
--------------------------------------------------------------------------------
@@ -245,16 +245,16 @@ function math.round(x)
return math.ceil(x - 0.5)
end
-
+local formspec_escapes = {
+ ["\\"] = "\\\\",
+ ["["] = "\\[",
+ ["]"] = "\\]",
+ [";"] = "\\;",
+ [","] = "\\,"
+}
function core.formspec_escape(text)
- if text ~= nil then
- text = string.gsub(text,"\\","\\\\")
- text = string.gsub(text,"%]","\\]")
- text = string.gsub(text,"%[","\\[")
- text = string.gsub(text,";","\\;")
- text = string.gsub(text,",","\\,")
- end
- return text
+ -- Use explicit character set instead of dot here because it doubles the performance
+ return text and text:gsub("[\\%[%];,]", formspec_escapes)
end
@@ -265,18 +265,21 @@ function core.wrap_text(text, max_length, as_table)
return as_table and {text} or text
end
- for word in text:gmatch('%S+') do
- local cur_length = #table.concat(line, ' ')
- if cur_length > 0 and cur_length + #word + 1 >= max_length then
+ local line_length = 0
+ for word in text:gmatch("%S+") do
+ if line_length > 0 and line_length + #word + 1 >= max_length then
-- word wouldn't fit on current line, move to next line
- table.insert(result, table.concat(line, ' '))
- line = {}
+ table.insert(result, table.concat(line, " "))
+ line = {word}
+ line_length = #word
+ else
+ table.insert(line, word)
+ line_length = line_length + 1 + #word
end
- table.insert(line, word)
end
- table.insert(result, table.concat(line, ' '))
- return as_table and result or table.concat(result, '\n')
+ table.insert(result, table.concat(line, " "))
+ return as_table and result or table.concat(result, "\n")
end
--------------------------------------------------------------------------------
diff --git a/builtin/common/tests/misc_helpers_spec.lua b/builtin/common/tests/misc_helpers_spec.lua
index f6ad96619..7d046d5b7 100644
--- a/builtin/common/tests/misc_helpers_spec.lua
+++ b/builtin/common/tests/misc_helpers_spec.lua
@@ -163,3 +163,11 @@ describe("table", function()
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
end)
end)
+
+describe("formspec_escape", function()
+ it("escapes", function()
+ assert.equal(nil, core.formspec_escape(nil))
+ assert.equal("", core.formspec_escape(""))
+ assert.equal("\\[Hello\\\\\\[", core.formspec_escape("[Hello\\["))
+ end)
+end)