writer#addString

This commit is contained in:
brianc 2011-01-01 12:36:26 -06:00
parent 99086cf4bf
commit eb3e4ca3ab
2 changed files with 28 additions and 2 deletions

View File

@ -60,6 +60,15 @@ p.join = function() {
return this.buffer.slice(0, this.offset);
}
p.addString = function(string) {
var string = string || "";
var len = Buffer.byteLength(string);
this._ensure(len);
this.buffer.write(string, this.offset);
this.offset += len;
return this;
}
p.getByteLength = function() {
return this.offset;
}

View File

@ -105,10 +105,27 @@ test('gets correct byte length', function() {
test('can add arbitrary buffer to the end', function() {
var subject = new Writer(4);
subject.addCString("!!!")
var result = subject.add(Buffer("!!!")).join();
assert.equalBuffers(result, [33, 33, 33, 0, 33, 33, 33]);
var result = subject.add(Buffer("@@@")).join();
assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]);
})
test('can write normal string', function() {
var subject = new Writer(4);
var result = subject.addString("!").join();
assert.equalBuffers(result, [33]);
test('can write cString too', function() {
var result = subject.addCString("!").join();
assert.equalBuffers(result, [33, 33, 0]);
test('can resize', function() {
var result = subject.addString("!!").join();
assert.equalBuffers(result, [33, 33, 0, 33, 33]);
})
})
})
test('clearing', function() {
var subject = new Writer();
subject.addCString("@!!#!#");