faster strings for bufferwriter.
This commit is contained in:
parent
5ef65a4129
commit
383014e754
@ -56,6 +56,7 @@ BufferWriter.prototype.render = function render(keep) {
|
|||||||
case '64be': off += utils.write64BE(data, item[1], off); break;
|
case '64be': off += utils.write64BE(data, item[1], off); break;
|
||||||
case 'varint': off += utils.writeVarint(data, item[1], off); break;
|
case 'varint': off += utils.writeVarint(data, item[1], off); break;
|
||||||
case 'bytes': off += item[1].copy(data, off); break;
|
case 'bytes': off += item[1].copy(data, off); break;
|
||||||
|
case 'str': off += data.write(item[1], off, item[2]); break;
|
||||||
case 'checksum':
|
case 'checksum':
|
||||||
off += utils.checksum(data.slice(0, off)).copy(data, off);
|
off += utils.checksum(data.slice(0, off)).copy(data, off);
|
||||||
break;
|
break;
|
||||||
@ -244,9 +245,10 @@ BufferWriter.prototype.getSize = function getSize() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
BufferWriter.prototype.writeString = function writeString(value, enc) {
|
BufferWriter.prototype.writeString = function writeString(value, enc) {
|
||||||
if (typeof value === 'string')
|
if (typeof value !== 'string')
|
||||||
value = new Buffer(value, enc);
|
return this.writeBytes(value);
|
||||||
this.writeBytes(value);
|
this.written += Buffer.byteLength(value, enc);
|
||||||
|
this.data.push(['str', value, enc]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -255,9 +257,7 @@ BufferWriter.prototype.writeString = function writeString(value, enc) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
BufferWriter.prototype.writeHash = function writeHash(value) {
|
BufferWriter.prototype.writeHash = function writeHash(value) {
|
||||||
if (typeof value === 'string')
|
this.writeString(value, 'hex');
|
||||||
value = new Buffer(value, 'hex');
|
|
||||||
this.writeBytes(value);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -267,9 +267,18 @@ BufferWriter.prototype.writeHash = function writeHash(value) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
||||||
if (typeof value === 'string')
|
var size;
|
||||||
value = new Buffer(value, enc);
|
|
||||||
this.writeVarBytes(value);
|
if (typeof value !== 'string')
|
||||||
|
return this.writeVarBytes(value);
|
||||||
|
|
||||||
|
size = Buffer.byteLength(value, enc);
|
||||||
|
|
||||||
|
this.written += utils.sizeVarint(size);
|
||||||
|
this.written += size;
|
||||||
|
|
||||||
|
this.data.push(['varint', size]);
|
||||||
|
this.data.push(['str', value, enc]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user