如何利用js编码函数encodeURIComponent进行源码解析
程序员文章站
2022-04-10 16:12:32
...
function URIAddEncodedOctetToBuffer(octet, result, index) { result[index++] = 37; // Char code of '%'. result[index++] = hexCharCodeArray[octet >> 4]; result[index++] = hexCharCodeArray[octet & 0x0F]; return index; }function URIEncodeOctets(octets, result, index) { if (hexCharCodeArray === 0) { // 0-F hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]; } index = URIAddEncodedOctetToBuffer(octets[0], result, index); if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); return index; }// 对unicode中除了代理对外的字符编码function URIEncodeSingle(cc, result, index) { var x = (cc >> 12) & 0xF; var y = (cc >> 6) & 63; var z = cc & 63; // unicode最多值是三个字节 var octets = new InternalArray(3); // ascii码 if (cc <= 0x007F) { octets[0] = cc; } else if (cc <= 0x07FF) { octets[0] = y + 192; octets[1] = z + 128; } else { octets[0] = x + 224; octets[1] = y + 128; octets[2] = z + 128; } return URIEncodeOctets(octets, result, index); }function URIEncodePair(cc1 , cc2, result, index) { var u = ((cc1 >> 6) & 0xF) + 1; var w = (cc1 >> 2) & 0xF; var x = cc1 & 3; var y = (cc2 >> 6) & 0xF; var z = cc2 & 63; var octets = new InternalArray(4); octets[0] = (u >> 2) + 240; octets[1] = (((u & 3) << 4) | w) + 128; octets[2] = ((x << 4) | y) + 128; octets[3] = z + 128; return URIEncodeOctets(octets, result, index); }// ECMA-262, section 15.1.3function Encode(uri, unescape) { uri = TO_STRING(uri); var uriLength = uri.length; var array = new InternalArray(uriLength); var index = 0; for (var k = 0; k < uriLength; k++) { var cc1 = %_StringCharCodeAt(uri, k); if (unescape(cc1)) { array[index++] = cc1; } else { if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); // 非高代理项 if (cc1 < 0xD800 || cc1 > 0xDBFF) { index = URIEncodeSingle(cc1, array, index); } else { // 高代理项 k++; if (k == uriLength) throw MakeURIError(); var cc2 = %_StringCharCodeAt(uri, k); // 不是合法的低代理项 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw MakeURIError(); index = URIEncodePair(cc1, cc2, array, index); } } } var result = %NewString(array.length, NEW_ONE_BYTE_STRING); for (var i = 0; i < array.length; i++) { %_OneByteSeqStringSetChar(i, array[i], result); } return result; }// ECMA-262 - 15.1.3.4function URIEncodeComponent(component) { var unescapePredicate = function(cc) { if (isAlphaNumeric(cc)) return true; // ! if (cc == 33) return true; // '()* if (39 <= cc && cc <= 42) return true; // -. if (45 <= cc && cc <= 46) return true; // _ if (cc == 95) return true; // ~ if (cc == 126) return true; return false; }; return Encode(component, unescapePredicate); }
URIEncodeComponent实现过程为,直接调用Encode函数进行编码,首先利用unescapePredicate 函数跳过一些特殊的字符,这些字符不需要进行编码,具体看代码。基本工程为,逐个字符遍历,如果当前字符为低代理项,则报错(cc1 >= 0xDC00 && cc1 <= 0xDFFF);因为低代理项必须跟在高代理项后面。接着判断如果当前字节不是高代理项(cc1 < 0xD800 || cc1 > 0xDBFF),则直接调用URIEncodeSingle函数进行编码,如果当前字符编码为高代理项,则取下一位,如果下一位超过了字符串长度,则报错,如果下一位不是低代理项则报错,因为高代理项后面必须跟着低代理项,如果下一位是低代理项,则调用URIEncodePair函数对代理对进行编码。Encode函数主要是判断,字符的是不是不需要编码的字符,是不是代理对,是不是一般的字符三种情形。
URIEncodeSingle函数基本流程是对一个字符进行某种运算,最后调用URIEncodeOctets函数按字节进行编码。unicode码最大为三个字节,所以URIEncodeSingle函数里定义了数组长度为3。URIEncodePair函数基本流程为,对高低代理项进行某种运算,高低代理项一共是四个字节,然后调用URIEncodeOctets函数对四个字节进行编码。
以上就是如何利用js编码函数encodeURIComponent进行源码解析 的详细内容,更多请关注其它相关文章!