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') );
}
}
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.
http://www.catb.org/esr/structure-packing/