1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Input and output streams of characters
14 intrude import text
::ropes
25 # Any kind of error that could be produced by an operation on Streams
30 # Any kind of stream to read/write/both to or from a source
32 # Error produced by the file stream
34 # var ifs = new FileReader.open("donotmakethisfile.binx")
37 # assert ifs.last_error != null
38 var last_error
: nullable IOError = null
44 # A `Stream` that can be read from
48 # Decoder used to transform input bytes to UTF-8
49 var decoder
: Decoder = utf8_decoder
is writable
51 # Reads a character. Returns `null` on EOF or timeout
52 fun read_char
: nullable Char is abstract
54 # Reads a byte. Returns `null` on EOF or timeout
55 fun read_byte
: nullable Byte is abstract
57 # Reads a String of at most `i` length
58 fun read
(i
: Int): String do return read_bytes
(i
).to_s
60 # Read at most i bytes
61 fun read_bytes
(i
: Int): Bytes
63 if last_error
!= null then return new Bytes.empty
64 var s
= new NativeString(i
)
65 var buf
= new Bytes(s
, 0, 0)
66 while i
> 0 and not eof
do
76 # Read a string until the end of the line.
78 # The line terminator '\n' and '\r\n', if any, is removed in each line.
81 # var txt = "Hello\n\nWorld\n"
82 # var i = new StringReader(txt)
83 # assert i.read_line == "Hello"
84 # assert i.read_line == ""
85 # assert i.read_line == "World"
89 # Only LINE FEED (`\n`), CARRIAGE RETURN & LINE FEED (`\r\n`), and
90 # the end or file (EOF) is considered to delimit the end of lines.
91 # CARRIAGE RETURN (`\r`) alone is not used for the end of line.
94 # var txt2 = "Hello\r\n\n\rWorld"
95 # var i2 = new StringReader(txt2)
96 # assert i2.read_line == "Hello"
97 # assert i2.read_line == ""
98 # assert i2.read_line == "\rWorld"
102 # NOTE: Use `append_line_to` if the line terminator needs to be preserved.
103 fun read_line
: String
105 if last_error
!= null then return ""
106 if eof
then return ""
107 var s
= new FlatBuffer
112 # Read all the lines until the eof.
114 # The line terminator '\n' and `\r\n` is removed in each line,
117 # var txt = "Hello\n\nWorld\n"
118 # var i = new StringReader(txt)
119 # assert i.read_lines == ["Hello", "", "World"]
122 # This method is more efficient that splitting
123 # the result of `read_all`.
125 # NOTE: SEE `read_line` for details.
126 fun read_lines
: Array[String]
128 var res
= new Array[String]
135 # Return an iterator that read each line.
137 # The line terminator '\n' and `\r\n` is removed in each line,
138 # The line are read with `read_line`. See this method for details.
141 # var txt = "Hello\n\nWorld\n"
142 # var i = new StringReader(txt)
143 # assert i.each_line.to_a == ["Hello", "", "World"]
146 # Unlike `read_lines` that read all lines at the call, `each_line` is lazy.
147 # Therefore, the stream should no be closed until the end of the stream.
150 # i = new StringReader(txt)
151 # var el = i.each_line
153 # assert el.item == "Hello"
155 # assert el.item == ""
160 # assert not el.is_ok
161 # # closed before "world" is read
163 fun each_line
: LineIterator do return new LineIterator(self)
165 # Read all the stream until the eof.
167 # The content of the file is returned as a String.
170 # var txt = "Hello\n\nWorld\n"
171 # var i = new StringReader(txt)
172 # assert i.read_all == txt
174 fun read_all
: String do
175 var s
= read_all_bytes
176 if not s
.is_utf8
then s
= s
.clean_utf8
178 if slen
== 0 then return ""
184 # The 129 size was decided more or less arbitrarily
185 # It will require some more benchmarking to compute
186 # if this is the best size or not
188 if chunksz
> remsp
then
189 rets
+= new FlatString.with_infos
(sits
, remsp
, pos
, pos
+ remsp
- 1)
192 var st
= sits
.find_beginning_of_char_at
(pos
+ chunksz
- 1)
193 var bytelen
= st
- pos
194 rets
+= new FlatString.with_infos
(sits
, bytelen
, pos
, st
- 1)
198 if rets
isa Concat then return rets
.balance
202 # Read all the stream until the eof.
204 # The content of the file is returned verbatim.
205 fun read_all_bytes
: Bytes
207 if last_error
!= null then return new Bytes.empty
208 var s
= new Bytes.empty
211 if c
!= null then s
.add
(c
)
216 # Read a string until the end of the line and append it to `s`.
218 # Unlike `read_line` and other related methods,
219 # the line terminator '\n', if any, is preserved in each line.
220 # Use the method `Text::chomp` to safely remove it.
223 # var txt = "Hello\n\nWorld\n"
224 # var i = new StringReader(txt)
225 # var b = new FlatBuffer
226 # i.append_line_to(b)
227 # assert b == "Hello\n"
228 # i.append_line_to(b)
229 # assert b == "Hello\n\n"
230 # i.append_line_to(b)
235 # If `\n` is not present at the end of the result, it means that
236 # a non-eol terminated last line was returned.
239 # var i2 = new StringReader("hello")
241 # var b2 = new FlatBuffer
242 # i2.append_line_to(b2)
243 # assert b2 == "hello"
247 # NOTE: The single character LINE FEED (`\n`) delimits the end of lines.
248 # Therefore CARRIAGE RETURN & LINE FEED (`\r\n`) is also recognized.
249 fun append_line_to
(s
: Buffer)
251 if last_error
!= null then return
258 if x
== '\n' then return
263 # Is there something to read.
264 # This function returns 'false' if there is something to read.
265 fun eof
: Bool is abstract
267 # Read the next sequence of non whitespace characters.
269 # Leading whitespace characters are skipped.
270 # The first whitespace character that follows the result is consumed.
272 # An empty string is returned if the end of the file or an error is encounter.
275 # var w = new StringReader(" Hello, \n\t World!")
276 # assert w.read_word == "Hello,"
277 # assert w.read_char == '\n'.ascii
278 # assert w.read_word == "World!"
279 # assert w.read_word == ""
282 # `Char::is_whitespace` determines what is a whitespace.
283 fun read_word
: String
285 var buf
= new FlatBuffer
286 var c
= read_nonwhitespace
291 if c
== null then break
292 if c
.is_whitespace
then break
300 # Skip whitespace characters (if any) then return the following non-whitespace character.
302 # Returns the code point of the character.
303 # Returns `null` on end of file or error.
305 # In fact, this method works like `read_char` except it skips whitespace.
308 # var w = new StringReader(" \nab\tc")
309 # assert w.read_nonwhitespace == 'a'
310 # assert w.read_nonwhitespace == 'b'
311 # assert w.read_nonwhitespace == 'c'
312 # assert w.read_nonwhitespace == null
315 # `Char::is_whitespace` determines what is a whitespace.
316 fun read_nonwhitespace
: nullable Char
318 var c
: nullable Char = null
321 if c
== null or not c
.is_whitespace
then break
327 # Iterator returned by `Reader::each_line`.
328 # See the aforementioned method for details.
330 super Iterator[String]
332 # The original stream
337 var res
= not stream
.eof
338 if not res
and close_on_finish
then stream
.close
346 line
= stream
.read_line
352 # The last line read (cache)
353 private var line
: nullable String = null
358 if line
== null then item
363 # Close the stream when the stream is at the EOF.
366 var close_on_finish
= false is writable
370 if close_on_finish
then stream
.close
374 # `Reader` capable of declaring if readable without blocking
375 abstract class PollableReader
378 # Is there something to read? (without blocking)
379 fun poll_in
: Bool is abstract
383 # A `Stream` that can be written to
384 abstract class Writer
387 # The coder from a nit UTF-8 String to the output file
388 var coder
: Coder = utf8_coder
is writable
390 # Writes bytes from `s`
391 fun write_bytes
(s
: Bytes) is abstract
394 fun write
(s
: Text) is abstract
396 # Write a single byte
397 fun write_byte
(value
: Byte) is abstract
399 # Can the stream be used to write
400 fun is_writable
: Bool is abstract
403 # Things that can be efficienlty written to a `Writer`
405 # The point of this interface is to allow the instance to be efficiently
406 # written into a `Writer`.
408 # Ready-to-save documents usually provide this interface.
410 # Write itself to a `stream`
411 # The specific logic it let to the concrete subclasses
412 fun write_to
(stream
: Writer) is abstract
414 # Like `write_to` but return a new String (may be quite large)
416 # This funtionality is anectodical, since the point
417 # of streamable object to to be efficienlty written to a
418 # stream without having to allocate and concatenate strings
419 fun write_to_string
: String
421 var stream
= new StringWriter
429 redef fun write_to
(stream
) do stream
.write
(self)
432 # Input streams with a buffered input for efficiency purposes
433 abstract class BufferedReader
437 if last_error
!= null then return null
439 last_error
= new IOError("Stream has reached eof")
442 # TODO: Fix when supporting UTF-8
443 var c
= _buffer
[_buffer_pos
].to_i
.ascii
450 if last_error
!= null then return null
452 last_error
= new IOError("Stream has reached eof")
455 var c
= _buffer
[_buffer_pos
]
460 # Resets the internal buffer
466 # Peeks up to `n` bytes in the buffer
468 # The operation does not consume the buffer
471 # var x = new FileReader.open("File.txt")
472 # assert x.peek(5) == x.read(5)
474 fun peek
(i
: Int): Bytes do
475 if eof
then return new Bytes.empty
476 var remsp
= _buffer_length
- _buffer_pos
478 var bf
= new Bytes.with_capacity
(i
)
479 bf
.append_ns_from
(_buffer
, i
, _buffer_pos
)
482 var bf
= new Bytes.with_capacity
(i
)
483 bf
.append_ns_from
(_buffer
, remsp
, _buffer_pos
)
484 _buffer_pos
= _buffer_length
485 read_intern
(i
- bf
.length
, bf
)
486 remsp
= _buffer_length
- _buffer_pos
487 var full_len
= bf
.length
+ remsp
488 if full_len
> _buffer_capacity
then
489 var c
= _buffer_capacity
490 while c
< full_len
do c
= c
* 2 + 2
493 var nns
= new NativeString(_buffer_capacity
)
494 bf
.items
.copy_to
(nns
, bf
.length
, 0, 0)
495 _buffer
.copy_to
(nns
, remsp
, _buffer_pos
, bf
.length
)
498 _buffer_length
= full_len
502 redef fun read_bytes
(i
)
504 if last_error
!= null then return new Bytes.empty
505 var buf
= new Bytes.with_capacity
(i
)
510 # Fills `buf` with at most `i` bytes read from `self`
511 private fun read_intern
(i
: Int, buf
: Bytes): Int do
514 var bufsp
= _buffer_length
- p
517 buf
.append_ns_from
(_buffer
, i
, p
)
520 _buffer_pos
= _buffer_length
521 var readln
= _buffer_length
- p
522 buf
.append_ns_from
(_buffer
, readln
, p
)
523 var rd
= read_intern
(i
- readln
, buf
)
527 redef fun read_all_bytes
529 if last_error
!= null then return new Bytes.empty
530 var s
= new Bytes.with_capacity
(10)
534 var k
= _buffer_length
536 s
.append_ns_from
(b
, rd_sz
, j
)
543 redef fun append_line_to
(s
)
545 var lb
= new Bytes.with_capacity
(10)
547 # First phase: look for a '\n'
549 while i
< _buffer_length
and _buffer
[i
] != 0xAu
8 do
554 if i
< _buffer_length
then
555 assert _buffer
[i
] == 0xAu
8
562 # if there is something to append
563 if i
> _buffer_pos
then
564 # Copy from the buffer to the string
594 if _buffer_pos
< _buffer_length
then return false
595 if end_reached
then return true
597 return _buffer_pos
>= _buffer_length
and end_reached
601 private var buffer
: NativeString = new NativeString(0)
603 # The current position in the buffer
604 private var buffer_pos
= 0
606 # Length of the current buffer (i.e. nuber of bytes in the buffer)
607 private var buffer_length
= 0
609 # Capacity of the buffer
610 private var buffer_capacity
= 0
613 protected fun fill_buffer
is abstract
615 # Has the last fill_buffer reached the end
616 protected fun end_reached
: Bool is abstract
618 # Allocate a `_buffer` for a given `capacity`.
619 protected fun prepare_buffer
(capacity
: Int)
621 _buffer
= new NativeString(capacity
)
622 _buffer_pos
= 0 # need to read
624 _buffer_capacity
= capacity
628 # A `Stream` that can be written to and read from
629 abstract class Duplex
634 # `Stream` that can be used to write to a `String`
636 # Mainly used for compatibility with Writer type and tests.
640 private var content
= new Array[String]
641 redef fun to_s
do return content
.plain_to_s
642 redef fun is_writable
do return not closed
644 redef fun write_bytes
(b
) do
651 content
.add
(str
.to_s
)
654 # Is the stream closed?
655 protected var closed
= false
657 redef fun close
do closed
= true
660 # `Stream` used to read from a `String`
662 # Mainly used for compatibility with Reader type and tests.
666 # The string to read from.
669 # The current position in the string (bytewise).
670 private var cursor
: Int = 0
672 redef fun read_char
do
673 if cursor
< source
.length
then
674 # Fix when supporting UTF-8
675 var c
= source
[cursor
]
683 redef fun read_byte
do
684 if cursor
< source
.length
then
685 var c
= source
.bytes
[cursor
]
697 redef fun read_all_bytes
do
698 var nslen
= source
.length
- cursor
699 var nns
= new NativeString(nslen
)
700 source
.copy_to_native
(nns
, nslen
, cursor
, 0)
701 return new Bytes(nns
, nslen
, nslen
)
704 redef fun eof
do return cursor
>= source
.bytelen