1
0
Fork 0
forked from 0x2620/oxjs

misc updates to ox.js

This commit is contained in:
rolux 2012-01-04 13:12:48 +05:30
commit 2ef642fdeb
11 changed files with 136 additions and 93 deletions

View file

@ -433,12 +433,12 @@
if (code < 128) {
str = chr;
} else if (code < 2048) {
str = String.fromCharCode(code >> 6 | 192) +
String.fromCharCode(code & 63 | 128);
str = String.fromCharCode(code >> 6 | 192)
+ String.fromCharCode(code & 63 | 128);
} else {
str = String.fromCharCode(code >> 12 | 224) +
String.fromCharCode(code >> 6 & 63 | 128) +
String.fromCharCode(code & 63 | 128);
str = String.fromCharCode(code >> 12 | 224)
+ String.fromCharCode(code >> 6 & 63 | 128)
+ String.fromCharCode(code & 63 | 128);
}
return str;
}).join('');
@ -455,40 +455,44 @@
'¥€$'
@*/
Ox.decodeUTF8 = function(str) {
var bytes = Ox.map(str, function(v) {
return v.charCodeAt(0);
}),
var code,
i = 0,
len = str.length,
str = '';
ret = '';
while (i < len) {
if (bytes[i] <= 128) {
str += String.fromCharCode(bytes[i]);
code = Ox.range(3).map(function(o) {
return str.charCodeAt(i + o);
});
if (code[0] <= 128) {
ret += str[i];
i++;
} else if (
bytes[i] >= 192 && bytes[i] < 240 &&
i < len - (bytes[i] < 224 ? 1 : 2)
code[0] >= 192 && code[0] < 240
&& i < len - (code[0] < 224 ? 1 : 2)
) {
if (bytes[i + 1] >= 128 && bytes[i + 1] < 192) {
if (bytes[i] < 224) {
str += String.fromCharCode((bytes[i] & 31) << 6 |
bytes[i + 1] & 63);
if (code[1] >= 128 && code[1] < 192) {
if (code[0] < 224) {
ret += String.fromCharCode(
(code[0] & 31) << 6 | code[1] & 63
);
i += 2;
} else if (bytes[i + 2] >= 128 && bytes[i + 2] < 192) {
str += String.fromCharCode((bytes[i] & 15) << 12 |
(bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63);
} else if (code[2] >= 128 && code[2] < 192) {
ret += String.fromCharCode(
(code[0] & 15) << 12 | (code[1] & 63) << 6
| code[2] & 63
);
i += 3;
} else {
throwUTF8Error(bytes[i + 2], i + 2);
throwUTF8Error(code[2], i + 2);
}
} else {
throwUTF8Error(bytes[i + 1], i + 1);
throwUTF8Error(code[1], i + 1);
}
} else {
throwUTF8Error(bytes[i], i);
throwUTF8Error(code[0], i);
}
}
return str;
return ret;
};
})();