From 5d49ead65d1001dba43d9e8b9e2d54313bb407ad Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 13 Nov 2013 18:02:14 -0800 Subject: [PATCH] fixed bug with readStringNT causing loss of end character --- lib/smart-buffer.js | 3 ++- test/test.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/smart-buffer.js b/lib/smart-buffer.js index 0a74478..6e7c25c 100644 --- a/lib/smart-buffer.js +++ b/lib/smart-buffer.js @@ -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; diff --git a/test/test.js b/test/test.js index a635665..6cb08fe 100644 --- a/test/test.js +++ b/test/test.js @@ -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();