From 715bf47aba140701488cf8126c7e1db7fb2a2be8 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 28 Sep 2022 20:57:24 +0200 Subject: [PATCH] Clean up text.escape_pattern --- text.lua | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/text.lua b/text.lua index bfe2a40..c28ae72 100644 --- a/text.lua +++ b/text.lua @@ -101,24 +101,13 @@ function is_hexadecimal(byte) return byte >= zero and byte <= nine or byte >= letter_a and byte <= letter_f end -magic_chars = { - "%", - "(", - ")", - ".", - "+", - "-", - "*", - "?", - "[", - "^", - "$" -} -local magic_charset = {} -for _, magic_char in pairs(magic_chars) do table.insert(magic_charset, "%" .. magic_char) end -magic_charset = "[" .. table.concat(magic_charset) .. "]" +magic_charset = "[" .. ("%^$+-*?.[]()"):gsub(".", "%%%1") .. "]" -function escape_magic_chars(text) return text:gsub("(" .. magic_charset .. ")", "%%%1") end +function escape_pattern(text) + return text:gsub(magic_charset, "%%%1") +end + +escape_magic_chars = escape_pattern local keywords = modlib.table.set{"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"} keywords["goto"] = true -- Lua 5.2 (LuaJIT) support