Browse Source

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

http://groups.google.com/group/nodejs/browse_thread/thread/5ad0660a0959d885#
v0.7.4-release
Ryan 16 years ago
parent
commit
9b3baf3d50
  1. 24
      src/util.js
  2. 11
      test/mjsunit/test-encode-utf8.js

24
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) {

11
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);
}
Loading…
Cancel
Save