Browse files

buffer: fix byteLength with UTF-16LE

Fixes #4075.
  • Loading branch information...
1 parent bf0bc35 commit fbb0ee6f24cb8679cb7b9de0cdbe9ad80f1363c7 @koichik koichik committed Oct 2, 2012
Showing with 46 additions and 32 deletions.
  1. +4 −0 src/node.cc
  2. +42 −32 test/simple/test-buffer.js
View
4 src/node.cc
@@ -1093,6 +1093,10 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
return UCS2;
} else if (strcasecmp(*encoding, "ucs-2") == 0) {
return UCS2;
+ } else if (strcasecmp(*encoding, "utf16le") == 0) {
+ return UCS2;
+ } else if (strcasecmp(*encoding, "utf-16le") == 0) {
+ return UCS2;
} else if (strcasecmp(*encoding, "binary") == 0) {
return BINARY;
} else if (strcasecmp(*encoding, "hex") == 0) {
View
74 test/simple/test-buffer.js
@@ -247,24 +247,26 @@ var f = new Buffer('über', 'ascii');
console.error('f.length: %d (should be 4)', f.length);
assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
-var f = new Buffer('über', 'ucs2');
-console.error('f.length: %d (should be 8)', f.length);
-assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
-
-var f = new Buffer('привет', 'ucs2');
-console.error('f.length: %d (should be 12)', f.length);
-assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
-assert.equal(f.toString('ucs2'), 'привет');
-
-var f = new Buffer([0, 0, 0, 0, 0]);
-assert.equal(f.length, 5);
-var size = f.write('あいうえお', 'ucs2');
-var charsWritten = Buffer._charsWritten; // Copy value out.
-console.error('bytes written to buffer: %d (should be 4)', size);
-console.error('chars written to buffer: %d (should be 2)', charsWritten);
-assert.equal(size, 4);
-assert.equal(charsWritten, 2);
-assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ var f = new Buffer('über', encoding);
+ console.error('f.length: %d (should be 8)', f.length);
+ assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
+
+ var f = new Buffer('привет', encoding);
+ console.error('f.length: %d (should be 12)', f.length);
+ assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
+ assert.equal(f.toString(encoding), 'привет');
+
+ var f = new Buffer([0, 0, 0, 0, 0]);
+ assert.equal(f.length, 5);
+ var size = f.write('あいうえお', encoding);
+ var charsWritten = Buffer._charsWritten; // Copy value out.
+ console.error('bytes written to buffer: %d (should be 4)', size);
+ console.error('chars written to buffer: %d (should be 2)', charsWritten);
+ assert.equal(size, 4);
+ assert.equal(charsWritten, 2);
+ assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
+});
var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
assert.equal(f.length, 4);
@@ -456,7 +458,9 @@ assert.equal('bcde', b.slice(1).toString());
// byte length
assert.equal(14, Buffer.byteLength('Il était tué'));
assert.equal(14, Buffer.byteLength('Il était tué', 'utf8'));
-assert.equal(24, Buffer.byteLength('Il était tué', 'ucs2'));
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ assert.equal(24, Buffer.byteLength('Il était tué', encoding));
+});
assert.equal(12, Buffer.byteLength('Il était tué', 'ascii'));
assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
@@ -581,9 +585,11 @@ for (var i = 0; i < 16; i++) assert.equal(0, b[i]);
for (; i < 32; i++) assert.equal(1, b[i]);
for (; i < b.length; i++) assert.equal(0, b[i]);
-var b = new SlowBuffer(10);
-b.write('あいうえお', 'ucs2');
-assert.equal(b.toString('ucs2'), 'あいうえお');
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ var b = new SlowBuffer(10);
+ b.write('あいうえお', encoding);
+ assert.equal(b.toString(encoding), 'あいうえお');
+});
// Binary encoding should write only one byte per character.
var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
@@ -670,14 +676,16 @@ assert.equal(buf[1], 0xAB);
assert.equal(buf[2], 0xCD);
assert.equal(buf[3], 0xFF);
-buf.fill(0xFF);
-written = buf.write('abcd', 0, 2, 'ucs2');
-console.log(buf);
-assert.equal(written, 2);
-assert.equal(buf[0], 0x61);
-assert.equal(buf[1], 0x00);
-assert.equal(buf[2], 0xFF);
-assert.equal(buf[3], 0xFF);
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ buf.fill(0xFF);
+ written = buf.write('abcd', 0, 2, encoding);
+ console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0x61);
+ assert.equal(buf[1], 0x00);
+ assert.equal(buf[2], 0xFF);
+ assert.equal(buf[3], 0xFF);
+});
// test for buffer overrun
buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
@@ -690,8 +698,10 @@ assert.equal(buf[4], 0);
buf = new Buffer(9);
buf.write('あいうえ', 'utf8'); // 3bytes * 4
assert.equal(Buffer._charsWritten, 3);
-buf.write('あいうえお', 'ucs2'); // 2bytes * 5
-assert.equal(Buffer._charsWritten, 4);
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ buf.write('あいうえお', encoding); // 2bytes * 5
+ assert.equal(Buffer._charsWritten, 4);
+});
buf.write('0123456789', 'ascii');
assert.equal(Buffer._charsWritten, 9);
buf.write('0123456789', 'binary');

0 comments on commit fbb0ee6

Please sign in to comment.