<SPAN style="COLOR: green">/* UTF-8 <=> UTF-16 convertion library.<BR> *<BR>/* Copyright (C) 1999 Masanao Izumo <<A href="mailto:iz@onicos.co.jp"><FONT color=#0000ff>iz@onicos.co.jp</FONT></A>><BR> * 2007 Ma Bingyao <<A href="mailto:andot@ujn.edu.cn"><FONT color=#0000ff>andot@ujn.edu.cn</FONT></A>><BR> * Version: 2.1<BR> * LastModified: Feb 25, 2007<BR> * This library is free. You can redistribute it and/or modify it.<BR> */</SPAN><P><BR></P><SPAN style="COLOR: green">/*<BR> * Interfaces:<BR> * utf8 = utf16to8(utf16);<BR> * utf16 = utf16to8(utf8);<BR> */</SPAN><P><BR></P><SPAN style="COLOR: blue">function</SPAN> utf16to8(str) {<BR> <SPAN style="COLOR: blue">if</SPAN> (str.match(<SPAN style="COLOR: maroon">/^[\x00-\x7f]*$/</SPAN>) != <SPAN style="COLOR: blue">null</SPAN>) {<BR> <SPAN style="COLOR: blue">return</SPAN> str;<BR> }<BR> <SPAN style="COLOR: blue">var</SPAN> out, i, j, len, c, c2;<BR> out = [];<BR> len = str.length;<BR> <SPAN style="COLOR: blue">for</SPAN> (i = <SPAN style="COLOR: #f00">0</SPAN>, j = <SPAN style="COLOR: #f00">0</SPAN>; i < len; i++, j++) {<BR> c = str.charCodeAt(i);<BR> <SPAN style="COLOR: blue">if</SPAN> (c <= 0x7f) {<BR> out[j] = str.charAt(i);<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> <SPAN style="COLOR: blue">if</SPAN> (c <= 0x7ff) {<BR> out[j] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(0xc0 | (c >>> <SPAN style="COLOR: #f00">6</SPAN>),<BR> 0x80 | (c & 0x3f));<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> <SPAN style="COLOR: blue">if</SPAN> (c < 0xd800 <SPAN style="COLOR: blue">||</SPAN> c > 0xdfff) {<BR> out[j] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(0xe0 | (c >>> <SPAN style="COLOR: #f00">12</SPAN>),<BR> 0x80 | ((c >>> <SPAN style="COLOR: #f00">6</SPAN>) & 0x3f),<BR> 0x80 | (c & 0x3f));<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> {<BR> <SPAN style="COLOR: blue">if</SPAN> (++i < len) {<BR> c2 = str.charCodeAt(i);<BR> <SPAN style="COLOR: blue">if</SPAN> (c <= 0xdbff <SPAN style="COLOR: blue">&&</SPAN> 0xdc00 <= c2 <SPAN style="COLOR: blue">&&</SPAN> c2 <= 0xdfff) {<BR> c = ((c & 0x03ff) << <SPAN style="COLOR: #f00">10</SPAN> | (c2 & 0x03ff)) + 0x010000;<BR> <SPAN style="COLOR: blue">if</SPAN> (0x010000 <= c <SPAN style="COLOR: blue">&&</SPAN> c <= 0x10ffff) {<BR> out[j] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(0xf0 | ((c >>> <SPAN style="COLOR: #f00">18</SPAN>) & 0x3f),<BR> 0x80 | ((c >>> <SPAN style="COLOR: #f00">12</SPAN>) & 0x3f),<BR> 0x80 | ((c >>> <SPAN style="COLOR: #f00">6</SPAN>) & 0x3f),<BR> 0x80 | (c & 0x3f));<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> {<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> }<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> {<BR> i--;<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> }<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> {<BR> i--;<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> }<BR> }<BR> }<BR> <SPAN style="COLOR: blue">return</SPAN> out.join(<SPAN style="COLOR: #f0f">''</SPAN>);<BR>}<P><BR></P><SPAN style="COLOR: blue">function</SPAN> utf8to16(str) {<BR> <SPAN style="COLOR: blue">if</SPAN> ((str.match(<SPAN style="COLOR: maroon">/^[\x00-\x7f]*$/</SPAN>) != <SPAN style="COLOR: blue">null</SPAN>) <SPAN style="COLOR: blue">||</SPAN><BR> (str.match(<SPAN style="COLOR: maroon">/^[\x00-\xff]*$/</SPAN>) == <SPAN style="COLOR: blue">null</SPAN>)) {<BR> <SPAN style="COLOR: blue">return</SPAN> str;<BR> }<BR> <SPAN style="COLOR: blue">var</SPAN> out, i, j, len, c, c2, c3, c4, s;<P><BR></P> out = [];<BR> len = str.length;<BR> i = j = <SPAN style="COLOR: #f00">0</SPAN>;<BR> <SPAN style="COLOR: blue">while</SPAN> (i < len) {<BR> c = str.charCodeAt(i++);<BR> <SPAN style="COLOR: blue">switch</SPAN> (c >> <SPAN style="COLOR: #f00">4</SPAN>) { <BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">0</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">1</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">2</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">3</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">4</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">5</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">6</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">7</SPAN>:<BR> <SPAN style="COLOR: green">// 0xxx xxxx</SPAN><BR>out[j++] = str.charAt(i - <SPAN style="COLOR: #f00">1</SPAN>);<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">12</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">13</SPAN>:<BR> <SPAN style="COLOR: green">// 110x xxxx 10xx xxxx</SPAN><BR>c2 = str.charCodeAt(i++);<BR> out[j++] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(((c & 0x1f) << <SPAN style="COLOR: #f00">6</SPAN>) |<BR> (c2 & 0x3f));<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">14</SPAN>:<BR> <SPAN style="COLOR: green">// 1110 xxxx 10xx xxxx 10xx xxxx</SPAN><BR>c2 = str.charCodeAt(i++);<BR> c3 = str.charCodeAt(i++);<BR> out[j++] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(((c & 0x0f) << <SPAN style="COLOR: #f00">12</SPAN>) |<BR> ((c2 & 0x3f) << <SPAN style="COLOR: #f00">6</SPAN>) |<BR> (c3 & 0x3f));<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">15</SPAN>:<BR> <SPAN style="COLOR: blue">switch</SPAN> (c & 0xf) {<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">0</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">1</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">2</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">3</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">4</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">5</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">6</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">7</SPAN>:<BR> <SPAN style="COLOR: green">// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx</SPAN><BR>c2 = str.charCodeAt(i++);<BR> c3 = str.charCodeAt(i++);<BR> c4 = str.charCodeAt(i++);<BR> s = ((c & 0x07) << <SPAN style="COLOR: #f00">18</SPAN>) |<BR> ((c2 & 0x3f) << <SPAN style="COLOR: #f00">12</SPAN>) |<BR> ((c3 & 0x3f) << <SPAN style="COLOR: #f00">6</SPAN>) |<BR> (c4 & 0x3f) - 0x10000;<BR> <SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: #f00">0</SPAN> <= s <SPAN style="COLOR: blue">&&</SPAN> s <= 0xfffff) {<BR> out[j] = <SPAN style="COLOR: #f00">String</SPAN>.fromCharCode(((s >>> <SPAN style="COLOR: #f00">10</SPAN>) & 0x03ff) | 0xd800,<BR> (s & 0x03ff) | 0xdc00);<BR> }<BR> <SPAN style="COLOR: blue">else</SPAN> {<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> }<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">8</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">9</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">10</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">11</SPAN>:<BR> <SPAN style="COLOR: green">// 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx</SPAN><BR>i+=<SPAN style="COLOR: #f00">4</SPAN>;<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">12</SPAN>: <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #f00">13</SPAN>:<BR> <SPAN style="COLOR: green">// 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx</SPAN><BR>i+=<SPAN style="COLOR: #f00">5</SPAN>;<BR> out[j] = <SPAN style="COLOR: #f0f">'?'</SPAN>;<BR> <SPAN style="COLOR: blue">break</SPAN>;<BR> }<BR> }<BR> j++;<BR> }<BR> <SPAN style="COLOR: blue">return</SPAN> out.join(<SPAN style="COLOR: #f0f">''</SPAN>);<BR>}<BR>
Copy after login