lib/core/stream: add start/finish for Stream
[nit.git] / lib / core / stream.nit
index 2db319a..ecfc77e 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
@@ -173,12 +193,13 @@ abstract class Reader
        # ~~~
        fun read_all: String do
                var s = read_all_bytes
-               if not s.is_utf8 then s = s.clean_utf8
                var slen = s.length
                if slen == 0 then return ""
                var rets = ""
                var pos = 0
-               var sits = s.items
+               var str = s.items.clean_utf8(slen)
+               slen = str.bytelen
+               var sits = str.items
                var remsp = slen
                while pos < slen do
                        # The 129 size was decided more or less arbitrarily
@@ -274,7 +295,7 @@ abstract class Reader
        # ~~~
        # var w = new StringReader(" Hello, \n\t World!")
        # assert w.read_word == "Hello,"
-       # assert w.read_char == '\n'.ascii
+       # assert w.read_char == '\n'
        # assert w.read_word == "World!"
        # assert w.read_word == ""
        # ~~~
@@ -440,7 +461,7 @@ abstract class BufferedReader
                        return null
                end
                # TODO: Fix when supporting UTF-8
-               var c = _buffer[_buffer_pos].to_i.ascii
+               var c = _buffer[_buffer_pos].to_i.code_point
                _buffer_pos += 1
                return c
        end