]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - skins/htmldump/utf8.js
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / skins / htmldump / utf8.js
1 /**
2  * Obtained from http://homepage3.nifty.com/aokura/jscript/index.html
3  * The webpage says, among other things:
4  *    * ソースコードの全てあるいは一部を使用したことにより生じた損害に関しては一切責任を負いません。
5  *    * ソースコードの使用、配布に制限はありません。ご自由にお使いください。
6  *    * 動作チェックが不充分な場合もありますので、注意してください。
7  * 
8  * Which, loosely translated, means:
9  *    * The author takes no responsibility for damage which occurs due to the use of this code.
10  *    * There is no restriction on the use and distribution of the source code. Please use freely.
11  *    * Please be careful, testing may have been insufficient.
12  */
13
14
15 /**********************************************************************
16  *
17  *  Unicode ⇔ UTF-8
18  *
19  *  Copyright (c) 2005 AOK <soft@aokura.com>
20  *
21  **********************************************************************/
22
23 function _to_utf8(s) {
24   var c, d = "";
25   for (var i = 0; i < s.length; i++) {
26     c = s.charCodeAt(i);
27     if (c <= 0x7f) {
28       d += s.charAt(i);
29     } else if (c >= 0x80 && c <= 0x7ff) {
30       d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0);
31       d += String.fromCharCode((c & 0x3f) | 0x80);
32     } else {
33       d += String.fromCharCode((c >> 12) | 0xe0);
34       d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80);
35       d += String.fromCharCode((c & 0x3f) | 0x80);
36     }
37   }
38   return d;
39 }
40
41 function _from_utf8(s) {
42   var c, d = "", flag = 0, tmp;
43   for (var i = 0; i < s.length; i++) {
44     c = s.charCodeAt(i);
45     if (flag == 0) {
46       if ((c & 0xe0) == 0xe0) {
47         flag = 2;
48         tmp = (c & 0x0f) << 12;
49       } else if ((c & 0xc0) == 0xc0) {
50         flag = 1;
51         tmp = (c & 0x1f) << 6;
52       } else if ((c & 0x80) == 0) {
53         d += s.charAt(i);
54       } else {
55         flag = 0;
56       }
57     } else if (flag == 1) {
58       flag = 0;
59       d += String.fromCharCode(tmp | (c & 0x3f));
60     } else if (flag == 2) {
61       flag = 3;
62       tmp |= (c & 0x3f) << 6;
63     } else if (flag == 3) {
64       flag = 0;
65       d += String.fromCharCode(tmp | (c & 0x3f));
66     } else {
67       flag = 0;
68     }
69   }
70   return d;
71 }
72