Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Bugfix: node.encodeUtf8 was broken. (Connor Dunn)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Aug 6, 2009
1 parent fb7dd02 commit 9b3baf3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/mjsunit/test-encode-utf8.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 9b3baf3

Please sign in to comment.