Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bugfix: node.encodeUtf8 was broken. (Connor Dunn)

  • Loading branch information...
commit 9b3baf3d508dbef355b2952b15b5dd3f854f92cb 1 parent fb7dd02
ry ry authored
Showing with 34 additions and 1 deletion.
  1. +23 −1 src/util.js
  2. +11 −0 test/mjsunit/test-encode-utf8.js
24 src/util.js
View
@@ -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) {
11 test/mjsunit/test-encode-utf8.js
View
@@ -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);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.