string escaping

master
Juraj Vajda 2018-11-13 18:48:25 -05:00
parent 13d3a5d305
commit 0b49973d11
2 changed files with 21 additions and 3 deletions

20
api.lua
View File

@ -20,6 +20,23 @@ function x_marketplace.isnan(value)
return value ~= value
end
local function esc(x)
return (x:gsub('%%', '%%%%')
:gsub('^%^', '%%^')
:gsub('%$$', '%%$')
:gsub('%(', '%%(')
:gsub('%)', '%%)')
:gsub('%.', '%%.')
:gsub('%,', '%%,')
:gsub('%;', '%%;')
:gsub('%[', '%%[')
:gsub('%]', '%%]')
:gsub('%*', '%%*')
:gsub('%+', '%%+')
:gsub('%-', '%%-')
:gsub('%?', '%%?'))
end
-- rounds a number to the nearest decimal places
local function round(val, decimal)
if (decimal) then
@ -34,8 +51,9 @@ end
-- @return string with found items, if no items found returns boolean false
function x_marketplace.store_find(string)
local found = ""
local str = esc(string)
for k, v in pairs(x_marketplace.store_list) do
if string.find(k, string) then
if string.find(k, str) then
found = found..k.." buy: "..string.format("%.2f", v.buy).." sell: "..string.format("%.2f", v.sell).."\n"
end
end

View File

@ -22,7 +22,7 @@ minetest.register_chatcommand("mp", {
return false, minetest.colorize(x_marketplace.colors.red, "MARKET PLACE: You need to write the item name you want to find. example: /mp find default:stone. See some suggestion from the store: ")..x_marketplace.store_get_random()
end
local items = x_marketplace.store_find(minetest.formspec_escape(params[2]))
local items = x_marketplace.store_find(params[2])
if not items then
return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: Oops there is no item like this in the store. Check out other items in the store: ")..x_marketplace.store_get_random()
@ -353,7 +353,7 @@ minetest.register_chatcommand("mp", {
-- item not in store
if not x_marketplace.store_list[params[2]] then
local suggestions = x_marketplace.store_find(minetest.formspec_escape(params[2]))
local suggestions = x_marketplace.store_find(params[2])
-- try suggest item from store, else show random items
if suggestions then