mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2024-11-05 10:00:28 +01:00
Made parse function 2.5 times faster, for no good reason. I guess if I'm going to do some optimizations in a function, I might as well go all out
This commit is contained in:
parent
eb2326cd79
commit
7f8eb22fc4
52
util.lua
52
util.lua
|
@ -9,31 +9,41 @@ module "irc"
|
|||
|
||||
--protocol parsing
|
||||
function parse(line)
|
||||
local colonsplit = line:find(":", 2)
|
||||
local last
|
||||
if colonsplit then
|
||||
last = line:sub(colonsplit+1)
|
||||
line = line:sub(1, colonsplit-2)
|
||||
end
|
||||
local prefix
|
||||
local lineStart = 1
|
||||
if line:sub(1,1) == ":" then
|
||||
local space = line:find(" ")
|
||||
prefix = line:sub(2, space-1)
|
||||
lineStart = space
|
||||
end
|
||||
|
||||
local trailtoken = line:find(":", lineStart)
|
||||
local lineStop = -1
|
||||
local trailing
|
||||
if trailtoken then
|
||||
trailing = line:sub(trailtoken + 1)
|
||||
lineStop = trailtoken - 2
|
||||
end
|
||||
|
||||
local prefix
|
||||
if line:sub(1,1) == ":" then
|
||||
local space = line:find(" ")
|
||||
prefix = line:sub(2, space-1)
|
||||
line = line:sub(space)
|
||||
end
|
||||
|
||||
local params = {}
|
||||
local it, state, init = line:gmatch("(%S+)")
|
||||
local cmd = it(state, init)
|
||||
local params = {}
|
||||
|
||||
for sub in it, state, init do
|
||||
params[#params + 1] = sub
|
||||
end
|
||||
local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
|
||||
local pos = cmdEnd + 1
|
||||
while true do
|
||||
local _, stop, param = line:find("(%S+)", pos)
|
||||
params[#params + 1] = param
|
||||
pos = stop + 1
|
||||
|
||||
if last then params[#params + 1] = last end
|
||||
if pos >= lineStop then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return prefix, cmd, params
|
||||
if trailing then
|
||||
params[#params + 1] = trailing
|
||||
end
|
||||
|
||||
return prefix, cmd, params
|
||||
end
|
||||
|
||||
function parseNick(nick)
|
||||
|
|
Loading…
Reference in New Issue
Block a user