Bugfix: totalUnconsumedBytes works properly within withReadableRange blocks

This commit is contained in:
Zahary Karadjov 2020-05-29 16:08:14 +03:00
commit 81c24860e2
No known key found for this signature in database
GPG key ID: C8936F8A3073D609
2 changed files with 13 additions and 2 deletions

View file

@ -282,8 +282,12 @@ func totalUnconsumedBytes*(s: InputStream): Natural =
## buffers and that can be consumed with `read` or `advance`. ## buffers and that can be consumed with `read` or `advance`.
let let
localRunway = s.span.len localRunway = s.span.len
runwayInBuffers = if s.buffers == nil: 0 runwayInBuffers = if s.buffers == nil:
else: s.buffers.totalBufferedBytes 0
elif s.buffers.fauxEofPos != 0:
s.buffers.fauxEofPos - s.spanEndPos
else:
s.buffers.totalBufferedBytes
if localRunway == 0 and runwayInBuffers > 0: if localRunway == 0 and runwayInBuffers > 0:
getNewSpan s getNewSpan s

View file

@ -48,6 +48,9 @@ procSuite "input stream":
test "next returns none": test "next returns none":
check input.next.isNone check input.next.isNone
emptyInputTests "memoryInput":
var input = InputStream()
emptyInputTests "unsafeMemoryInput": emptyInputTests "unsafeMemoryInput":
var str = "" var str = ""
var input = unsafeMemoryInput(str) var input = unsafeMemoryInput(str)
@ -140,12 +143,16 @@ procSuite "input stream":
let s = fileInput(asciiTableFile, pageSize = 100) let s = fileInput(asciiTableFile, pageSize = 100)
if s.readable(20): if s.readable(20):
s.withReadableRange(20, r): s.withReadableRange(20, r):
check r.len.get == 20
check r.totalUnconsumedBytes == 20
check r.readAll.len == 20 check r.readAll.len == 20
check s.readable check s.readable
if s.readable(200): if s.readable(200):
s.withReadableRange(200, r): s.withReadableRange(200, r):
check r.len.get == 200
check r.totalUnconsumedBytes == 200
check r.readAll.len == 200 check r.readAll.len == 200
check s.readable check s.readable