Skip to content

Commit

Permalink
Merge 7d324db into b77a3f9
Browse files Browse the repository at this point in the history
  • Loading branch information
coolme200 committed Jul 1, 2015
2 parents b77a3f9 + 7d324db commit 72405f4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

21 changes: 19 additions & 2 deletions lib/byte.js
Expand Up @@ -346,6 +346,7 @@ ByteBuffer.prototype._putString = function (index, value, format) {
return this;
};

// Prints a string to the Buffer, encoded as UTF-8
ByteBuffer.prototype.putRawString = function (index, str) {
if (typeof index === 'string') {
// putRawString(str)
Expand All @@ -358,7 +359,23 @@ ByteBuffer.prototype.putRawString = function (index, str) {
if (!str || str.length === 0) {
return this;
}
this._bytes.write(str, index);
for (var i = 0, len = str.length; i < len; i++) {
var ch = str.charCodeAt(i);
if (ch < 0x80) {
this._bytes[index++] = ch >>> 32;
} else if (ch < 0x800) {
this._bytes[index++] = (0xc0 + ((ch >> 6) & 0x1f)) >>> 32;
this._bytes[index++] = (0x80 + (ch & 0x3f)) >>> 32;
} else {
this._bytes[index++] = (0xe0 + ((ch >> 12) & 0xf)) >>> 32;
this._bytes[index++] = (0x80 + ((ch >> 6) & 0x3f)) >>> 32;
this._bytes[index++] = (0x80 + (ch & 0x3f)) >>> 32;
}
}
if (index > this._offset) {
this._offset = index;
}
// this._bytes.write(str, index);
return this;
};

Expand Down Expand Up @@ -482,7 +499,7 @@ ByteBuffer.prototype.skip = function (size) {

ByteBuffer.prototype.flip = function () {
// switch to read mode
this.limit(this.position());
this.limit(this.position());
this.position(0);
return this;
};
Expand Down
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -20,19 +20,19 @@
"dependencies": {
"debug": "~2.2.0",
"is-type-of": "~0.3.1",
"long": "~2.2.3",
"utility": "~1.3.2"
"long": "~2.2.5",
"utility": "~1.4.0"
},
"devDependencies": {
"autod": "*",
"beautify-benchmark": "*",
"benchmark": "*",
"beautify-benchmark": "0",
"benchmark": "1",
"contributors": "*",
"istanbul": "*",
"jshint": "*",
"mocha": "*",
"optimized": "~1.2.0",
"should": "~6.0.3"
"optimized": "1",
"should": "7"
},
"homepage": "https://github.com/node-modules/byte",
"repository": {
Expand Down
23 changes: 23 additions & 0 deletions test/byte.test.js
Expand Up @@ -486,6 +486,29 @@ describe('byte.test.js', function () {
bytes.putRawString('');
bytes.toString().should.equal('<ByteBuffer>');
});

it('should put emoji', function () {
// utf8
var bytes = ByteBuffer.allocate(1);
var str = new Buffer('aGVsbG/ppoPlsLI=', 'base64').toString();
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer 68 65 6c 6c 6f e9 a6 83 e5 b0 b2>');
bytes.getRawString(0, 11).should.eql(str);
// gbk
var bytes = ByteBuffer.allocate(1);
var str = new Buffer('aGVsbG/wn4y8', 'base64').toString();
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer 68 65 6c 6c 6f ed a0 bc ed bc bc>');
bytes.getRawString(0, 11).should.eql(str);

// 😀Www那
var bytes = ByteBuffer.allocate(1);
// java encode bytes: [-19, -96, -67, -19, -72, -128, 87, 119, 119, -23, -126, -93]
var str = new Buffer([-19, -96, -67, -19, -72, -128, 87, 119, 119, -23, -126, -93]).toString();
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer ed a0 bd ed b8 80 57 77 77 e9 82 a3>');
bytes.getRawString(0, 12).should.eql(str);
});
});

describe('array(), copy()', function () {
Expand Down

0 comments on commit 72405f4

Please sign in to comment.