缘起
JS的escape到底是怎么实现的?如:
1 2 3 4 5 6 |
escape(String.fromCharCode(0x41)) "A" escape(String.fromCharCode(129)) "%81" escape(String.fromCharCode(257)) "%u0101" |
哪些字符不做编码?哪些字符做 ‘%’ 编码?哪些字符做 ‘%u’ 编码?
参考资料:
Ecma: http://www.ecma-international.org/publications/standards/Ecma-262.htm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
1. Call ToString(string). 2. Compute the number of characters in Result(1). 3. Let R be the empty string. 4. Let k be 0. 5. If k equals Result(2), return R. 6. Get the character (represented as a 16-bit unsigned integer) at position k within Result(1). 7. If Result(6) is one of the 69 nonblank characters “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./” then go to step 13. 8. If Result(6), is less than 256, go to step 11. 9. Let S be a String containing six characters “%uwxyz” where wxyz are four hexadecimal digits encoding the value of Result(6). 10. Go to step 14. 11. Let S be a String containing three characters “%xy” where xy are two hexadecimal digits encoding the value of Result(6). 12. Go to step 14. 13. Let S be a String containing the single character Result(6). 14. Let R be a new String value computed by concatenating the previous value of R and S. 15. Increase k by 1. 16. Go to step 5. |
Google V8项目主页: https://code.google.com/p/v8/
Google V8源码下载: https://code.google.com/p/v8/wiki/Source 参看:uri.h中 URIEscape::Escape 的实现
Google V8开发者参考: https://developers.google.com/v8/build
关于JS做压缩:
http://www.poluoluo.com/jzxy/201004/83108.html
lz77
JS二进制操作: http://blog.chinaunix.net/uid-52437-id-3325514.html