lib/core/stream: LineIterator use CachedIterator
[nit.git] / lib / core / stream.nit
index 6d0278a..b0486ef 100644 (file)
@@ -122,7 +122,7 @@ abstract class Reader
                for i in [0 .. max[ do
                        var b = raw_read_byte
                        if b < 0 then break
-                       buf[i] = b.to_b
+                       buf[i] = b
                        rd += 1
                end
                return rd
@@ -484,37 +484,18 @@ end
 # Iterator returned by `Reader::each_line`.
 # See the aforementioned method for details.
 class LineIterator
-       super Iterator[String]
+       super CachedIterator[String]
 
        # The original stream
        var stream: Reader
 
-       redef fun is_ok
+       redef fun next_item
        do
-               var res = not stream.eof
-               if not res and close_on_finish then stream.close
-               return res
-       end
-
-       redef fun item
-       do
-               var line = self.line
-               if line == null then
-                       line = stream.read_line
+               if stream.eof then
+                       if close_on_finish then stream.close
+                       return null
                end
-               self.line = line
-               return line
-       end
-
-       # The last line read (cache)
-       private var line: nullable String = null
-
-       redef fun next
-       do
-               # force the read
-               if line == null then item
-               # drop the line
-               line = null
+               return stream.read_line
        end
 
        # Close the stream when the stream is at the EOF.
@@ -551,7 +532,7 @@ abstract class Writer
        fun write(s: Text) is abstract
 
        # Write a single byte
-       fun write_byte(value: Byte) is abstract
+       fun write_byte(value: Int) is abstract
 
        # Write a single char
        fun write_char(c: Char) do
@@ -627,7 +608,7 @@ end
 #
 # writer.write "Strings "
 # writer.write_char '&'
-# writer.write_byte 0x20u8
+# writer.write_byte 0x20
 # writer.write_bytes "bytes".to_bytes
 #
 # assert writer.to_s == "\\x53\\x74\\x72\\x69\\x6E\\x67\\x73\\x20\\x26\\x20\\x62\\x79\\x74\\x65\\x73"
@@ -641,12 +622,12 @@ end
 # writer = new BytesWriter
 #
 # # Write just the character first half
-# writer.write_byte 0xC2u8
+# writer.write_byte 0xC2
 # assert writer.to_s == "\\xC2"
 # assert writer.bytes.to_s == "�"
 #
 # # Complete the character
-# writer.write_byte 0xA2u8
+# writer.write_byte 0xA2
 # assert writer.to_s == "\\xC2\\xA2"
 # assert writer.bytes.to_s == "¢"
 # ~~~
@@ -698,7 +679,7 @@ end
 #
 # writer.write "Strings "
 # writer.write_char '&'
-# writer.write_byte 0x20u8
+# writer.write_byte 0x20
 # writer.write_bytes "bytes".to_bytes
 #
 # assert writer.to_s == "Strings & bytes"