• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1function dec2char(n) {
2	// converts a decimal number to a Unicode character
3	// n: the dec codepoint value to be converted
4	if (n <= 0xffff) {
5		out = String.fromCharCode(n);
6	} else if (n <= 0x10ffff) {
7		n -= 0x10000;
8		out =
9			String.fromCharCode(0xd800 | (n >> 10)) +
10			String.fromCharCode(0xdc00 | (n & 0x3ff));
11	} else out = "dec2char error: Code point out of range: " + n;
12	return out;
13}
14
15function big5Decoder(stream) {
16	stream = stream.replace(/%/g, " ");
17	stream = stream.replace(/[\s]+/g, " ").trim();
18	var bytes = stream.split(" ");
19	for (var i = 0; i < bytes.length; i++) bytes[i] = parseInt(bytes[i], 16);
20	var out = "";
21	var lead, byte, offset, ptr, cp;
22	var big5lead = 0x00;
23	var endofstream = 2000000;
24	var finished = false;
25
26	while (!finished) {
27		if (bytes.length == 0) byte = endofstream;
28		else byte = bytes.shift();
29
30		if (byte == endofstream && big5lead != 0x00) {
31			big5lead = 0x00;
32			out += "�";
33			continue;
34		}
35		if (byte == endofstream && big5lead == 0x00) {
36			finished = true;
37			continue;
38		}
39
40		if (big5lead != 0x00) {
41			lead = big5lead;
42			ptr = null;
43			big5lead = 0x00;
44			if (byte < 0x7f) offset = 0x40;
45			else offset = 0x62;
46			if ((byte >= 0x40 && byte <= 0x7e) || (byte >= 0xa1 && byte <= 0xfe))
47				ptr = (lead - 0x81) * 157 + (byte - offset);
48			// "If there is a row in the table below whose first column is pointer, return the two code points listed in its second column"
49			switch (ptr) {
50				case 1133:
51					out += "Ê̄";
52					continue;
53				case 1135:
54					out += "Ê̌";
55					continue;
56				case 1164:
57					out += "ê̄";
58					continue;
59				case 1166:
60					out += "ê̌";
61					continue;
62			}
63			if (ptr == null) cp = null;
64			else cp = big5[ptr];
65			if (cp == null && byte >= 0x00 && byte <= 0x7f) {
66				bytes.unshift(byte);
67			}
68			if (cp == null) {
69				out += "�";
70				continue;
71			}
72			out += dec2char(cp);
73			continue;
74		}
75		if (byte >= 0x00 && byte <= 0x7f) {
76			out += dec2char(byte);
77			continue;
78		}
79		if (byte >= 0x81 && byte <= 0xfe) {
80			big5lead = byte;
81			continue;
82		}
83		out += "�";
84	}
85	return out;
86}
87