From b88cc3a85ecd2d1bb5baf1466763e9e02d006b26 Mon Sep 17 00:00:00 2001 From: Starfflame Date: Wed, 19 May 2021 00:37:47 -0500 Subject: [PATCH] fix inputstream --- InputStream.tl | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/InputStream.tl b/InputStream.tl index 1a91a07..4d8db6a 100644 --- a/InputStream.tl +++ b/InputStream.tl @@ -3,7 +3,6 @@ local type InputStream = record offset: number refill: function(): string - new: function(InputStream): InputStream new: function(InputStream, function(): string): InputStream setRefill: function(InputStream, function(): string) @@ -19,28 +18,36 @@ function InputStream:_manageBuffer() self.str = self.refill() self.offset = 1 end - if self.offset > #self.str + 1 then + if self.offset > #self.str then self.str = self.refill() self.offset = 1 end end +local function initial_stream(): string + return "DUMMY" +end -- constructors function InputStream:new(): InputStream - return setmetatable( - { - string = "", - offset = 1 - } as InputStream, - istream_mt) -end -function InputStream:new(refill: function(): string): InputStream local mt = setmetatable( { string = "", - offset = 1 + offset = 1, + refill = initial_stream + } as InputStream, + istream_mt) + mt:setRefill(initial_stream) + return mt +end +function InputStream:new(refill: function(): string): InputStream + refill = refill or initial_stream + local mt = setmetatable( + { + string = "", + offset = 1, + refill = function(): string return "DUMMY" end } as InputStream, istream_mt) mt:setRefill(refill) @@ -53,6 +60,7 @@ end -- setters/getters function InputStream:setRefill(func: function(): string) self.refill = func + self:_manageBuffer() end -- @@ -62,9 +70,9 @@ function InputStream:readCurrentCharacter(): string end function InputStream:advanceOffset(): string + self:_manageBuffer() local current_char = self.str:sub(self.offset, self.offset) self.offset = self.offset + 1 - self:_manageBuffer() return current_char end