From 9b3baf3d508dbef355b2952b15b5dd3f854f92cb Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 6 Aug 2009 13:17:30 +0200 Subject: [PATCH] Bugfix: node.encodeUtf8 was broken. (Connor Dunn) http://groups.google.com/group/nodejs/browse_thread/thread/5ad0660a0959d885# --- src/util.js | 24 +++++++++++++++++++++++- test/mjsunit/test-encode-utf8.js | 11 +++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/mjsunit/test-encode-utf8.js diff --git a/src/util.js b/src/util.js index 6d45dedfbd..67bccbb1f0 100644 --- a/src/util.js +++ b/src/util.js @@ -21,7 +21,29 @@ node.inherits = function (ctor, superCtor) { // This is useful for dealing with raw encodings. node.encodeUtf8 = function (array) { - return String.fromCharCode.apply(String, array); + var string = ""; + var i = 0; + var c = c1 = c2 = 0; + + while (i < array.length) { + c = array[i]; + + if (c < 128) { + string += String.fromCharCode(c); + i++; + } else if ((c > 191) && (c < 224)) { + c2 = array[i+1]; + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } else { + c2 = array[i+1]; + c3 = array[i+2]; + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + + return string; }; node.cat = function(location, encoding) { diff --git a/test/mjsunit/test-encode-utf8.js b/test/mjsunit/test-encode-utf8.js new file mode 100644 index 0000000000..461b8956fa --- /dev/null +++ b/test/mjsunit/test-encode-utf8.js @@ -0,0 +1,11 @@ +include("mjsunit.js"); + +function onLoad () { + var a = [116,101,115,116,32,206,163,207,131,207,128,206,177,32,226,161,140,226,160, 129,226,160,167,226,160,145]; + var s = node.encodeUtf8(a); + assertEquals("test Σσπα ⡌⠁⠧⠑", s); + + a = [104, 101, 108, 108, 111]; + s = node.encodeUtf8(a); + assertEquals("hello", s); +}