Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 9b3baf3d508dbef355b2952b15b5dd3f854f92cb 1 parent fb7dd02
ry ry authored

Showing 2 changed files with 34 additions and 1 deletion. Show diff stats Hide diff stats

  1. +23 1 src/util.js
  2. +11 0 test/mjsunit/test-encode-utf8.js
24 src/util.js
@@ -21,7 +21,29 @@ node.inherits = function (ctor, superCtor) {
21 21
22 22 // This is useful for dealing with raw encodings.
23 23 node.encodeUtf8 = function (array) {
24   - return String.fromCharCode.apply(String, array);
  24 + var string = "";
  25 + var i = 0;
  26 + var c = c1 = c2 = 0;
  27 +
  28 + while (i < array.length) {
  29 + c = array[i];
  30 +
  31 + if (c < 128) {
  32 + string += String.fromCharCode(c);
  33 + i++;
  34 + } else if ((c > 191) && (c < 224)) {
  35 + c2 = array[i+1];
  36 + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  37 + i += 2;
  38 + } else {
  39 + c2 = array[i+1];
  40 + c3 = array[i+2];
  41 + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  42 + i += 3;
  43 + }
  44 + }
  45 +
  46 + return string;
25 47 };
26 48
27 49 node.cat = function(location, encoding) {
11 test/mjsunit/test-encode-utf8.js
... ... @@ -0,0 +1,11 @@
  1 +include("mjsunit.js");
  2 +
  3 +function onLoad () {
  4 + 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];
  5 + var s = node.encodeUtf8(a);
  6 + assertEquals("test Σσπα ⡌⠁⠧⠑", s);
  7 +
  8 + a = [104, 101, 108, 108, 111];
  9 + s = node.encodeUtf8(a);
  10 + assertEquals("hello", s);
  11 +}

0 comments on commit 9b3baf3

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