;(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的更多相关文章

  1. C# 对JS编码/解码进行转换

    public static class Extension { #region [编码/解码统一转换] /// <summary> /// /// </summary> /// ...

  2. JS编码解码详解

    今天在整理 js编码解码方法时,在网上搜资料,发现一篇文章讲的不错,讲解的非常简单明了,于是乎就想转载过来,却发现无法转载到博客园,最后只能卑鄙的摘抄过来.js编码解码就是将一些对URL和数据库敏感的 ...

  3. ajax请求参数中含有特殊字符"#"的问题 (另附上js编码解码的几种方法)

    使用ajax向后台提交的时候 由于参数中含有#  默认会被截断 只保留#之前的字符  json格式的字符串则不会被请求到后台的action 可以使用encodeURIComponent在前台进行编码, ...

  4. 【转】JS编码解码、C#编码解码

    GB2312,指的是中文 UTF8,指的是国标,包含中文.英文. 但是通过JQuery.ajax的Get.Post,如果直接传递中文或者特殊字符的特使字符的时候,这个时候就会出现乱码现象. JS编码 ...

  5. JS编码,解码. asp.net(C#)对应解码,编码

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...

  6. JS编码解码

    一.定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要 ...

  7. php与js 编码解码交互

    javascript: var  fontcolorEncode=encodeURIComponent(fontcolor.value);  //编码 php: $fontcolordecode= u ...

  8. Js编码和Java后台解码

    1.java.将resultMsg 转为utf-8 (1) resultMsg = URLEncoder.encode(resultMsg, "utf-8"); (2) new S ...

  9. url编码解码-js编码、C#编码

    JS编码解码 函数一定义和用法encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 U ...

随机推荐

  1. 题解 P1601 【A+B Problem(高精)】

    P1601 A+B Problem(高精) 题目描述 高精度加法,x相当于a+b problem,b不用考虑负数. 输入输出格式 输入格式: 分两行输入a,b<=10^500 输出格式: 输出只 ...

  2. JAVA多线程-内存模型、三大特性、线程池

    一.线程的三大特性 原子性.可见性.有序性 1)原子性,即一个操作或者多个操作要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.原子性其实就是保证数据一致.线程安全一部分. 2)可见性,即 ...

  3. Linux之文本编辑器Vim

    一.什么是vim vi是一种模式编辑器.vi 是Unix世界里极为普遍的全屏幕文本编辑器,几乎可以说任何一台Unix机器都会提供这套软体,其他的文本编辑器则不一定会存在,但是目前我们使用比较多的是 v ...

  4. Windows服务器【由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作】问题调查

    今天测试反应了一个问题,说接口返回的速度变慢了,并且返回的数据也不对.然后就找到了我o(╥﹏╥)o. 第一个反应就是查日志,不查不要紧,一查吓一跳,整个服务器上所有的站点都报错了.异常信息如下: Sy ...

  5. C# 执行DOS命令和批处理

    在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便.今天就来说一下C#中如何调用外部程序,执行一些特殊任务. 这 ...

  6. Hbase 元数据一致性检查(转)

    最近在学习HBase先关的知识,顺便做一下笔记,以加深知识的了解和掌握. Hbase常用工具 文件检测修复工具 hbase hbck -help 常用选项: -details 显示所有region检查 ...

  7. java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。

    java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...

  8. 【转载】c++指针的指针和指针的引用

    https://www.cnblogs.com/li-peng/p/4116349.html

  9. [再寄小读者之数学篇](2014-06-22 不等式 [中国科学技术大学2011年高等数学B考研试题])

    证明不等式: $$\bex 1+x\ln\sex{x+\sqrt{1+x^2}}>\sqrt{1+x^2},\quad x>0. \eex$$ 证明: 令 $x=\tan t,\ 0< ...

  10. JAVA进阶8

    间歇性混吃等死,持续性踌躇满志系列-------------第8天 1.遍历输出HashSet中的全部元素 import java.util.HashSet; import java.util.Ite ...