-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.js
63 lines (52 loc) · 1.41 KB
/
utf8.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// -------------------------------------------------
// ------------------ UTF8 Helpers -----------------
// -------------------------------------------------
"use strict";
var UTF8 = {};
/** @constructor */
function UTF8StreamToUnicode() {
this.stream = new Uint8Array(5);
this.ofs = 0;
this.Put = function(key) {
this.stream[this.ofs] = key;
this.ofs++;
switch(this.ofs) {
case 1:
if (this.stream[0] < 128) {
this.ofs = 0;
return this.stream[0];
}
break;
case 2:
if ((this.stream[0]&0xE0) == 0xC0)
if ((this.stream[1]&0xC0) == 0x80) {
this.ofs = 0;
return ((this.stream[0]&0x1F)<<6) | (this.stream[1]&0x3F);
}
break;
case 3:
break;
case 4:
break;
default:
return -1;
//this.ofs = 0;
//break;
}
return -1;
};
}
function UnicodeToUTF8Stream(key)
{
if (key < 0x80) return [key];
if (key < 0x800) return [0xC0|((key>>6)&0x1F), 0x80|(key&0x3F)];
}
UTF8.UTF8Length = function(s)
{
var length = 0;
for(var i=0; i<s.length; i++) {
var c = s.charCodeAt(i);
length += c<128?1:2;
}
return length;
};