Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/write-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,28 @@ function getWriteType(options) {
}

function number(encoder, value) {
var ivalue = value | 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be leaved as is: it has the same semantics, but there are performance consideration to take into account. See https://jsperf.com/math-floor-alternatives

var type;
if (value !== ivalue) {
if (Math.floor(value) != value) {
// float 64 -- 0xcb
type = 0xcb;
token[type](encoder, value);
return;
} else if (-0x20 <= ivalue && ivalue <= 0x7F) {
} else if (-0x20 <= value && value <= 0x7F) {
// positive fixint -- 0x00 - 0x7f
// negative fixint -- 0xe0 - 0xff
type = ivalue & 0xFF;
} else if (0 <= ivalue) {
type = value & 0xFF;
} else if (0 <= value) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
type = (ivalue <= 0xFF) ? 0xcc : (ivalue <= 0xFFFF) ? 0xcd : 0xce;
// uint 64 -- 0xcf
type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this, together with line 74 are the most important. All the others should be reversed.

} else {
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
type = (-0x80 <= ivalue) ? 0xd0 : (-0x8000 <= ivalue) ? 0xd1 : 0xd2;
// int 64 -- 0xd3
type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3;
}
token[type](encoder, ivalue);
token[type](encoder, value);
}

// uint 64 -- 0xcf
Expand Down
6 changes: 5 additions & 1 deletion test/10.encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ function run_tests(codecopt) {
it("cc-cf: uint 8/16/32/64", function() {
assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF, options)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFF, options)), [0xce, 0xFF, 0xFF, 0xFF, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0x8000000000000000, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
assert.notDeepEqual(toArray(msgpack.encode(0x8000000000000001, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
assert.notDeepEqual(toArray(msgpack.encode(0xFFFFFFFFFFFFFFFF, options)), [0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
});

// int 8 -- 0xd0
Expand All @@ -138,6 +141,7 @@ function run_tests(codecopt) {
assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]);
assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]);
assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
assert.notDeepEqual(toArray(msgpack.encode(-0x80000001, options)), [0xd3, 0x80, 0x00, 0x00, 0x01]);
});

// str 8 -- 0xd9
Expand Down