forked from nitlang/nit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: nitlang#2539 Reviewed-by: Jean Privat <jean@pryen.org> Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
- Loading branch information
Showing
3 changed files
with
144 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters