js编码解码 punyCode
;(function(w) {
var PunycodeModule = function () {
function IdnMapping() {
this.utf16 = {
decode: function (input) {
var output = [], i = , len = input.length, value, extra;
while (i < len) {
value = input.charCodeAt(i++);
if ((value & 0xF800) === 0xD800) {
extra = input.charCodeAt(i++);
if (((value & 0xFC00) !== 0xD800) || ((extra & 0xFC00) !== 0xDC00)) {
throw new RangeError("UTF-16(decode): Illegal UTF-16 sequence");
}
value = ((value & 0x3FF) << ) + (extra & 0x3FF) + 0x10000;
}
output.push(value);
}
return output;
},
encode: function (input) {
var output = [], i = , len = input.length, value;
while (i < len) {
value = input[i++];
if ((value & 0xF800) === 0xD800) {
throw new RangeError("UTF-16(encode): Illegal UTF-16 value");
}
if (value > 0xFFFF) {
value -= 0x10000;
output.push(String.fromCharCode(((value >>> ) & 0x3FF) | 0xD800));
value = 0xDC00 | (value & 0x3FF);
}
output.push(String.fromCharCode(value));
}
return output.join("");
}
}
var initial_n = 0x80;
var initial_bias = ;
var delimiter = "\x2D";
var base = ;
var damp = ;
var tmin = ;
var tmax = ;
var skew = ;
var maxint = 0x7FFFFFFF;
function decode_digit(cp) {
return cp - < ? cp - : cp - < ? cp - : cp - < ? cp - : base;
}
function encode_digit(d, flag) {
return d + + * (d < ) - ((flag != ) << );
}
function adapt(delta, numpoints, firsttime) {
var k;
delta = firsttime ? Math.floor(delta / damp) : (delta >> );
delta += Math.floor(delta / numpoints);
for (k = ; delta > (((base - tmin) * tmax) >> ) ; k += base) {
delta = Math.floor(delta / (base - tmin));
}
return Math.floor(k + (base - tmin + ) * delta / (delta + skew));
}
function encode_basic(bcp, flag) {
bcp -= (bcp - < ) << ;
return bcp + ((!flag && (bcp - < )) << );
}
this.decode = function (input, preserveCase) {
// Dont use utf16
var output = [];
var case_flags = [];
var input_length = input.length;
var n, out, i, bias, basic, j, ic, oldi, w, k, digit, t, len;
// Initialize the state:
n = initial_n;
i = ;
bias = initial_bias;
// Handle the basic code points: Let basic be the number of input code
// points before the last delimiter, or 0 if there is none, then
// copy the first basic code points to the output.
basic = input.lastIndexOf(delimiter);
if (basic < ) basic = ;
for (j = ; j < basic; ++j) {
if (preserveCase) case_flags[output.length] = (input.charCodeAt(j) - < );
if (input.charCodeAt(j) >= 0x80) {
throw new RangeError("Illegal input >= 0x80");
}
output.push(input.charCodeAt(j));
}
// Main decoding loop: Start just after the last delimiter if any
// basic code points were copied; start at the beginning otherwise.
for (ic = basic > ? basic + : ; ic < input_length;) {
// ic is the index of the next character to be consumed,
// Decode a generalized variable-length integer into delta,
// which gets added to i. The overflow checking is easier
// if we increase i as we go, then subtract off its starting
// value at the end to obtain delta.
for (oldi = i, w = , k = base; ; k += base) {
if (ic >= input_length) {
throw RangeError("punycode_bad_input(1)");
}
digit = decode_digit(input.charCodeAt(ic++));
if (digit >= base) {
throw RangeError("punycode_bad_input(2)");
}
if (digit > Math.floor((maxint - i) / w)) {
throw RangeError("punycode_overflow(1)");
}
i += digit * w;
t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
if (digit < t) { break; }
if (w > Math.floor(maxint / (base - t))) {
throw RangeError("punycode_overflow(2)");
}
w *= (base - t);
}
out = output.length + ;
bias = adapt(i - oldi, out, oldi === );
// i was supposed to wrap around from out to 0,
// incrementing n each time, so we'll fix that now:
if (Math.floor(i / out) > maxint - n) {
throw RangeError("punycode_overflow(3)");
}
n += Math.floor(i / out);
i %= out;
// Insert n at position i of the output:
// Case of last character determines uppercase flag:
if (preserveCase) { case_flags.splice(i, , input.charCodeAt(ic - ) - < ); }
output.splice(i, , n);
i++;
}
if (preserveCase) {
for (i = , len = output.length; i < len; i++) {
if (case_flags[i]) {
output[i] = (String.fromCharCode(output[i]).toUpperCase()).charCodeAt();
}
}
}
return this.utf16.encode(output);
};
this.encode = function (input, preserveCase) {
//** Bias adaptation function **
var n, delta, h, b, bias, j, m, q, k, t, ijv, case_flags;
if (preserveCase) {
// Preserve case, step1 of 2: Get a list of the unaltered string
case_flags = this.utf16.decode(input);
}
// Converts the input in UTF-16 to Unicode
input = this.utf16.decode(input.toLowerCase());
var input_length = input.length; // Cache the length
if (preserveCase) {
// Preserve case, step2 of 2: Modify the list to true/false
for (j = ; j < input_length; j++) {
case_flags[j] = input[j] != case_flags[j];
}
}
var output = [];
// Initialize the state:
n = initial_n;
delta = ;
bias = initial_bias;
// Handle the basic code points:
for (j = ; j < input_length; ++j) {
if (input[j] < 0x80) {
output.push(
String.fromCharCode(
case_flags ? encode_basic(input[j], case_flags[j]) : input[j]
)
);
}
}
h = b = output.length;
// h is the number of code points that have been handled, b is the
// number of basic code points
if (b > ) output.push(delimiter);
// Main encoding loop:
//
while (h < input_length) {
// All non-basic code points < n have been
// handled already. Find the next larger one:
for (m = maxint, j = ; j < input_length; ++j) {
ijv = input[j];
if (ijv >= n && ijv < m) m = ijv;
}
// Increase delta enough to advance the decoder's
// <n,i> state to <m,0>, but guard against overflow:
if (m - n > Math.floor((maxint - delta) / (h + ))) {
throw RangeError("punycode_overflow (1)");
}
delta += (m - n) * (h + );
n = m;
for (j = ; j < input_length; ++j) {
ijv = input[j];
if (ijv < n) {
if (++delta > maxint) return Error("punycode_overflow(2)");
}
if (ijv == n) {
// Represent delta as a generalized variable-length integer:
for (q = delta, k = base; ; k += base) {
t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
if (q < t) break;
output.push(String.fromCharCode(encode_digit(t + (q - t) % (base - t), )));
q = Math.floor((q - t) / (base - t));
}
output.push(String.fromCharCode(encode_digit(q, preserveCase && case_flags[j] ? : )));
bias = adapt(delta, h + , h == b);
delta = ;
++h;
}
}
++delta, ++n;
}
return output.join("");
}
}
this.toASCII = function (domain) {
var idn = new IdnMapping();
var domainarray = domain.split(".");
var out = [];
for (var i = ; i < domainarray.length; ++i) {
var s = domainarray[i];
out.push(
s.match(/[^A-Za-z0--]/) ?
"xn--" + idn.encode(s) :
s
);
}
return out.join(".");
}
this.toUnicode = function (domain) {
var idn = new IdnMapping();
var domainarray = domain.split(".");
var out = [];
for (var i = ; i < domainarray.length; ++i) {
var s = domainarray[i];
out.push(
s.match(/^xn--/) ?
idn.decode(s.slice()) :
s
);
}
return out.join(".");
}
}
w.idnMapping = PunycodeModule;
})(window)
<script>
window.onload = function () {
var idn = new idnMapping();
var str = idn.toASCII("www.北京朝阳.com");
console.log(str); var str1 = idn.toUnicode(str);
console.log(str1);
}
</script>
转载 b̶i̶n̶g̶.̶
js编码解码 punyCode的更多相关文章
- C# 对JS编码/解码进行转换
public static class Extension { #region [编码/解码统一转换] /// <summary> /// /// </summary> /// ...
- JS编码解码详解
今天在整理 js编码解码方法时,在网上搜资料,发现一篇文章讲的不错,讲解的非常简单明了,于是乎就想转载过来,却发现无法转载到博客园,最后只能卑鄙的摘抄过来.js编码解码就是将一些对URL和数据库敏感的 ...
- ajax请求参数中含有特殊字符"#"的问题 (另附上js编码解码的几种方法)
使用ajax向后台提交的时候 由于参数中含有# 默认会被截断 只保留#之前的字符 json格式的字符串则不会被请求到后台的action 可以使用encodeURIComponent在前台进行编码, ...
- 【转】JS编码解码、C#编码解码
GB2312,指的是中文 UTF8,指的是国标,包含中文.英文. 但是通过JQuery.ajax的Get.Post,如果直接传递中文或者特殊字符的特使字符的时候,这个时候就会出现乱码现象. JS编码 ...
- JS编码,解码. asp.net(C#)对应解码,编码
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...
- JS编码解码
一.定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要 ...
- php与js 编码解码交互
javascript: var fontcolorEncode=encodeURIComponent(fontcolor.value); //编码 php: $fontcolordecode= u ...
- Js编码和Java后台解码
1.java.将resultMsg 转为utf-8 (1) resultMsg = URLEncoder.encode(resultMsg, "utf-8"); (2) new S ...
- url编码解码-js编码、C#编码
JS编码解码 函数一定义和用法encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 U ...
随机推荐
- python 条件分支与循环
一.if判断: 语法一: if 条件: # 条件成立时执行的子代码块 代码1 代码2 代码3 示例: sex='female' age=18 is_beautiful=True if sex == ' ...
- Elasticsearch6.x和Kibana6.x的安装
Elasticsearch6.x的安装(centos6.x下) Elasticsearch6.x目前需要至少jdk8的支持,关于如何安装jdk不在讲述.Oracle的推荐安装文档可以在Oracle的网 ...
- mpvue——小程序修改input的placehold样式
前言 官方地址 https://developers.weixin.qq.com/miniprogram/dev/component/input.html 用placeholder-class的时候没 ...
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...
- 使用Sublime Text 3进行Markdown编辑+实时预览
使用Sublime Text 3进行Markdown编辑+实时预览 安装软件包管理器 打开Sublime Text 3 同时按下 ctrl+` ,窗口底部出现一个小控制台 复制以下代码,粘贴到控制台的 ...
- jdk各个版本之间的差异
背景:求职过程中,这个问题反复被问到.如果答不上来,只能说明基本功不扎实,并不能说自己擅长java. 技术趣味史-Java 各个版本的特性 Java 5 2004 年 Sun 公司发布 J2SE5(没 ...
- springboot+mybatis+cucumber
import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucu ...
- Redis扩展机制
Redis扩展机制扫盲 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Redis的Avalibility解决方案有很多,比如Twemproxy,Codis, 一.Twempro ...
- CMDB资产管理系统开发【day27】:理解RESTful架构
理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(hig ...
- JN_0001:在微信朋友圈分享时长大于10s的视频
1,先在聊天窗口里发送视频. 2,长按视频点击”收藏“. 3,进入微信收藏管理页面,播放视频. 4,点击右上角三点按钮,选择“转存为笔记”. 5,于是在收藏页面中会生成一个新的收藏笔记链接,打开链接再 ...