Skip to content
Browse files

buffer: add UTF-16LE encoding name.

  • Loading branch information...
1 parent 6f82b9f commit f2e22363255d7355536a97813d4d882b0cf9774f @koichik committed May 3, 2012
Showing with 18 additions and 3 deletions.
  1. +6 −3 doc/api/buffer.markdown
  2. +8 −0 lib/buffer.js
  3. +4 −0 test/simple/test-buffer.js
View
9 doc/api/buffer.markdown
@@ -23,10 +23,13 @@ encoding method. Here are the different string encodings.
`0x20` (character code of a space). If you want to convert a null character
into `0x00`, you should use `'utf8'`.
-* `'utf8'` - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.
+* `'utf8'` - Multibyte encoded Unicode characters. Many web pages and other
+ document formats use UTF-8.
-* `'ucs2'` - 2-bytes, little endian encoded Unicode characters. It can encode
- only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).
+* `'utf16le'` - 2 or 4 bytes, little endian encoded Unicode characters.
+ Surrogate pairs (U+10000 to U+10FFFF) are supported.
+
+* `'ucs2'` - Alias to `'utf16le'`.
* `'base64'` - Base64 string encoding.
View
8 lib/buffer.js
@@ -89,6 +89,8 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
case 'ucs2':
case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
return this.ucs2Slice(start, end);
default:
@@ -173,6 +175,8 @@ SlowBuffer.prototype.write = function(string, offset, length, encoding) {
case 'ucs2':
case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
return this.ucs2Write(string, offset, length);
default:
@@ -374,6 +378,8 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
case 'ucs2':
case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
ret = this.parent.ucs2Write(string, this.offset + offset, length);
break;
@@ -425,6 +431,8 @@ Buffer.prototype.toString = function(encoding, start, end) {
case 'ucs2':
case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
return this.parent.ucs2Slice(start, end);
default:
View
4 test/simple/test-buffer.js
@@ -266,6 +266,10 @@ 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);
+assert.deepEqual(f, new Buffer('3DD84DDC', 'hex'));
+
var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
var g = new Buffer(arrayIsh);

0 comments on commit f2e2236

Please sign in to comment.
Something went wrong with that request. Please try again.