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 ...
随机推荐
- Shell命令-文件及目录操作之file、md5sum
文件及目录操作 - file.md5sum 1. file:显示文件的类型 file命令的功能说明 用于辨识文件类型.通过 file 指令,我们得以辨识该文件的类型. file命令的语法格式 file ...
- [BZOJ 2242] [SDOI 2011] 计算器
Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...
- Codeforces Global Round 2
A:答案一定包含首位或末位. #include<iostream> #include<cstdio> #include<cmath> #include<cst ...
- LOJ #2135. 「ZJOI2015」幻想乡战略游戏(点分树)
题意 给你一颗 \(n\) 个点的树,每个点的度数不超过 \(20\) ,有 \(q\) 次修改点权的操作. 需要动态维护带权重心,也就是找到一个点 \(v\) 使得 \(\displaystyle ...
- 初识 go 语言:方法,接口及并发
目录 方法,接口及并发 方法 接口 并发 信道 结束语 前言: go语言的第四篇文章,主要讲述go语言中的方法,包括指针,结构体,数组,切片,映射,函数闭包等,每个都提供了示例,可直接运行. 方法,接 ...
- POJ 3186 Treats for the Cows (动态规划)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- JS学习笔记Day9
一.BOM (一)概念:是 Browser object model 的缩写,简称浏览器对象模型. BOM 提供了独立于内容而与浏览器窗口进行交互的对象 由于 BOM 主要用于管理窗口与窗口之间的通讯 ...
- LFYZ-OJ ID: 1024 火车站
火车过站 问题描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从 ...
- Ubuntu 开机自动挂载硬盘
1.查看Linux硬盘信息: $ sudo fdisk -l 2.格式化硬盘(根据需要确定文件系统): sudo mkfs.xfs /dev/sdb 3.创建/data目录 sudo mkdir /d ...
- python GUI 之 tkinter
写一个 登陆窗口来学习 tkinter ,还剩下一些问题 代码暂时如下 import tkinter as tk import webbrowser import pickle from tkinte ...