From 81c24860e2622a15e05c81d15e3d1cc02c460870 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 29 May 2020 16:08:14 +0300 Subject: [PATCH] Bugfix: totalUnconsumedBytes works properly within withReadableRange blocks --- faststreams/inputs.nim | 8 ++++++-- tests/test_inputs.nim | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/faststreams/inputs.nim b/faststreams/inputs.nim index a5b5ca9..10c8950 100644 --- a/faststreams/inputs.nim +++ b/faststreams/inputs.nim @@ -282,8 +282,12 @@ func totalUnconsumedBytes*(s: InputStream): Natural = ## buffers and that can be consumed with `read` or `advance`. let localRunway = s.span.len - runwayInBuffers = if s.buffers == nil: 0 - else: s.buffers.totalBufferedBytes + runwayInBuffers = if s.buffers == nil: + 0 + elif s.buffers.fauxEofPos != 0: + s.buffers.fauxEofPos - s.spanEndPos + else: + s.buffers.totalBufferedBytes if localRunway == 0 and runwayInBuffers > 0: getNewSpan s diff --git a/tests/test_inputs.nim b/tests/test_inputs.nim index 74f2b9e..3bfa3a7 100644 --- a/tests/test_inputs.nim +++ b/tests/test_inputs.nim @@ -48,6 +48,9 @@ procSuite "input stream": test "next returns none": check input.next.isNone + emptyInputTests "memoryInput": + var input = InputStream() + emptyInputTests "unsafeMemoryInput": var str = "" var input = unsafeMemoryInput(str) @@ -140,12 +143,16 @@ procSuite "input stream": let s = fileInput(asciiTableFile, pageSize = 100) if s.readable(20): s.withReadableRange(20, r): + check r.len.get == 20 + check r.totalUnconsumedBytes == 20 check r.readAll.len == 20 check s.readable if s.readable(200): s.withReadableRange(200, r): + check r.len.get == 200 + check r.totalUnconsumedBytes == 200 check r.readAll.len == 200 check s.readable