34 lines
571 B
Lua
34 lines
571 B
Lua
_G.buffer = {}
|
|
function buffer.create(w,c) -- worker, close
|
|
local t={}
|
|
t.b=""
|
|
function t.w(s,d)
|
|
s.b=s.b..tostring(d)
|
|
end
|
|
t.write = t.w
|
|
function t.r(s,l)
|
|
if type(l) == "number" then
|
|
local ns,bs=s.b:sub(1,l+1),s.b:sub(l+2)
|
|
s.b=bs
|
|
return ns
|
|
elseif type(l) == "string" then
|
|
local oS=s.b
|
|
if l == "*a" then
|
|
s.b=""
|
|
return oS
|
|
elseif l == "*l" then
|
|
S=s.b:find("\n") or #s.b
|
|
s.b=s.b:sub(S+1)
|
|
rS = oS:sub(1,S-1)
|
|
if rS:len() < 1 then return nil end
|
|
return rS
|
|
end
|
|
end
|
|
end
|
|
t.read = t.r
|
|
t.close = c
|
|
w(t)
|
|
return t
|
|
end
|
|
|