tests: add test_keep_going.nit
[nit.git] / lib / standard / stream.nit
index 46ad73c..7c4e606 100644 (file)
@@ -25,11 +25,11 @@ class IOError
        super Error
 end
 
-# Abstract stream class
-abstract class IOS
+# Any kind of stream to read/write/both to or from a source
+abstract class Stream
        # Error produced by the file stream
        #
-       #     var ifs = new IFStream.open("donotmakethisfile.binx")
+       #     var ifs = new FileReader.open("donotmakethisfile.binx")
        #     ifs.read_all
        #     ifs.close
        #     assert ifs.last_error != null
@@ -39,11 +39,14 @@ abstract class IOS
        fun close is abstract
 end
 
-# Abstract input streams
-abstract class IStream
-       super IOS
-       # Read a character. Return its ASCII value, -1 on EOF or timeout
-       fun read_char: Int is abstract
+# A `Stream` that can be read from
+abstract class Reader
+       super Stream
+       # Reads a character. Returns `null` on EOF or timeout
+       fun read_char: nullable Char is abstract
+
+       # Reads a byte. Returns `null` on EOF or timeout
+       fun read_byte: nullable Int is abstract
 
        # Read at most i bytes
        fun read(i: Int): String
@@ -52,8 +55,8 @@ abstract class IStream
                var s = new FlatBuffer.with_capacity(i)
                while i > 0 and not eof do
                        var c = read_char
-                       if c >= 0 then
-                               s.add(c.ascii)
+                       if c != null then
+                               s.add(c)
                                i -= 1
                        end
                end
@@ -66,7 +69,7 @@ abstract class IStream
        #
        # ~~~
        # var txt = "Hello\n\nWorld\n"
-       # var i = new StringIStream(txt)
+       # var i = new StringReader(txt)
        # assert i.read_line == "Hello"
        # assert i.read_line == ""
        # assert i.read_line == "World"
@@ -79,7 +82,7 @@ abstract class IStream
        #
        # ~~~
        # var txt2 = "Hello\r\n\n\rWorld"
-       # var i2 = new StringIStream(txt2)
+       # var i2 = new StringReader(txt2)
        # assert i2.read_line == "Hello"
        # assert i2.read_line == ""
        # assert i2.read_line == "\rWorld"
@@ -102,7 +105,7 @@ abstract class IStream
        #
        # ~~~
        # var txt = "Hello\n\nWorld\n"
-       # var i = new StringIStream(txt)
+       # var i = new StringReader(txt)
        # assert i.read_lines == ["Hello", "", "World"]
        # ~~~
        #
@@ -126,7 +129,7 @@ abstract class IStream
        #
        # ~~~
        # var txt = "Hello\n\nWorld\n"
-       # var i = new StringIStream(txt)
+       # var i = new StringReader(txt)
        # assert i.each_line.to_a == ["Hello", "", "World"]
        # ~~~
        #
@@ -134,7 +137,7 @@ abstract class IStream
        # Therefore, the stream should no be closed until the end of the stream.
        #
        # ~~~
-       # i = new StringIStream(txt)
+       # i = new StringReader(txt)
        # var el = i.each_line
        #
        # assert el.item == "Hello"
@@ -155,7 +158,7 @@ abstract class IStream
        #
        # ~~~
        # var txt = "Hello\n\nWorld\n"
-       # var i = new StringIStream(txt)
+       # var i = new StringReader(txt)
        # assert i.read_all == txt
        # ~~~
        fun read_all: String
@@ -164,7 +167,7 @@ abstract class IStream
                var s = new FlatBuffer
                while not eof do
                        var c = read_char
-                       if c >= 0 then s.add(c.ascii)
+                       if c != null then s.add(c)
                end
                return s.to_s
        end
@@ -177,7 +180,7 @@ abstract class IStream
        #
        # ~~~
        # var txt = "Hello\n\nWorld\n"
-       # var i = new StringIStream(txt)
+       # var i = new StringReader(txt)
        # var b = new FlatBuffer
        # i.append_line_to(b)
        # assert b == "Hello\n"
@@ -192,7 +195,7 @@ abstract class IStream
        # a non-eol terminated last line was returned.
        #
        # ~~~
-       # var i2 = new StringIStream("hello")
+       # var i2 = new StringReader("hello")
        # assert not i2.eof
        # var b2 = new FlatBuffer
        # i2.append_line_to(b2)
@@ -207,12 +210,11 @@ abstract class IStream
                if last_error != null then return
                loop
                        var x = read_char
-                       if x == -1 then
+                       if x == null then
                                if eof then return
                        else
-                               var c = x.ascii
-                               s.chars.push(c)
-                               if c == '\n' then return
+                               s.chars.push(x)
+                               if x == '\n' then return
                        end
                end
        end
@@ -220,15 +222,74 @@ abstract class IStream
        # Is there something to read.
        # This function returns 'false' if there is something to read.
        fun eof: Bool is abstract
+
+       # Read the next sequence of non whitespace characters.
+       #
+       # Leading whitespace characters are skipped.
+       # The first whitespace character that follows the result is consumed.
+       #
+       # An empty string is returned if the end of the file or an error is encounter.
+       #
+       # ~~~
+       # var w = new StringReader(" Hello, \n\t World!")
+       # assert w.read_word == "Hello,"
+       # assert w.read_char == '\n'.ascii
+       # assert w.read_word == "World!"
+       # assert w.read_word == ""
+       # ~~~
+       #
+       # `Char::is_whitespace` determines what is a whitespace.
+       fun read_word: String
+       do
+               var buf = new FlatBuffer
+               var c = read_nonwhitespace
+               if c != null then
+                       buf.add(c)
+                       while not eof do
+                               c = read_char
+                               if c == null then break
+                               if c.is_whitespace then break
+                               buf.add(c)
+                       end
+               end
+               var res = buf.to_s
+               return res
+       end
+
+       # Skip whitespace characters (if any) then return the following non-whitespace character.
+       #
+       # Returns the code point of the character.
+       # Returns `null` on end of file or error.
+       #
+       # In fact, this method works like `read_char` except it skips whitespace.
+       #
+       # ~~~
+       # var w = new StringReader(" \nab\tc")
+       # assert w.read_nonwhitespace == 'a'
+       # assert w.read_nonwhitespace == 'b'
+       # assert w.read_nonwhitespace == 'c'
+       # assert w.read_nonwhitespace == null
+       # ~~~
+       #
+       # `Char::is_whitespace` determines what is a whitespace.
+       fun read_nonwhitespace: nullable Char
+       do
+               var c: nullable Char = null
+               while not eof do
+                       c = read_char
+                       if c == null or not c.is_whitespace then break
+               end
+               return c
+       end
 end
 
-# Iterator returned by `IStream::each_line`.
+# Iterator returned by `Reader::each_line`.
 # See the aforementioned method for details.
 class LineIterator
        super Iterator[String]
 
        # The original stream
-       var stream: IStream
+       var stream: Reader
 
        redef fun is_ok
        do
@@ -269,35 +330,38 @@ class LineIterator
        end
 end
 
-# IStream capable of declaring if readable without blocking
-abstract class PollableIStream
-       super IStream
+# `Reader` capable of declaring if readable without blocking
+abstract class PollableReader
+       super Reader
 
        # Is there something to read? (without blocking)
        fun poll_in: Bool is abstract
 
 end
 
-# Abstract output stream
-abstract class OStream
-       super IOS
+# A `Stream` that can be written to
+abstract class Writer
+       super Stream
        # write a string
        fun write(s: Text) is abstract
 
+       # Write a single byte
+       fun write_byte(value: Int) is abstract
+
        # Can the stream be used to write
        fun is_writable: Bool is abstract
 end
 
-# Things that can be efficienlty writen to a OStream
+# Things that can be efficienlty written to a `Writer`
 #
-# The point of this interface it to allow is instance to be efficenty
-# writen into a OStream without having to allocate a big String object
+# The point of this interface is to allow the instance to be efficiently
+# written into a `Writer`.
 #
-# ready-to-save documents usually provide this interface.
-interface Streamable
+# Ready-to-save documents usually provide this interface.
+interface Writable
        # Write itself to a `stream`
        # The specific logic it let to the concrete subclasses
-       fun write_to(stream: OStream) is abstract
+       fun write_to(stream: Writer) is abstract
 
        # Like `write_to` but return a new String (may be quite large)
        #
@@ -306,48 +370,87 @@ interface Streamable
        # stream without having to allocate and concatenate strings
        fun write_to_string: String
        do
-               var stream = new StringOStream
+               var stream = new StringWriter
                write_to(stream)
                return stream.to_s
        end
 end
 
 redef class Text
-       super Streamable
+       super Writable
        redef fun write_to(stream) do stream.write(self)
 end
 
-# Input streams with a buffer
-abstract class BufferedIStream
-       super IStream
+# Input streams with a buffered input for efficiency purposes
+abstract class BufferedReader
+       super Reader
        redef fun read_char
        do
-               if last_error != null then return -1
+               if last_error != null then return null
+               if eof then
+                       last_error = new IOError("Stream has reached eof")
+                       return null
+               end
+               var c = _buffer[_buffer_pos]
+               _buffer_pos += 1
+               return c
+       end
+
+       redef fun read_byte
+       do
+               if last_error != null then return null
                if eof then
                        last_error = new IOError("Stream has reached eof")
-                       return -1
+                       return null
                end
