Merge: core: intro BytesWriter and BytesReader
authorJean Privat <jean@pryen.org>
Mon, 18 Sep 2017 15:19:50 +0000 (11:19 -0400)
committerJean Privat <jean@pryen.org>
Mon, 18 Sep 2017 15:19:50 +0000 (11:19 -0400)
Intro the classes `BytesReader` and `BytesWriter` to support byte read/writer operations directly on `Bytes` in memory.

The new classes are now super classes to `StringReader` (converting the `String` source to `Bytes` in the constructor) and `StringWriter`(cleaning the UTF-8 string in `to_s` only). The only differences between the string and bytes streams are the behavior of `StringWriter::to_s` and the `String` parameter expected by the constructor of `StringReader`.

`BytesWriter` supports writing any bytes, including composing UTF-8 characters byte by byte, a behavior also useful for `StringWriter`:
~~~
var writer = new BytesWriter

# Write just the character first half
writer.write_byte 0xC2u8
assert writer.to_s == "\\xC2"
assert writer.bytes.to_s == "�"

# Complete the character
writer.write_byte 0xA2u8
assert writer.to_s == "\\xC2\\xA2"
assert writer.bytes.to_s == "¢"
~~~

`BytesReader` also supports reading any bytes, including the bytes composing an UTF-8 character for `StringReader`:

~~~
var reader = new BytesReader(b"a…b")
assert reader.read_char == 'a'
assert reader.read_byte == 0xE2u8 # 1st byte of '…'
assert reader.read_byte == 0x80u8 # 2nd byte of '…'
assert reader.read_char == '�' # Reads the last byte as an invalid char
assert reader.read_all_bytes == b"b"
~~~

Pull-Request: #2539
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>


Trivial merge