Maison > interface Web > js tutoriel > le corps du texte

lib.utf.js_lib_js

WBOY
Libérer: 2016-05-16 19:09:47
original
1217 Les gens l'ont consulté
<span style="COLOR: green">/* UTF-8  UTF-16 convertion library.<br> *<br>/* Copyright (C) 1999 Masanao Izumo <font color="#0000ff">iz@onicos.co.jp</font>><br> * 2007 Ma Bingyao <font color="#0000ff">andot@ujn.edu.cn</font>><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         c = str.charCodeAt(i);<br>        <span style="COLOR: blue">if</span> (c             out[j] = str.charAt(i);<br>        }<br>        <span style="COLOR: blue">else</span> <span style="COLOR: blue">if</span> (c             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 || 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                 c2 = str.charCodeAt(i);<br>                <span style="COLOR: blue">if</span> (c && 0xdc00 && c2                  c = ((c & 0x03ff) 10 | (c2 & 0x03ff)) + 0x010000;<br>                 <span style="COLOR: blue">if</span> (0x010000 && c                  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         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) 6) |<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) 12) |<br>                 ((c2 & 0x3f) 6) |<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) 18) |<br>                 ((c2 & 0x3f) 12) |<br>                 ((c3 & 0x3f) 6) |<br>                 (c4 & 0x3f) - 0x10000;<br>                <span style="COLOR: blue">if</span> (<span style="COLOR: #f00">0</span> && s                  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>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal