Skip to content

Commit

Permalink
it turns out that some UTF characters are more than 1 byte. :\ sillin…
Browse files Browse the repository at this point in the history
…ess is now fixed.
  • Loading branch information
cainus committed Apr 4, 2013
1 parent 89b90df commit 620ca7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/BufferMaker.js
Expand Up @@ -43,7 +43,11 @@ BufferMaker.prototype.make = function(){
for(i = 0; i < this.plan.length; i++){
item = this.plan[i];
if (item.type === 'string'){
bytecount += item.value.length;
if (Buffer.isBuffer(item.value)){
bytecount += item.value.length;
} else {
bytecount += Buffer.byteLength(item.value);
}
} else {
bytecount += types[item.type].bytes;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "buffermaker",
"description" : "buffermaker is a convenient way of creating binary strings",
"keywords" : ["buffer", "binary"],
"version": "0.0.8",
"version": "0.0.9",
"bugs": {
"url": "https://github.com/cainus/BufferMaker/issues"
},
Expand Down
18 changes: 18 additions & 0 deletions test/BufferMaker.js
Expand Up @@ -131,12 +131,30 @@ describe("BufferMaker", function(){
.make();
messageSet.should.eql(new Buffer([0, 0, 0, 4, 'f'.charCodeAt(0), 'o'.charCodeAt(0), 'u'.charCodeAt(0), 'r'.charCodeAt(0)]));
});
// non breaking space ( \u00a0 ) uses two bytes in utf8:
// http://en.wikipedia.org/wiki/UTF-8#Description
it ("can create a buffer from a string with some 2-byte utf8 in it", function(){
var expected = new Buffer("fo\u00a0ur");
var actual = new BufferMaker()
.string("fo\u00a0ur")
.make();
actual.should.eql(expected);
actual.toString('utf8').should.equal("fo\u00a0ur");
actual.length.should.equal(6);
expected.length.should.equal(6);
});
it("can create a buffer from a string", function(){
var buffer = new Buffer(8);
buffer.write("12345678");
var actual = new BufferMaker().string("12345678").make();
actual.should.eql(buffer);
});
it("can create a buffer from a weird string", function(){
var buffer = new Buffer(10);
buffer.write("1234\u00a05678");
var actual = new BufferMaker().string("1234\u00a05678").make();
actual.should.eql(buffer);
});

it("can create a buffer from a buffer", function(){
var buffer = new Buffer(8);
Expand Down

0 comments on commit 620ca7b

Please sign in to comment.