Add InputStream type.
This commit is contained in:
parent
b37499b7bc
commit
5dc66425f5
|
@ -0,0 +1,48 @@
|
|||
local type InputStream = record
|
||||
str: string
|
||||
offset: number
|
||||
refill: function(): string
|
||||
end
|
||||
|
||||
local istream_mt = {__index = InputStream}
|
||||
|
||||
function InputStream:_manageBuffer()
|
||||
if self.offset > #self.str then
|
||||
self.str = self.refill()
|
||||
self.offset = 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- constructors
|
||||
function InputStream:new(): InputStream
|
||||
return setmetatable(
|
||||
{
|
||||
string = "",
|
||||
offset = 1
|
||||
} as InputStream,
|
||||
istream_mt)
|
||||
end
|
||||
|
||||
-- setters/getters
|
||||
function InputStream:setRefill(func: function(): string)
|
||||
self.refill = func
|
||||
end
|
||||
|
||||
--
|
||||
function InputStream:readCurrentCharacter(): string
|
||||
self:_manageBuffer()
|
||||
return self.str:sub(self.offset, 1)
|
||||
end
|
||||
|
||||
function InputStream:advanceOffset(): string
|
||||
self.offset = self.offset + 1
|
||||
self:_manageBuffer()
|
||||
return self.str:sub(self.offset, 1)
|
||||
end
|
||||
|
||||
return InputStream
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue