Merge: Nitgs optims
[nit.git] / lib / standard / stream.nit
index 5dbe9d8..d65d377 100644 (file)
@@ -37,7 +37,7 @@ interface IStream
        # Read at most i bytes
        fun read(i: Int): String
        do
-               var s = new Buffer.with_capacity(i)
+               var s = new FlatBuffer.with_capacity(i)
                while i > 0 and not eof do
                        var c = read_char
                        if c >= 0 then
@@ -52,7 +52,7 @@ interface IStream
        fun read_line: String
        do
                assert not eof
-               var s = new Buffer
+               var s = new FlatBuffer
                append_line_to(s)
                return s.to_s
        end
@@ -60,7 +60,7 @@ interface IStream
        # Read all the stream until the eof.
        fun read_all: String
        do
-               var s = new Buffer
+               var s = new FlatBuffer
                while not eof do
                        var c = read_char
                        if c >= 0 then s.add(c.ascii)
@@ -77,7 +77,7 @@ interface IStream
                                if eof then return
                        else
                                var c = x.ascii
-                               s.push(c)
+                               s.chars.push(c)
                                if c == '\n' then return
                        end
                end
@@ -98,6 +98,35 @@ interface OStream
        fun is_writable: Bool is abstract
 end
 
+# Things that can be efficienlty writen to a OStream
+#
+# 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
+#
+# ready-to-save documents usually provide this interface.
+interface Streamable
+       # Write itself to a `stream`
+       # The specific logic it let to the concrete subclasses
+       fun write_to(stream: OStream) is abstract
+
+       # Like `write_to` but return a new String (may be quite large)
+       #
+       # This funtionnality is anectodical, since the point
+       # of streamable object to to be efficienlty written to a
+       # stream without having to allocate and concatenate strings
+       fun write_to_string: String
+       do
+               var stream = new StringOStream
+               write_to(stream)
+               return stream.to_s
+       end
+end
+
+redef class String
+       super Streamable
+       redef fun write_to(stream) do stream.write(self)
+end
+
 # Input streams with a buffer
 abstract class BufferedIStream
        super IStream
@@ -110,14 +139,14 @@ abstract class BufferedIStream
                if _buffer_pos >= _buffer.length then
                        return -1
                end
-               var c = _buffer[_buffer_pos]
+               var c = _buffer.chars[_buffer_pos]
                _buffer_pos += 1
                return c.ascii
        end
 
        redef fun read(i)
        do
-               var s = new Buffer.with_capacity(i)
+               var s = new FlatBuffer.with_capacity(i)
                var j = _buffer_pos
                var k = _buffer.length
                while i > 0 do
@@ -128,7 +157,7 @@ abstract class BufferedIStream
                                k = _buffer.length
                        end
                        while j < k and i > 0 do
-                               s.add(_buffer[j])
+                               s.add(_buffer.chars[j])
                                j +=  1
                                i -= 1
                        end
@@ -139,12 +168,12 @@ abstract class BufferedIStream
 
        redef fun read_all
        do
-               var s = new Buffer
+               var s = new FlatBuffer
                while not eof do
                        var j = _buffer_pos
                        var k = _buffer.length
                        while j < k do
-                               s.add(_buffer[j])
+                               s.add(_buffer.chars[j])
                                j += 1
                        end
                        _buffer_pos = j
@@ -158,7 +187,7 @@ abstract class BufferedIStream
                loop
                        # First phase: look for a '\n'
                        var i = _buffer_pos
-                       while i < _buffer.length and _buffer[i] != '\n' do i += 1
+                       while i < _buffer.length and _buffer.chars[i] != '\n' do i += 1
 
                        # if there is something to append
                        if i > _buffer_pos then
@@ -168,7 +197,7 @@ abstract class BufferedIStream
                                # Copy from the buffer to the string
                                var j = _buffer_pos
                                while j < i do
-                                       s.add(_buffer[j])
+                                       s.add(_buffer.chars[j])
                                        j += 1
                                end
                        end
@@ -192,7 +221,7 @@ abstract class BufferedIStream
        redef fun eof do return _buffer_pos >= _buffer.length and end_reached
 
        # The buffer
-       var _buffer: nullable Buffer = null
+       var _buffer: nullable FlatBuffer = null
 
        # The current position in the buffer
        var _buffer_pos: Int = 0
@@ -206,7 +235,7 @@ abstract class BufferedIStream
        # Allocate a `_buffer` for a given `capacity`.
        protected fun prepare_buffer(capacity: Int)
        do
-               _buffer = new Buffer.with_capacity(capacity)
+               _buffer = new FlatBuffer.with_capacity(capacity)
                _buffer_pos = 0 # need to read
        end
 end