rename `NativeString` to `CString`
[nit.git] / lib / core / stream.nit
index b4a0e2b..217e9c8 100644 (file)
@@ -39,6 +39,26 @@ abstract class Stream
 
        # close the stream
        fun close is abstract
+
+       # Pre-work hook.
+       #
+       # Used to inform `self` that operations will start.
+       # Specific streams can use this to prepare some resources.
+       #
+       # Is automatically invoked at the beginning of `with` structures.
+       #
+       # Do nothing by default.
+       fun start do end
+
+       # Post-work hook.
+       #
+       # Used to inform `self` that the operations are over.
+       # Specific streams can use this to free some resources.
+       #
+       # Is automatically invoked at the end of `woth` structures.
+       #
+       # call `close` by default.
+       fun finish do close
 end
 
 # A `Stream` that can be read from
@@ -46,7 +66,7 @@ abstract class Reader
        super Stream
 
        # Decoder used to transform input bytes to UTF-8
-       var decoder: Decoder = utf8_decoder is writable
+       var decoder: Codec = utf8_codec is writable
 
        # Reads a character. Returns `null` on EOF or timeout
        fun read_char: nullable Char is abstract
@@ -61,7 +81,7 @@ abstract class Reader
        fun read_bytes(i: Int): Bytes
        do
                if last_error != null then return new Bytes.empty
-               var s = new NativeString(i)
+               var s = new CString(i)
                var buf = new Bytes(s, 0, 0)
                while i > 0 and not eof do
                        var c = read_byte
@@ -178,7 +198,7 @@ abstract class Reader
                var rets = ""
                var pos = 0
                var str = s.items.clean_utf8(slen)
-               slen = str.bytelen
+               slen = str.byte_length
                var sits = str.items
                var remsp = slen
                while pos < slen do
@@ -187,14 +207,14 @@ abstract class Reader
                        # if this is the best size or not
                        var chunksz = 129
                        if chunksz > remsp then
-                               rets += new FlatString.with_infos(sits, remsp, pos, pos + remsp - 1)
+                               rets += new FlatString.with_infos(sits, remsp, pos)
                                break
                        end
                        var st = sits.find_beginning_of_char_at(pos + chunksz - 1)
-                       var bytelen = st - pos
-                       rets += new FlatString.with_infos(sits, bytelen, pos, st - 1)
+                       var byte_length = st - pos
+                       rets += new FlatString.with_infos(sits, byte_length, pos)
                        pos = st
-                       remsp -= bytelen
+                       remsp -= byte_length
                end
                if rets isa Concat then return rets.balance
                return rets
@@ -386,7 +406,7 @@ abstract class Writer
        super Stream
 
        # The coder from a nit UTF-8 String to the output file
-       var coder: Coder = utf8_coder is writable
+       var coder: Codec = utf8_codec is writable
 
        # Writes bytes from `s`
        fun write_bytes(s: Bytes) is abstract
@@ -397,6 +417,9 @@ abstract class Writer
        # Write a single byte
        fun write_byte(value: Byte) is abstract
 
+       # Writes a single char
+       fun write_char(c: Char) do write(c.to_s)
+
        # Can the stream be used to write
        fun is_writable: Bool is abstract
 end
@@ -425,6 +448,13 @@ interface Writable
        end
 end
 
+redef class Bytes
+       super Writable
+       redef fun write_to(s) do s.write_bytes(self)
+
+       redef fun write_to_string do return to_s
+end
+
 redef class Text
        super Writable
        redef fun write_to(stream) do stream.write(self)
@@ -491,7 +521,7 @@ abstract class BufferedReader
                        while c < full_len do c = c * 2 + 2
                        _buffer_capacity = c
                end
-               var nns = new NativeString(_buffer_capacity)
+               var nns = new CString(_buffer_capacity)
                bf.items.copy_to(nns, bf.length, 0, 0)
                _buffer.copy_to(nns, remsp, _buffer_pos, bf.length)
                _buffer = nns
@@ -599,7 +629,7 @@ abstract class BufferedReader
        end
 
        # The buffer
-       private var buffer: NativeString = new NativeString(0)
+       private var buffer: CString = new CString(0)
 
        # The current position in the buffer
        private var buffer_pos = 0
@@ -619,7 +649,7 @@ abstract class BufferedReader
        # Allocate a `_buffer` for a given `capacity`.
        protected fun prepare_buffer(capacity: Int)
        do
-               _buffer = new NativeString(capacity)
+               _buffer = new CString(capacity)
                _buffer_pos = 0 # need to read
                _buffer_length = 0
                _buffer_capacity = capacity
@@ -697,10 +727,10 @@ class StringReader
 
        redef fun read_all_bytes do
                var nslen = source.length - cursor
-               var nns = new NativeString(nslen)
+               var nns = new CString(nslen)
                source.copy_to_native(nns, nslen, cursor, 0)
                return new Bytes(nns, nslen, nslen)
        end
 
-       redef fun eof do return cursor >= source.bytelen
+       redef fun eof do return cursor >= source.byte_length
 end