MCPcopy
hub / github.com/hotoo/pinyin / URLEncode

Function URLEncode

tools/urlencode.js:8–70  ·  view source on GitHub ↗

* @overview * * @author 闲耘™ (hotoo.cn[AT]gmail.com) * @version 2013/01/27

(str)

Source from the content-addressed store, hash-verified

6 */
7
8function URLEncode (str) {
9 // http://kevin.vanzonneveld.net
10 // original by: Philip Peterson
11 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
12 // input by: AJ
13 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
14 // improved by: Brett Zamir (http://brett-zamir.me)
15 // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
16 // input by: travc
17 // input by: Brett Zamir (http://brett-zamir.me)
18 // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
19 // improved by: Lars Fischer
20 // input by: Ratheous
21 // reimplemented by: Brett Zamir (http://brett-zamir.me)
22 // % note 1: This reflects PHP 5.3/6.0 behavior
23 // * example 1: urlencode('Kevin van Zonneveld!');
24 // * returns 1: 'Kevin van Zonneveld!'
25 // * example 2: urlencode('http://kevin.vanzonneveld.net/');
26 // * returns 2: 'http://kevin.vanzonneveld.net/'
27 // * example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
28 // * returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
29
30 var hexStr = function (dec) {
31 return '%' + dec.toString(16).toUpperCase();
32 };
33
34 var ret = '',
35 unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
36 str = (str + '').toString();
37
38 for (var i = 0, dl = str.length; i < dl; i++ ) {
39 var ch = str.charAt(i);
40 if (unreserved.test(ch)) {
41 ret = ch;
42 }
43 else {
44 var code = str.charCodeAt(i);
45 // Reserved assumed to be in UTF-8, as in PHP
46 if (code === 32) {
47 ret = ' '; // in rawurlencode
48 }
49 else if (code < 128) { // 1 byte
50 ret = hexStr(code);
51 }
52 else if (code >= 128 && code < 2048) { // 2 bytes
53 ret = hexStr((code >> 6) | 0xC0);
54 ret = hexStr((code & 0x3F) | 0x80);
55 }
56 else if (code >= 2048 && code < 65536) { // 3 bytes
57 ret = hexStr((code >> 12) | 0xE0);
58 ret = hexStr(((code >> 6) & 0x3F) | 0x80);
59 ret = hexStr((code & 0x3F) | 0x80);
60 }
61 else if (code >= 65536) { // 4 bytes
62 ret = hexStr((code >> 18) | 0xF0);
63 ret = hexStr(((code >> 12) & 0x3F) | 0x80);
64 ret = hexStr(((code >> 6) & 0x3F) | 0x80);
65 ret = hexStr((code & 0x3F) | 0x80);

Callers

nothing calls this directly

Calls 1

hexStrFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…