diff options
Diffstat (limited to 'builtin/common/misc_helpers.lua')
-rw-r--r-- | builtin/common/misc_helpers.lua | 39 |
1 files changed, 21 insertions, 18 deletions
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 -------------------------------------------------------------------------------- |