BigInt parsing bullet-proofing

This commit is contained in:
sevastos 2013-05-23 20:10:08 +03:00
parent 30d97e3a31
commit f827f56ed2
5 changed files with 17 additions and 12 deletions

View File

@ -1,3 +1,5 @@
var ref = require('ref');
var parseBits = function(data, bits, offset, invert, callback) { var parseBits = function(data, bits, offset, invert, callback) {
offset = offset || 0; offset = offset || 0;
invert = invert || false; invert = invert || false;
@ -106,11 +108,7 @@ var parseInt32 = function(value) {
}; };
var parseInt64 = function(value) { var parseInt64 = function(value) {
if (parseBits(value, 1) == 1) { return String(ref.readInt64BE(value, 0));
return -1 * (parseBits(value, 63, 1, true) + 1);
}
return parseBits(value, 63, 1);
}; };
var parseFloat32 = function(value) { var parseFloat32 = function(value) {

View File

@ -162,8 +162,14 @@ var parseInteger = function(val) {
return parseInt(val, 10); return parseInt(val, 10);
}; };
var parseBigInteger = function(val) {
var valStr = String(val);
if (/^\d+$/.test(valStr)) { return valStr; }
return val;
};
var init = function(register) { var init = function(register) {
register(20, parseInteger); register(20, parseBigInteger);
register(21, parseInteger); register(21, parseInteger);
register(23, parseInteger); register(23, parseInteger);
register(26, parseInteger); register(26, parseInteger);

View File

@ -19,7 +19,8 @@
"main": "./lib", "main": "./lib",
"dependencies": { "dependencies": {
"generic-pool": "2.0.3", "generic-pool": "2.0.3",
"buffer-writer": "1.0.0" "buffer-writer": "1.0.0",
"ref": "0.1.3"
}, },
"devDependencies": { "devDependencies": {
"jshint": "1.1.0", "jshint": "1.1.0",

View File

@ -46,7 +46,7 @@ var types = [{
values: [-1, 0, 1, null] values: [-1, 0, 1, null]
},{ },{
name: 'bigint', name: 'bigint',
values: [-10000, 0, 10000, null] values: ['-9223372036854775808', '-9007199254740992', '0', '72057594037928030', '9007199254740992', '9223372036854775807', null]
},{ },{
name: 'varchar(5)', name: 'varchar(5)',
values: ['yo', '', 'zomg!', null] values: ['yo', '', 'zomg!', null]

View File

@ -30,8 +30,8 @@ test('typed results', function() {
name: 'bigint/int8', name: 'bigint/int8',
format: 'text', format: 'text',
dataTypeID: 20, dataTypeID: 20,
actual: '102', actual: '9223372036854775807',
expected: 102 expected: '9223372036854775807'
},{ },{
name: 'oid', name: 'oid',
format: 'text', format: 'text',
@ -222,13 +222,13 @@ test('typed results', function() {
format: 'binary', format: 'binary',
dataTypeID: 20, dataTypeID: 20,
actual: [0, 0, 0, 0, 0, 0, 0, 102], actual: [0, 0, 0, 0, 0, 0, 0, 102],
expected: 102 expected: '102'
},{ },{
name: 'binary-bigint/int8-full', name: 'binary-bigint/int8-full',
format: 'binary', format: 'binary',
dataTypeID: 20, dataTypeID: 20,
actual: [1, 0, 0, 0, 0, 0, 0, 102], actual: [1, 0, 0, 0, 0, 0, 0, 102],
expected: 72057594037928030 expected: '72057594037928038'
},{ },{
name: 'binary-oid', name: 'binary-oid',
format: 'binary', format: 'binary',