lib/binary: intro read/write strings as blocks
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 24 May 2015 17:24:33 +0000 (13:24 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 25 May 2015 16:00:26 +0000 (12:00 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/binary/binary.nit

index 9e63874..28ddf32 100644 (file)
@@ -97,6 +97,17 @@ redef abstract class Writer
                write_byte 0x00
        end
 
+       # Write the length as a 64 bits integer, then the content of `text`
+       #
+       # To be used with `Reader::read_block`.
+       #
+       # Compared to `write_string`, this method supports null bytes in `text`.
+       fun write_block(text: Text)
+       do
+               write_int64 text.length
+               write text
+       end
+
        # Write a floating point `value` on 32 bits
        #
        # Using this format may result in a loss of precision as it uses less bits
@@ -162,6 +173,16 @@ redef abstract class Reader
                end
        end
 
+       # Read the length as a 64 bits integer, then the content of the block
+       #
+       # To be used with `Writer::write_block`.
+       fun read_block: String
+       do
+               var length = read_int64
+               if length == 0 then return ""
+               return read(length)
+       end
+
        # Read a floating point on 32 bits and return it as a `Float`
        #
        # Using this format may result in a loss of precision as it uses less bits