resize internal buffer on cstring

This commit is contained in:
brianc 2010-12-29 20:33:36 -06:00
parent ee35bd8a78
commit dd1e291ef3

View File

@ -44,6 +44,7 @@ p.addInt16 = function(num) {
p.addCString = function(string) { p.addCString = function(string) {
var string = string || ""; var string = string || "";
var len = Buffer.byteLength(string) + 1; var len = Buffer.byteLength(string) + 1;
this._ensure(len);
this.offset += len; this.offset += len;
this.buffer.write(string); this.buffer.write(string);
this.buffer[this.offset] = 0; //add null terminator this.buffer[this.offset] = 0; //add null terminator
@ -120,4 +121,17 @@ test('cString', function() {
assert.equalBuffers(result, [0]) assert.equalBuffers(result, [0])
}) })
test('writes non-empty cstring', function() {
var subject = new ElasticBuffer();
var result = subject.addCString("!!!").join();
assert.equalBuffers(result, [33, 33, 33, 0]);
})
test('resizes if reached end', function() {
var subject = new ElasticBuffer(3);
var result = subject.addCString("!!!").join();
assert.equalBuffers(result, [33, 33, 33, 0]);
})
}) })