inputstream changes; curr never reads from after the string

This commit is contained in:
Starfflame 2021-05-20 02:00:48 -05:00
parent f432c05216
commit 59b7c5a028
3 changed files with 20 additions and 5 deletions

View File

@ -11,7 +11,7 @@ function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(En
end
function CoreHelpers.standardInputRefill(): string
local input = io.read().."\n"
local input = io.read().."\n\n"
return input
end
@ -30,7 +30,7 @@ end
function CoreHelpers._fileRefill(fname: string): function(): string
local f = assert(io.open(fname, "r"))
return function(): string
local chunk = f:read(2^13)
local chunk = f:read(2^3)
if not chunk then chunk = "" end
return chunk
end

View File

@ -14,6 +14,13 @@ end
local istream_mt = {__index = InputStream}
function InputStream:_manageBuffer()
local length = 0
if self.str then
length = #self.str
else
length = -1
end
print("self.str = ",self.str, self.offset,length)
if not self.str then
self.str = self.refill()
self.offset = 1
@ -48,20 +55,27 @@ end
-- setters/getters
function InputStream:__setRefill(func: function(): string)
self.refill = func
print("setrefill")
self:_manageBuffer()
end
--
function InputStream:curr(): string | nil
self:_manageBuffer()
if #self.str > 0 then
if self.offset > #self.str then
self:_manageBuffer()
end
if #self.str > 0 and self.offset < #self.str + 1 then
return self.str:sub(self.offset, self.offset)
end
return nil
end
function InputStream:next(): string | nil
self:_manageBuffer()
print("next")
if self.offset > #self.str then
self:_manageBuffer()
end
local current_char: string | nil = nil
if #self.str > 0 then
current_char = self.str:sub(self.offset, self.offset)

View File

@ -78,6 +78,7 @@ describe("InputStream tests", function()
it("handles multiple lines", function()
local inputstr = "TEST\nTEST\nTEST\nTEST\nTEST\nTEST51515151"
input = CoreHelpers.oneReadInputStream(inputstr)
input:curr()
for i = 1, #inputstr do
assert.are.same(inputstr:sub(i, i), input:next())
end