forked from Mirrorlandia_minetest/minetest
Builtin: Optimize misc helpers (#12377)
Also add formspec_escape unit test
This commit is contained in:
parent
fe299e24d6
commit
e8b2954586
@ -22,7 +22,6 @@ local LIST_FORMSPEC_DESCRIPTION = [[
|
|||||||
|
|
||||||
local F = core.formspec_escape
|
local F = core.formspec_escape
|
||||||
local S = core.get_translator("__builtin")
|
local S = core.get_translator("__builtin")
|
||||||
local check_player_privs = core.check_player_privs
|
|
||||||
|
|
||||||
|
|
||||||
-- CHAT COMMANDS FORMSPEC
|
-- CHAT COMMANDS FORMSPEC
|
||||||
@ -58,10 +57,11 @@ local function build_chatcommands_formspec(name, sel, copy)
|
|||||||
.. "any entry in the list.").. "\n" ..
|
.. "any entry in the list.").. "\n" ..
|
||||||
S("Double-click to copy the entry to the chat history.")
|
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
|
for i, data in ipairs(mod_cmds) do
|
||||||
rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
|
rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
|
||||||
for j, cmds in ipairs(data[2]) do
|
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(
|
rows[#rows + 1] = ("%s,1,%s,%s"):format(
|
||||||
has_priv and COLOR_GREEN or COLOR_GRAY,
|
has_priv and COLOR_GREEN or COLOR_GRAY,
|
||||||
cmds[1], F(cmds[2].params))
|
cmds[1], F(cmds[2].params))
|
||||||
|
@ -204,7 +204,7 @@ end
|
|||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function string:trim()
|
function string:trim()
|
||||||
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
return self:match("^%s*(.-)%s*$")
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
@ -245,16 +245,16 @@ function math.round(x)
|
|||||||
return math.ceil(x - 0.5)
|
return math.ceil(x - 0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local formspec_escapes = {
|
||||||
|
["\\"] = "\\\\",
|
||||||
|
["["] = "\\[",
|
||||||
|
["]"] = "\\]",
|
||||||
|
[";"] = "\\;",
|
||||||
|
[","] = "\\,"
|
||||||
|
}
|
||||||
function core.formspec_escape(text)
|
function core.formspec_escape(text)
|
||||||
if text ~= nil then
|
-- Use explicit character set instead of dot here because it doubles the performance
|
||||||
text = string.gsub(text,"\\","\\\\")
|
return text and text:gsub("[\\%[%];,]", formspec_escapes)
|
||||||
text = string.gsub(text,"%]","\\]")
|
|
||||||
text = string.gsub(text,"%[","\\[")
|
|
||||||
text = string.gsub(text,";","\\;")
|
|
||||||
text = string.gsub(text,",","\\,")
|
|
||||||
end
|
|
||||||
return text
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -265,18 +265,21 @@ function core.wrap_text(text, max_length, as_table)
|
|||||||
return as_table and {text} or text
|
return as_table and {text} or text
|
||||||
end
|
end
|
||||||
|
|
||||||
for word in text:gmatch('%S+') do
|
local line_length = 0
|
||||||
local cur_length = #table.concat(line, ' ')
|
for word in text:gmatch("%S+") do
|
||||||
if cur_length > 0 and cur_length + #word + 1 >= max_length then
|
if line_length > 0 and line_length + #word + 1 >= max_length then
|
||||||
-- word wouldn't fit on current line, move to next line
|
-- word wouldn't fit on current line, move to next line
|
||||||
table.insert(result, table.concat(line, ' '))
|
table.insert(result, table.concat(line, " "))
|
||||||
line = {}
|
line = {word}
|
||||||
|
line_length = #word
|
||||||
|
else
|
||||||
|
table.insert(line, word)
|
||||||
|
line_length = line_length + 1 + #word
|
||||||
end
|
end
|
||||||
table.insert(line, word)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(result, table.concat(line, ' '))
|
table.insert(result, table.concat(line, " "))
|
||||||
return as_table and result or table.concat(result, '\n')
|
return as_table and result or table.concat(result, "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -163,3 +163,11 @@ describe("table", function()
|
|||||||
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
|
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
|
||||||
end)
|
end)
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user