From 1f4d6f3af62cf596e0d3bede22e2bbaea4f4bff3 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 5 Apr 2018 22:15:28 -0400 Subject: [PATCH 1/1] stream: move decoder to stream and rename as codec Since every stream will need to pass through a codec to code/decode to and from textual data, the former 'decoder' attribute is now moved to Stream and renamed to better reflect its use. Signed-off-by: Lucas Bajolet --- lib/core/stream.nit | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/core/stream.nit b/lib/core/stream.nit index 61b59dd..d077a73 100644 --- a/lib/core/stream.nit +++ b/lib/core/stream.nit @@ -29,6 +29,51 @@ end # Any kind of stream to read/write/both to or from a source abstract class Stream + # Codec used to transform raw data to text + # + # Note: defaults to UTF-8 + var codec: Codec = utf8_codec is protected writable(set_codec) + + # Lookahead buffer for codecs + # + # Since some codecs are multibyte, a lookahead may be required + # to store the next bytes and consume them only if a valid character + # is read. + protected var lookahead: CString is noinit + + # Capacity of the lookahead + protected var lookahead_capacity = 0 + + # Current occupation of the lookahead + protected var lookahead_length = 0 + + # Buffer for writing data to a stream + protected var write_buffer: CString is noinit + + init do + var lcap = codec.max_lookahead + lookahead = new CString(lcap) + write_buffer = new CString(lcap) + lookahead_length = 0 + lookahead_capacity = lcap + end + + # Change the codec for this stream. + fun codec=(c: Codec) do + if c.max_lookahead > lookahead_capacity then + var lcap = codec.max_lookahead + var lk = new CString(lcap) + var llen = lookahead_length + if llen > 0 then + lookahead.copy_to(lk, llen, 0, 0) + end + lookahead = lk + lookahead_capacity = lcap + write_buffer = new CString(lcap) + end + set_codec(c) + end + # Error produced by the file stream # # var ifs = new FileReader.open("donotmakethisfile.binx") @@ -65,9 +110,6 @@ end abstract class Reader super Stream - # Decoder used to transform input bytes to UTF-8 - var decoder: Codec = utf8_codec is writable - # Reads a character. Returns `null` on EOF or timeout fun read_char: nullable Char is abstract @@ -409,9 +451,6 @@ end abstract class Writer super Stream - # The coder from a nit UTF-8 String to the output file - var coder: Codec = utf8_codec is writable - # Writes bytes from `s` fun write_bytes(s: Bytes) is abstract -- 1.7.9.5