Skip to content

Commit

Permalink
Support wildcard * in Packer#unpack methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-kimoto committed Dec 1, 2024
1 parent f7b7555 commit 9e2f51b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
Native::Field
Native::Method
Native::MethodCall
* Add C, S, L, Q specifiers to Packer#unpack and Packer#pack method.
* Add H, h specifiers to Packer#unpack and Packer#pack method.
* Add C, S, L, Q specifiers to Packer#unpack and Packer#pack methods.
* Add H, h specifiers to Packer#unpack and Packer#pack methods.
* Support wildcard * in Packer#unpack and Packer#pack methods.
[Performance Improvement]
* Methods in Packer class are precompiled.
[Internal Changes]
Expand Down
22 changes: 16 additions & 6 deletions lib/SPVM/Packer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ Packer class in L<SPVM> has methods for pack and unpack operations
my $packer = Packer->new;
my $objects = [(object)1, 2L, 0.5, [1, 2], "abc"];
{
my $objects = [(object)1, 2L, 0.5, [1, 2], "abc"];
my $binary = $packer->pack("lqdl2a128");
my $objects_unpack = $packer->unpack($binary);
}
{
my $objects = [(object)[1, 2, 3]];
my $binary = $packer->pack("l*");
my $objects_unpack = $packer->unpack($binary);
}
my $binary = $packer->pack("lqdl2a128");
my $objects_unpack = $packer->unpack($binary);
=head1 Class Methods
C<static method new : Packer ();>
Expand Down Expand Up @@ -83,7 +93,7 @@ If big-endian is specified, the binary data is converted from big-endian to syst
If little-endian is specified, the binary data is converted from little-endian to system-endian in L</"pack"> method, or from system-endian to little-endian in L</"unpack"> method.
A length must be a positive integer. If a length is specified, the type(such as C<Int>) becomes a corresponding array type(such as C<int[]>).
A length must be a positive integer or a wildcard C<*>. If a length or a wildcard is specified, the type(such as C<Int>) becomes a corresponding array type(such as C<int[]>).
Exceptions:
Expand Down
4 changes: 4 additions & 0 deletions lib/SPVM/Packer.spvm
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ class Packer {

my $process_length = $length;

if ($has_wildcard_length) {
$process_length = ($binary_length - $binary_offset) / $size;
}

if ($specifier_type == Packer::Specifier->TYPE_HEX_STRING_LOW || $specifier_type == Packer::Specifier->TYPE_HEX_STRING_HIGH) {
$process_length = ($process_length + 1) / 2;
}
Expand Down
12 changes: 4 additions & 8 deletions t/02_vm/lib/SPVM/TestCase/Module/Packer.spvm
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ class TestCase::Module::Packer {
{
my $objects = [(object)"abc"];

my $template_pack = "a*";
my $template = "a*";

my $template = "a3";

my $binary = $packer->pack($template_pack, $objects);
my $binary = $packer->pack($template, $objects);

unless (length $binary == 3) {
return 0;
Expand Down Expand Up @@ -837,11 +835,9 @@ class TestCase::Module::Packer {
{
my $objects = [(object)[Fn->INT_MIN, 1]];

my $template_pack = "l*";
my $template = "l*";

my $template = "l2";

my $binary = $packer->pack($template_pack, $objects);
my $binary = $packer->pack($template, $objects);

unless (length $binary == 4 * 2) {
return 0;
Expand Down

0 comments on commit 9e2f51b

Please sign in to comment.