Skip to content

Commit

Permalink
Escape % before using strings in gsub
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiber authored and McSinyx committed May 3, 2022
1 parent b20f68b commit 51707f3
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-- Copyright (C) 2018-2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
-- Copyright (C) 2019 Alexander Koch <lynix47@gmail.com>
-- Copyright (C) 2019 Enric Morales <me@enric.me>
-- Copyright (C) 2022 Constantin Piber <cp.piber@gmail.com>
--
-- This file is part of Vicious.
--
Expand All @@ -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
Expand Down Expand Up @@ -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
-- }}}
Expand Down

0 comments on commit 51707f3

Please sign in to comment.