From 51707f3917ececa96dff3ac1ebc405cd0eec39f5 Mon Sep 17 00:00:00 2001 From: Constantin Piber Date: Tue, 19 Apr 2022 11:52:13 +0200 Subject: [PATCH] Escape % before using strings in gsub --- helpers.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/helpers.lua b/helpers.lua index 1e3b198..db7550b 100644 --- a/helpers.lua +++ b/helpers.lua @@ -10,6 +10,7 @@ -- Copyright (C) 2018-2019 Nguyễn Gia Phong -- Copyright (C) 2019 Alexander Koch -- Copyright (C) 2019 Enric Morales +-- Copyright (C) 2022 Constantin Piber -- -- This file is part of Vicious. -- @@ -33,6 +34,7 @@ local rawget = rawget local require = require local tonumber = tonumber local tostring = tostring +local type = type local io = { open = io.open, popen = io.popen } local setmetatable = setmetatable local getmetatable = getmetatable @@ -164,11 +166,12 @@ end -- {{{ Format a string with args function helpers.format(format, args) for var, val in pairs(args) do - format = format:gsub("$" .. (tonumber(var) and var or - var:gsub("[-+?*]", function(i) return "%"..i end)), - val) + if tonumber(var) == nil then + var = var:gsub("[-+?*]", function(i) return "%"..i end) + end + if type(val) == "string" then val = val:gsub("%%", "%%%%") end + format = format:gsub("$" .. var, val) end - return format end -- }}}