fix writing empty string to buffer. closes gh-39

This commit is contained in:
brianc 2011-07-20 15:32:47 -05:00
parent 621857746d
commit c317606b0a
2 changed files with 13 additions and 1 deletions

View File

@ -37,7 +37,12 @@ p.addInt16 = function(num) {
}
p.addCString = function(string) {
var string = string || "";
//just write a 0 for empty or null strings
if(!string) {
this._ensure(1);
this.buffer[this.offset++] = 0;
return this;
}
var len = Buffer.byteLength(string) + 1;
this._ensure(len);
this.buffer.write(string, this.offset);

View File

@ -66,6 +66,13 @@ test('cString', function() {
var result = subject.addCString().join();
assert.equalBuffers(result, [0])
})
test('writes two empty cstrings', function() {
var subject = new Writer();
var result = subject.addCString("").addCString("").join();
assert.equalBuffers(result, [0, 0])
})
test('writes non-empty cstring', function() {
var subject = new Writer();