[protobuf-perl commit] r154 - in trunk/perl: lib/Protobuf t

0 views
Skip to first unread message

codesite...@google.com

unread,
Jul 30, 2008, 5:55:17 PM7/30/08
to protobuf-p...@googlegroups.com
Author: br...@danga.com
Date: Wed Jul 30 14:55:05 2008
New Revision: 154

Modified:
trunk/perl/lib/Protobuf/Decoder.pm
trunk/perl/t/wire-numbers.t

Log:
all encoding and decoding works now\!

Modified: trunk/perl/lib/Protobuf/Decoder.pm
==============================================================================
--- trunk/perl/lib/Protobuf/Decoder.pm (original)
+++ trunk/perl/lib/Protobuf/Decoder.pm Wed Jul 30 14:55:05 2008
@@ -12,10 +12,6 @@
my ($class, $attr, $value) = @_;
my $type_name = lc type_name($attr->field->type);
my $method = "decode_field_" . $type_name;
- unless ($class->can($method)) {
- # by default
- return $value;
- }
return $class->$method($value);
}

@@ -97,6 +93,17 @@

my $sign_bit_64 = Math::BigInt->new("0x8000000000000000");

+sub decode_field_int64 {
+ my ($self, $int) = @_;
+ if (UNIVERSAL::isa($int, "Math::BigInt") && ($int & $sign_bit_64)) {
+ # TODO(bradfitz): pairs of bnot works too. benchmark which is faster.
+ $int->bneg;
+ $int &= Math::BigInt->new("0xffffffffffffffff");
+ $int->bneg;
+ }
+ return $int;
+}
+
sub decode_field_int32 {
my ($self, $int) = @_;
if (UNIVERSAL::isa($int, "Math::BigInt")) {
@@ -111,7 +118,10 @@
# shouldn't get here, though?
return $int->numify;
}
- return unpack("l", pack("L", int($int)));
+ # If it was negative at all, it would've been a negative 64-bit
+ # number (10 byte varint), so we know this is not a bigint and not
+ # negative here:
+ return $int;
}

sub decode_field_fixed32 {

Modified: trunk/perl/t/wire-numbers.t
==============================================================================
--- trunk/perl/t/wire-numbers.t (original)
+++ trunk/perl/t/wire-numbers.t Wed Jul 30 14:55:05 2008
@@ -54,6 +54,8 @@
['int32', -7, "\x08"."\xf9\xff\xff\xff\xff\xff\xff\xff\xff\x01"],
['int32', -2147483648, "\x08"."\x80\x80\x80\x80\xf8\xff\xff\xff\xff\x01"],

+ ['int64', 7, "\x10"."\x07"],
+ ['int64', -7, "\x10"."\xf9\xff\xff\xff\xff\xff\xff\xff\xff\x01"],
['int64', 0-(BI(2)**63), "\x10"."\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01"],

['uint32', $max_u32, "\x18"."\xff\xff\xff\xff\x0f"],
@@ -93,6 +95,8 @@

foreach my $t (@tests) {
my ($type, $num, $expected_encoded) = @$t;
+ next if $ENV{TEST_TYPE} && $type ne $ENV{TEST_TYPE};
+
$expected_encoded = "?" unless defined $expected_encoded;

# they're all prefixed with optional in their name in this type:

Reply all
Reply to author
Forward
0 new messages