From: Jean Privat Date: Mon, 18 Sep 2017 15:19:50 +0000 (-0400) Subject: Merge: core: intro BytesWriter and BytesReader X-Git-Url: http://nitlanguage.org?ds=sidebyside Merge: core: intro BytesWriter and BytesReader 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 Reviewed-by: Lucas Bajolet --- 1f1aa04c817c9a8f2e31cb2f627f635a1ab4639c