about summary refs log tree commit diff stats
path: root/src/encoding
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-26 13:54:29 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-26 13:54:29 +0100
commitf286f4b16537907c93324a89937fa2e9ad001cab (patch)
treef1273352a034ac9fbcd75c7b09d86ee009f5da07 /src/encoding
parentca88af27d30722b6ecb6cc7dd47e572526218eae (diff)
downloadchawan-f286f4b16537907c93324a89937fa2e9ad001cab.tar.gz
decoderstream: fix some bugs that broke readAll
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/decoderstream.nim12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/encoding/decoderstream.nim b/src/encoding/decoderstream.nim
index e3161921..362d9607 100644
--- a/src/encoding/decoderstream.nim
+++ b/src/encoding/decoderstream.nim
@@ -766,9 +766,11 @@ proc readData*(stream: DecoderStream, buffer: pointer, olen: int): int =
   let oq = cast[ptr UncheckedArray[uint32]](buffer)
   result = stream.copyBuffers(oq, olen)
   let olen = olen - result
-  if olen == 0:
+  if olen == 0 or stream.source.atEnd:
+    # either output filled with buffered data; nothing to decode
+    # or we're at the end of the source stream
     stream.checkEnd(oq, olen, result)
-    return result # output filled with buffered data; nothing to decode.
+    return result
   var iq = newSeqUninitialized[uint8](ReadSize)
   let ilen = stream.source.readData(cast[pointer](addr iq[0]), ReadSize)
   case stream.charset
@@ -814,6 +816,10 @@ proc readData*(stream: DecoderStream, buffer: pointer, olen: int): int =
   of CHARSET_UNKNOWN: assert false, "Somebody forgot to set the character set here"
   stream.checkEnd(oq, olen, result)
 
+# Returns the number of bytes read.
+proc readData*(stream: DecoderStream, buf: var seq[uint32]): int =
+  return stream.readData(addr buf[0], buf.len * sizeof(buf[0]))
+
 proc readRunes*(stream: DecoderStream, olen: int): seq[Rune] =
   when nimvm:
     let s = stream.source.readStr(olen)
@@ -831,7 +837,7 @@ proc atEnd*(stream: DecoderStream): bool =
 proc readAll*(stream: DecoderStream): string =
   var buf = newSeqUninitialized[uint32](stream.buflen)
   while not stream.atEnd:
-    let n = stream.readData(addr buf, buf.len * sizeof(buf[0]))
+    let n = stream.readData(buf)
     for i in 0 ..< n div 4:
       let r = cast[Rune](buf[i])
       result &= $r