Skip to content

Commit af12e7e

Browse files
committed
Speed up, fix, and update the buffer_extras lib.
1 parent e233e5c commit af12e7e

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

lib/buffer_extras.js

+19-33
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,35 @@ proto.zeroOut = function zeroOut() {
3333
// Writes a 32 bit integer at offset
3434
proto.int32Write = function int32Write(number, offset) {
3535
offset = offset || 0;
36-
var unsigned = (number < 0) ? (number + 0x100000000) : number;
37-
this[offset] = Math.floor(unsigned / 0xffffff);
38-
unsigned &= 0xffffff;
39-
this[offset + 1] = Math.floor(unsigned / 0xffff);
40-
unsigned &= 0xffff;
41-
this[offset + 2] = Math.floor(unsigned / 0xff);
42-
unsigned &= 0xff;
43-
this[offset + 3] = Math.floor(unsigned);
36+
this[offset] = number >>> 24;
37+
this[offset + 1] = number >>> 16;
38+
this[offset + 2] = number >>> 8;
39+
this[offset + 3] = number >>> 0;
4440
};
4541

4642
// Writes a 16 bit integer at offset
4743
proto.int16Write = function int16Write(number, offset) {
4844
offset = offset || 0;
49-
var unsigned = (number < 0) ? (number + 0x10000) : number;
50-
this[offset] = Math.floor(unsigned / 0xff);
51-
unsigned &= 0xff;
52-
this[offset + 1] = Math.floor(unsigned);
45+
this[offset] = number >>> 8;
46+
this[offset + 1] = number >>> 0;
5347
}
5448

5549
// Reads a 32 bit integer from offset
5650
proto.int32Read = function int32Read(offset) {
5751
offset = offset || 0;
58-
var unsigned = this[offset] * 0x1000000 +
59-
this[offset + 1] * 0x10000 +
60-
this[offset + 2] * 0x100 +
61-
this[offset + 3];
62-
return (unsigned & 0x80000000) ? (unsigned - 0x100000000) : unsigned
52+
return this[offset] * 0x1000000 +
53+
this[offset + 1] << 16 +
54+
this[offset + 2] << 8 +
55+
this[offset + 3];
6356
};
6457

6558
// Reads a 32 bit integer from offset
6659
proto.int16Read = function int16Read(offset) {
6760
offset = offset || 0;
68-
var unsigned = this[offset] * 0x100 +
69-
this[offset + 1];
70-
return (unsigned & 0x8000) ? (unsigned - 0x10000) : unsigned
61+
return this[offset] << 8 +
62+
this[offset + 1];
7163
};
7264

73-
Buffer.fromString = function fromString(string) {
74-
var b = new Buffer(Buffer.byteLength(string));
75-
b.write(string, 'utf8');
76-
return b;
77-
}
78-
7965
Buffer.makeWriter = function makeWriter() {
8066
var data = [];
8167
var writer;
@@ -93,15 +79,15 @@ Buffer.makeWriter = function makeWriter() {
9379
return writer;
9480
},
9581
string: function pushString(string) {
96-
data.push(Buffer.fromString(string));
82+
data.push(new Buffer(string, 'utf8'));
9783
return writer;
9884
},
9985
cstring: function pushCstring(string) {
100-
data.push(Buffer.fromString(string + "\0"));
86+
data.push(new Buffer(string + "\0", 'utf8'));
10187
return writer;
10288
},
10389
multicstring: function pushMulticstring(fields) {
104-
data.push(Buffer.fromString(fields.join("\0") + "\0\0"));
90+
data.push(new Buffer(fields.join("\0") + "\0\0", 'utf8'));
10591
return writer;
10692
},
10793
hash: function pushHash(hash) {
@@ -111,14 +97,14 @@ Buffer.makeWriter = function makeWriter() {
11197
var key = keys[i];
11298
pairs.push(key + "\0" + hash[key] + "\0");
11399
}
114-
data.push(Buffer.fromString(pairs.join("") + "\0"));
100+
data.push(new Buffer(pairs.join("") + "\0", 'utf8'));
115101
return writer;
116102
}
117103
};
118104
writer = {
119105
data: data,
120106
push: push,
121-
107+
122108
// Convert an array of buffers into a single buffer using memcopy
123109
toBuffer: function toBuffer() {
124110
var total = 0;
@@ -192,7 +178,7 @@ proto.toReader = function toReader() {
192178

193179
// Test It
194180
// var sys = require('sys');
195-
//
181+
//
196182
// var w = Buffer.makeWriter();
197183
// w.push.int32(305419896);
198184
// w.push.int16(4660);
@@ -215,4 +201,4 @@ proto.toReader = function toReader() {
215201
// r.int32(),
216202
// r.int16()
217203
// ]);
218-
//
204+
//

0 commit comments

Comments
 (0)