Add InputStream type.

This commit is contained in:
Starfflame 2021-05-11 22:58:33 -05:00
parent b37499b7bc
commit 5dc66425f5
1 changed files with 48 additions and 0 deletions

48
InputStream.tl Normal file
View File

@ -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