You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
31 KiB
JavaScript
201 lines
31 KiB
JavaScript
8 months ago
|
/*!
|
||
|
* gbk.js v0.3.0
|
||
|
* Homepage https://github.com/cnwhy/GBK.js
|
||
|
* License MIT
|
||
|
*/
|
||
|
|
||
|
(function (global, factory) {
|
||
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||
|
typeof define === 'function' && define.amd ? define(factory) :
|
||
|
(global.GBK = factory());
|
||
|
}(this, (function () { 'use strict';
|
||
|
|
||
|
var GBK = function (gbk_us) {
|
||
|
var arr_index = 0x8140; //33088;
|
||
|
var gbk = {
|
||
|
decode: function (arr) {
|
||
|
var str = "";
|
||
|
for (var n = 0, max = arr.length; n < max; n++) {
|
||
|
var code = arr[n] & 0xff;
|
||
|
if (code > 0x80 && n + 1 < max) {
|
||
|
var code1 = arr[n + 1] & 0xff;
|
||
|
if(code1 >= 0x40){
|
||
|
code = gbk_us[(code << 8 | code1) - arr_index];
|
||
|
n++;
|
||
|
}
|
||
|
}
|
||
|
str += String.fromCharCode(code);
|
||
|
}
|
||
|
return str;
|
||
|
},
|
||
|
encode: function (str) {
|
||
|
str += '';
|
||
|
var gbk = [];
|
||
|
var wh = '?'.charCodeAt(0); //gbk中没有的字符的替换符
|
||
|
for (var i = 0; i < str.length; i++) {
|
||
|
var charcode = str.charCodeAt(i);
|
||
|
if (charcode < 0x80) gbk.push(charcode);
|
||
|
else {
|
||
|
var gcode = gbk_us.indexOf(charcode);
|
||
|
if (~gcode) {
|
||
|
gcode += arr_index;
|
||
|
gbk.push(0xFF & (gcode >> 8), 0xFF & gcode);
|
||
|
} else {
|
||
|
gbk.push(wh);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return gbk;
|
||
|
}
|
||
|
};
|
||
|
return gbk;
|
||
|
};
|
||
|
var gbk = GBK;
|
||
|
|
||
|
var URI = function(GBK){
|
||
|
var passChars = '!\'()*-._~';
|
||
|
var otherPassChars = '#$&+,/:;=?@';
|
||
|
function getModue(passChars){
|
||
|
var passBits = passChars.split('').sort();
|
||
|
var isPass = function (s){
|
||
|
return ~passChars.indexOf(s) || /[0-9a-zA-Z]/.test(s)
|
||
|
};
|
||
|
return {
|
||
|
encode:function(str){
|
||
|
return (str+'').replace(/./g,function(v){
|
||
|
if(isPass(v)) return v;
|
||
|
var bitArr = GBK.encode(v);
|
||
|
for(var i=0; i<bitArr.length; i++){
|
||
|
bitArr[i] = '%' + ('0'+bitArr[i].toString(16)).substr(-2).toUpperCase();
|
||
|
}
|
||
|
return bitArr.join('');
|
||
|
})
|
||
|
},
|
||
|
decode:function(enstr){
|
||
|
enstr = String(enstr);
|
||
|
var outStr = '';
|
||
|
for(var i=0; i<enstr.length; i++){
|
||
|
var char = enstr.charAt(i);
|
||
|
if(char === '%' && i + 2 < enstr.length){
|
||
|
var code1 = parseInt(enstr.substr(i+1,2),16);
|
||
|
if(!isNaN(code1)){
|
||
|
var _i = i + 2;
|
||
|
if(code1 > 0x80){
|
||
|
var code2;
|
||
|
if(enstr.charAt(_i+1) === '%'){
|
||
|
code2 = parseInt(enstr.substr(_i+2,2),16);
|
||
|
_i += 3;
|
||
|
}else{
|
||
|
code2 = enstr.charCodeAt(_i+1);
|
||
|
_i += 1;
|
||
|
}
|
||
|
if(code2 >= 0x40){
|
||
|
i = _i;
|
||
|
outStr += GBK.decode([code1,code2]);
|
||
|
continue;
|
||
|
}
|
||
|
}else{
|
||
|
i += 2;
|
||
|
outStr += String.fromCharCode(code1);
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
outStr += char;
|
||
|
}
|
||
|
return outStr;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var URIComponent = getModue(passChars);
|
||
|
var URI = getModue(passChars + otherPassChars);
|
||
|
|
||
|
return {
|
||
|
encodeURI:URI.encode,
|
||
|
decodeURI:URI.decode,
|
||
|
encodeURIComponent:URIComponent.encode,
|
||
|
decodeURIComponent:URIComponent.decode
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var src = function (gbk_us){
|
||
|
var gbk$$1 = gbk(gbk_us);
|
||
|
gbk$$1.URI = URI(gbk$$1);
|
||
|
return gbk$$1;
|
||
|
};
|
||
|
|
||
|
// 多进制转换后的数字还原函数 构建时会替换占位符
|
||
|
var Fn_Hex_decode = function decode(){
|
||
|
var n = 0, str = arguments[0];
|
||
|
for (var i = 0,w = str.length; i < w; i++) {
|
||
|
var code = str.charCodeAt(i);
|
||
|
if(code < 38 || code > 126) return NaN;
|
||
|
n += (code - 38) * Math.pow(89, w - i - 1);
|
||
|
}
|
||
|
return n;
|
||
|
};
|
||
|
|
||
|
// 解压Unicode编码字符串函数 构建时会替换占位符
|
||
|
var Fn_unzip = function unZip() {
|
||
|
return arguments[0].replace(/\x23(\d+)\x24/g, function (a, b) {
|
||
|
return Array(+b + 4).join("#");
|
||
|
})
|
||
|
.replace(/[\x26-\x7e]\x25[\x26-\x7e]/g,function(a){
|
||
|
var b = a.substr(0,1).charCodeAt(0)
|
||
|
,e = a.substr(2).charCodeAt(0)
|
||
|
,str = String.fromCharCode(b);
|
||
|
while(b++<e){
|
||
|
str += String.fromCharCode(b);
|
||
|
}
|
||
|
return str;
|
||
|
})
|
||
|
.replace(/\x23/g, "###")
|
||
|
.replace(/([\x26-\x7e]{2})\x21([\x26-\x7e\x23]+)(?:\x20|$)/g, function (all, hd, dt) {
|
||
|
return dt.replace(/./g, function (a) {
|
||
|
if (a != "#") {
|
||
|
return hd + a;
|
||
|
} else {
|
||
|
return a;
|
||
|
}
|
||
|
});
|
||
|
})
|
||
|
.match(/.../g);
|
||
|
};
|
||
|
|
||
|
function gbkArray(gbkArr) {
|
||
|
var data = [];
|
||
|
for (var i = 0x81, k = 0; i <= 0xfe; i++) {
|
||
|
if (data.length > 0) {
|
||
|
data.length += 0x40 + 1;
|
||
|
}
|
||
|
for (var j = 0x40; j <= 0xfe; j++) {
|
||
|
if (
|
||
|
(j == 0x7f) ||
|
||
|
((0xa1 <= i && i <= 0xa7) && j <= 0xa0) ||
|
||
|
((0xaa <= i && i <= 0xaf) && j >= 0xa1) ||
|
||
|
(0xf8 <= i && j >= 0xa1)
|
||
|
) {
|
||
|
data.push(undefined);
|
||
|
continue;
|
||
|
}
|
||
|
var hex = gbkArr[k++];
|
||
|
var key = Fn_Hex_decode(hex);
|
||
|
data.push(key ? key : undefined);
|
||
|
}
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
var GBK$1 = function () {
|
||
|
// 生成按GBk编码顺数排列的编码映射数组 构建时会替换 zipData 的占位符
|
||
|
var gbk_us = gbkArray(Fn_unzip("(T!HJ%LUX]e%gilotuwy{} (U!)-%/137>BDGHO%RTUW%\\_a%jl%rtw} (V!*+-0%27>C%EHJ%MP%R\\`cdfn%ptvz{} (W!()*,/3%579;=%CFGM%QWX\\^cdg%ilnprtvy%} (X!&')%.468CDHJLMOPSTWY%\\_b%dg%ilnprtuwxz%|~ (Y!'(*+-469%=?%GI%KO%RT%V[%_bdikmnptuy{}~ (Z!&')+%-/%;>@ACE%GKMNPR%TW[_%ikmo%rt%vy%{} ([!'(%+-%024%;=%BD%LO%QSTX%[]^`%ce%y{} (\\!()+%/1%7:%LN%SU%WY%cf%im%prt%xz%~ (]!&'%*-%/1%68%EG%cgloqs%uwx|%~ (^!')%-/02356;>FJKOPRSVWZ%]_`dfi%kmor%vyz (_!'+%-124%68;=@ACE%MOQRUVX%]_adegjqwx|~ (`!&)*-%/689;%=?%ADFIKLNOVX^%cehilmoq%uwyz|%~ (a!'),%/124%=?AD%HJ%PRSU%[]e%ho%qu%~ (b!()*,%.024%79;%=?A%FH%KM%WY%`c%ei%loq%tvy%| (c!'*+-.1346%8:%<>%GKLOQSUZ%\\_cghjltwy{| (d!(,/1%4679=>@D%JLMOQRTVWZ]`%ce%km%pr%tvy%} (e!+,-/0279%;>?DQW[%]_bdhqu%wy (f!&().47:;>ACEFHIKMP%SU^a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