Skip to content

Commit

Permalink
fixed bug with readStringNT causing loss of end character
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh committed Nov 14, 2013
1 parent b90132d commit 5d49ead
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/smart-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ var SmartBuffer = (function () {
* @returns {string}
*/
SmartBuffer.prototype.readStringNT = function (encoding) {
var nullpos = this.length - 1;
var nullpos = this.length;
for (var i = this._readOffset; i < this.length; i++) {
if (this.buff[i] == 0x00) {
nullpos = i;
break;
}
}

var result = this.buff.slice(this._readOffset, nullpos);
this._readOffset = nullpos + 1;

Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ describe("Reading/Writing To/From SmartBuffer", function () {
});
});

describe("Null/non-null terminating strings", function () {
var reader = new SmartBuffer();
reader.writeString("hello\0test\0bleh");

it("should equal hello", function() {
assert.strictEqual(reader.readStringNT(), "hello");
});

it("should equal: test", function() {
assert.strictEqual(reader.readString(4), "test");
});

it("should have a length of zero", function() {
assert.strictEqual(reader.readStringNT().length, 0);
});

it("should equal: bleh", function () {
assert.strictEqual(reader.readStringNT(), "bleh");
});


});

describe("Buffer Values", function () {
var buff = new Buffer([0x01, 0x02, 0x04, 0x08, 0x16, 0x32, 0x64]);
var reader = new SmartBuffer();
Expand Down

0 comments on commit 5d49ead

Please sign in to comment.