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 ...
随机推荐
- rsync 远程拷贝
rsync -vzP win7.qcow2 agu@192.168.1.198:/tmp/
- 洛谷 P1088 火星人
https://www.luogu.org/problemnew/show/P1088 这个题一开始是很蒙的 感觉很麻烦,每次都要交换balabala..... 后来才知道有这么一个神奇的stl 真是 ...
- Django+Vue打造购物网站(五)
注册和登陆 drf的认证 http://www.django-rest-framework.org/api-guide/authentication/ settings.py文件的配置 INSTALL ...
- Codeforce Round #554 Div.2 C - Neko does Maths
数论 gcd 看到这个题其实知道应该是和(a+k)(b+k)/gcd(a+k,b+k)有关,但是之后推了半天,思路全无. 然而..有一个引理: gcd(a, b) = gcd(a, b - a) = ...
- Magento2自定义命令
命令命名准则 命名指南概述 Magento 2引入了一个新的命令行界面(CLI),使组件开发人员能够插入模块提供的命令. Command name Command name 在命令中,它紧跟在命令的名 ...
- BZOJ2870 最长道路
题意:给定树,有点权.求一条路径使得最小点权 * 总点数最大.只需输出这个最大值.5w. 解:树上路径问题,点分治. 考虑合并两个子树的时候,答案的形式是val1 * (d1 + d2),当1是新插入 ...
- Day048--jQuery自定义动画和DOM操作
内容回顾 BOM location.reload() 全局刷新页面 location.href location.hash location.pathname location.hostname lo ...
- 深入理解pthread_cond_wait、pthread_cond_signal
===============================man pthread_cond_wait的解释========================== LINUX环境下多线程编程肯定会遇到 ...
- Pandas系列(五)-分类数据处理
内容目录 1. 创建对象 2. 常用操作 3. 内存使用量的陷阱 一.创建对象 1.基本概念:分类数据直白来说就是取值为有限的,或者说是固定数量的可能值.例如:性别.血型. 2.创建分类数据:这里以血 ...
- Pandas系列(九)-分组聚合详解
目录 1. 将对象分割成组 1.1 关闭排序 1.2 选择列 1.3 遍历分组 1.4 选择一个组 2. 聚合 2.1 一次应用多个聚合操作 2.2 对DataFrame列应用不同的聚合操作 3. t ...