From 0e6a63a48533dfd4b34a44282f5f684881defaa3 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 6 Jun 2016 11:04:24 +0200 Subject: [PATCH] small refactoring, using new util function tokenize instead of split in slash.lua --- slash.lua | 30 +++++++++++++++--------------- util.lua | 8 ++++++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/slash.lua b/slash.lua index 6099971..784d9ff 100644 --- a/slash.lua +++ b/slash.lua @@ -1,7 +1,7 @@ SLASH_AUX1 = '/aux' function SlashCmdList.AUX(command) if not command then return end - local arguments = Aux.util.split(command, " ") + local arguments = Aux.util.tokenize(command) if command == 'clear history' then Aux.persistence.load_dataset().history = nil Aux.log('History cleared.') @@ -47,11 +47,11 @@ function SlashCmdList.AUX(command) Aux.log('Ignoring of owner '..(aux_ignore_owner and 'enabled' or 'disabled')..'.') elseif arguments[1] == 'addchar' and arguments[2] then local exists = false - for i=2, table.getn(arguments) do + for i=2, getn(arguments) do exists = false - if arguments[i] ~= "" then - arguments[i] = strupper(strsub(arguments[i], 1, 1))..strsub(arguments[i], 2) -- force uppercase - for i_aux,v in ipairs(aux_characters) do + if arguments[i] ~= '' then + arguments[i] = string.gsub(arguments[i], '^%l', strupper) -- force uppercase + for i_aux,_ in ipairs(aux_characters) do if (aux_characters[i_aux] == arguments[i]) then exists = true break @@ -68,7 +68,7 @@ function SlashCmdList.AUX(command) if arguments[i] ~= "" then arguments[i] = strupper(strsub(arguments[i], 1, 1))..strsub(arguments[i], 2) for i_aux,v in ipairs(aux_characters) do - if (aux_characters[i_aux] == arguments[i]) then + if aux_characters[i_aux] == arguments[i] then table.remove(aux_characters, i_aux) Aux.log('Character "'..arguments[i]..'" removed.') break @@ -77,20 +77,20 @@ function SlashCmdList.AUX(command) end end elseif command == 'chars' then - local chars = nil - local num = table.getn(aux_characters); + local chars + local num = table.getn(aux_characters) for i,v in ipairs(aux_characters) do - if( i ~= num) then - if chars ~= nil then - chars = chars .. v .. ", "; + if i ~= num then + if chars then + chars = chars .. v .. ", " else - chars = v .. ", "; + chars = v .. ", " end else - if chars ~= nil then - chars = chars .. v; + if chars then + chars = chars .. v else - chars = v; + chars = v end end end diff --git a/util.lua b/util.lua index 1402b21..235ccbe 100644 --- a/util.lua +++ b/util.lua @@ -334,6 +334,14 @@ function public.join(array, separator) return str end +function public.tokenize(str) + local tokens = {} + for token in string.gfind(str, '%S+') do + tinsert(tokens, token) + end + return tokens +end + function public.split(str, separator) local array = {}