Skip to content

Commit

Permalink
fix: readFloat 2.3 => 2.299
Browse files Browse the repository at this point in the history
  • Loading branch information
tangyao committed Nov 18, 2015
1 parent 1f8ccd7 commit 88abdae
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
61 changes: 61 additions & 0 deletions benchmark/buffer_toString_fromCharCode.js
@@ -0,0 +1,61 @@
/**!
* byte - benchmark/buffer_toString_fromCharCode.js
*
* Copyright(c) tangyao and other contributors.
* MIT Licensed
*
* Authors:
* tangyao <2001-wms@163.com>
*/

'use strict';

/**
* Module dependencies.
*/

var Benchmark = require('benchmark');
var benchmarks = require('beautify-benchmark');

function getRawString(_bytes, index, length) {
var data = [];
for (var pos = index, end = index + length; pos < end; pos++) {
var ch = _bytes[pos];
if (ch < 0x80) {
data.push(ch);
} else if ((ch & 0xe0) === 0xc0) {
var ch1 = _bytes[++pos];
var v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);
data.push(v);
} else if ((ch & 0xf0) === 0xe0) {
var ch1 = _bytes[++pos];
var ch2 = _bytes[++pos];
var v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
data.push(v);
}
}
return String.fromCharCode.apply(null, data);
}

var suite = new Benchmark.Suite();

var buffer = new Buffer('eda0bdedb880577777e982a3', 'hex');

suite
.add('buffer.toString', function () {
buffer.toString('utf8', 0, 12);
})
.add('getRawString', function () {
getRawString(buffer, 0, 12);
})

.on('cycle', function(event) {
benchmarks.add(event.target);
})
.on('start', function(event) {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', function done() {
benchmarks.log();
})
.run({ 'async': false });
4 changes: 3 additions & 1 deletion lib/byte.js
Expand Up @@ -170,6 +170,7 @@ Object.keys(numbers).forEach(function (type) {
var getMethod = 'get' + type;
var handles = numbers[type];
var size = handles.size;
var isFloat = type === 'Float';

ByteBuffer.prototype[putMethod] = function (index, value) {
// index, value
Expand Down Expand Up @@ -198,7 +199,8 @@ Object.keys(numbers).forEach(function (type) {
var handle = this._order === BIG_ENDIAN
? handles.readBE
: handles.readLE;
return this._bytes[handle](index);
var rs = this._bytes[handle](index);
return isFloat ? Number(rs.toLocaleString()) : rs;
};
});

Expand Down
12 changes: 10 additions & 2 deletions test/byte.test.js
Expand Up @@ -18,6 +18,7 @@
var should = require('should');
var Long = require('long');
var ByteBuffer = require('../');
var debug = require('debug')('byte');

describe('byte.test.js', function () {
describe('new ByteBuffer()', function () {
Expand Down Expand Up @@ -134,16 +135,23 @@ describe('byte.test.js', function () {
bytes.getFloat().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
debug('putFloat', item[0]);
bytes.putFloat(0, item[0]);
bytes.toString().should.equal(item[1]);
String(bytes.getFloat(0)).should.containEql(item[0]);
bytes.getFloat(0).should.containEql(item[0]);

bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putFloat(0, item[0]);
bytes.toString().should.equal(item[2]);
String(bytes.getFloat(0)).should.containEql(item[0]);
bytes.getFloat(0).should.containEql(item[0]);
});

bytes.position(0);
bytes.putFloat(2.3);
bytes.getFloat(0).should.eql(2.3);

});

});

describe('putInt(), getInt()', function () {
Expand Down

0 comments on commit 88abdae

Please sign in to comment.