diff --git a/InputStream.tl b/InputStream.tl new file mode 100644 index 0000000..f8782e9 --- /dev/null +++ b/InputStream.tl @@ -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 + + +