-               var c = _buffer.chars[_buffer_pos]
+               var c = _buffer[_buffer_pos].ascii
                _buffer_pos += 1
-               return c.ascii
+               return c
+       end
+
+       # Peeks up to `n` bytes in the buffer, returns an empty string on EOF
+       #
+       # The operation does not consume the buffer
+       #
+       # ~~~nitish
+       #       var x = new FileReader("File.txt")
+       #       assert x.peek(5) == x.read(5)
+       # ~~~
+       fun peek(i: Int): String do
+               if eof then return ""
+               var b = new FlatBuffer.with_capacity(i)
+               while i > 0 and not eof do
+                       b.add _buffer[_buffer_pos]
+                       _buffer_pos += 1
+                       i -= 1
+               end
+               var nbuflen = b.length + (_buffer.length - _buffer_pos)
+               var nbuf = new FlatBuffer.with_capacity(nbuflen)
+               nbuf.append(b)
+               while _buffer_pos < _buffer.length do
+                       nbuf.add(_buffer[_buffer_pos])
+                       _buffer_pos += 1
+               end
+               _buffer_pos = 0
+               _buffer = nbuf
+               return b.to_s
        end
 
        redef fun read(i)
        do
                if last_error != null then return ""
-               if _buffer.length == _buffer_pos then
-                       if not eof then
-                               return read(i)
-                       end
-                       return ""
-               end
-               if _buffer_pos + i >= _buffer.length then
-                       var from = _buffer_pos
-                       _buffer_pos = _buffer.length
-                       return _buffer.substring_from(from).to_s
+               if eof then return ""
+               var p = _buffer_pos
+               var bufsp = _buffer.length - p
+               if bufsp >= i then
+                       _buffer_pos += i
+                       return _buffer.substring(p, i).to_s
                end
-               _buffer_pos += i
-               return _buffer.substring(_buffer_pos - i, i).to_s
+               _buffer_pos = _buffer.length
+               var readln = _buffer.length - p
+               var s = _buffer.substring(p, readln).to_s
+               fill_buffer
+               return s + read(i - readln)
        end
 
        redef fun read_all
@@ -372,11 +475,11 @@ abstract class BufferedIStream
                loop
                        # First phase: look for a '\n'
                        var i = _buffer_pos
-                       while i < _buffer.length and _buffer.chars[i] != '\n' do i += 1
+                       while i < _buffer.length and _buffer[i] != '\n' do i += 1
 
                        var eol
                        if i < _buffer.length then
-                               assert _buffer.chars[i] == '\n'
+                               assert _buffer[i] == '\n'
                                i += 1
                                eol = true
                        else
@@ -391,7 +494,7 @@ abstract class BufferedIStream
                                # Copy from the buffer to the string
                                var j = _buffer_pos
                                while j < i do
-                                       s.add(_buffer.chars[j])
+                                       s.add(_buffer[j])
                                        j += 1
                                end
                                _buffer_pos = i
@@ -439,17 +542,17 @@ abstract class BufferedIStream
        end
 end
 
-# An Input/Output Stream
-abstract class IOStream
-       super IStream
-       super OStream
+# A `Stream` that can be written to and read from
+abstract class Duplex
+       super Reader
+       super Writer
 end
 
-# Stream to a String.
+# `Stream` that can be used to write to a `String`
 #
-# Mainly used for compatibility with OStream type and tests.
-class StringOStream
-       super OStream
+# Mainly used for compatibility with Writer type and tests.
+class StringWriter
+       super Writer
 
        private var content = new Array[String]
        redef fun to_s do return content.to_s
@@ -466,11 +569,11 @@ class StringOStream
        redef fun close do closed = true
 end
 
-# Stream from a String.
+# `Stream` used to read from a `String`
 #
-# Mainly used for compatibility with IStream type and tests.
-class StringIStream
-       super IStream
+# Mainly used for compatibility with Reader type and tests.
+class StringReader
+       super Reader
 
        # The string to read from.
        var source: String
@@ -480,12 +583,23 @@ class StringIStream
 
        redef fun read_char do
                if cursor < source.length then
-                       var c = source[cursor].ascii
+                       var c = source[cursor]
 
                        cursor += 1
                        return c
                else
-                       return -1
+                       return null
+               end
+       end
+
+       redef fun read_byte do
+               if cursor < source.length then
+                       var c = source[cursor]
+
+                       cursor += 1
+                       return c.ascii
+               else
+                       return null
                end
        end
 
@@ -493,5 +607,12 @@ class StringIStream
                source = ""
        end
 
+       redef fun read_all do
+               var c = cursor
+               cursor = source.length
+               if c == 0 then return source
+               return source.substring_from(c)
+       end
+
        redef fun eof do return cursor >= source.length
 end