inputstream changes; curr never reads from after the string
This commit is contained in:
parent
f432c05216
commit
59b7c5a028
|
@ -11,7 +11,7 @@ function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(En
|
||||||
end
|
end
|
||||||
|
|
||||||
function CoreHelpers.standardInputRefill(): string
|
function CoreHelpers.standardInputRefill(): string
|
||||||
local input = io.read().."\n"
|
local input = io.read().."\n\n"
|
||||||
return input
|
return input
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ end
|
||||||
function CoreHelpers._fileRefill(fname: string): function(): string
|
function CoreHelpers._fileRefill(fname: string): function(): string
|
||||||
local f = assert(io.open(fname, "r"))
|
local f = assert(io.open(fname, "r"))
|
||||||
return function(): string
|
return function(): string
|
||||||
local chunk = f:read(2^13)
|
local chunk = f:read(2^3)
|
||||||
if not chunk then chunk = "" end
|
if not chunk then chunk = "" end
|
||||||
return chunk
|
return chunk
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,13 @@ end
|
||||||
local istream_mt = {__index = InputStream}
|
local istream_mt = {__index = InputStream}
|
||||||
|
|
||||||
function InputStream:_manageBuffer()
|
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
|
if not self.str then
|
||||||
self.str = self.refill()
|
self.str = self.refill()
|
||||||
self.offset = 1
|
self.offset = 1
|
||||||
|
@ -48,20 +55,27 @@ end
|
||||||
-- setters/getters
|
-- setters/getters
|
||||||
function InputStream:__setRefill(func: function(): string)
|
function InputStream:__setRefill(func: function(): string)
|
||||||
self.refill = func
|
self.refill = func
|
||||||
|
print("setrefill")
|
||||||
self:_manageBuffer()
|
self:_manageBuffer()
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
--
|
||||||
function InputStream:curr(): string | nil
|
function InputStream:curr(): string | nil
|
||||||
self:_manageBuffer()
|
if self.offset > #self.str then
|
||||||
if #self.str > 0 then
|
self:_manageBuffer()
|
||||||
|
end
|
||||||
|
if #self.str > 0 and self.offset < #self.str + 1 then
|
||||||
return self.str:sub(self.offset, self.offset)
|
return self.str:sub(self.offset, self.offset)
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function InputStream:next(): string | nil
|
function InputStream:next(): string | nil
|
||||||
self:_manageBuffer()
|
print("next")
|
||||||
|
if self.offset > #self.str then
|
||||||
|
self:_manageBuffer()
|
||||||
|
end
|
||||||
|
|
||||||
local current_char: string | nil = nil
|
local current_char: string | nil = nil
|
||||||
if #self.str > 0 then
|
if #self.str > 0 then
|
||||||
current_char = self.str:sub(self.offset, self.offset)
|
current_char = self.str:sub(self.offset, self.offset)
|
||||||
|
|
|
@ -78,6 +78,7 @@ describe("InputStream tests", function()
|
||||||
it("handles multiple lines", function()
|
it("handles multiple lines", function()
|
||||||
local inputstr = "TEST\nTEST\nTEST\nTEST\nTEST\nTEST51515151"
|
local inputstr = "TEST\nTEST\nTEST\nTEST\nTEST\nTEST51515151"
|
||||||
input = CoreHelpers.oneReadInputStream(inputstr)
|
input = CoreHelpers.oneReadInputStream(inputstr)
|
||||||
|
input:curr()
|
||||||
for i = 1, #inputstr do
|
for i = 1, #inputstr do
|
||||||
assert.are.same(inputstr:sub(i, i), input:next())
|
assert.are.same(inputstr:sub(i, i), input:next())
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue