Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Last active December 22, 2015 11:19
Show Gist options
  • Save FROGGS/6464777 to your computer and use it in GitHub Desktop.
Save FROGGS/6464777 to your computer and use it in GitHub Desktop.
nqp::(un)pack proposal
my @items := nqp::unpack( 'U2 (xxxC)[W]', $binary_string );

I, FROGGS, solemnly propose the implementation of pack() and unpack in nqp.

For one, these are useful for Perl 5 and Perl 6, and the other reason is that they should be pretty fast. If a backend then can even provide a faster implementation (e.g. in C), then it would be easy to #ifdef it in.

The signatures I propose are:

my $str  := nqp::pack( "CCC", [65, 66, 67] );
my @list := nqp::unpack( "CCC", $str );

You might wonder how this will cope with Buf, Str, utf8 and what not. Since pack/unpack work on byte-level, and one must be able to extract a single byte of a utf8-byte-sequence, it gets a string of octets.

class utf8
    multi method unpack(utf8:D: $template) {
        nqp::unpack( $template, nqp::decode(self, 'ascii') );
    }
}

Implementaion details

I would implement it exactly like nqp::sprintf, because unpack supports group-repeatition and relative positioning inside groups.

What needs to be discussed is how diretives like I should be handled. I'd like to see some sort of dynamic variable, a hash, which can be set to specify how many bytes a native int should have when packing it into a string. Same technique would be useful for sprintf's platform specific directives.

@FROGGS
Copy link
Author

FROGGS commented Jan 2, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment