MCPcopy Create free account
hub / github.com/43081j/picoquery / encodeString

Function encodeString

src/string-util.ts:27–92  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

25 * @returns {string}
26 */
27export function encodeString(str: string): string {
28 const len = str.length;
29 if (len === 0) {
30 return '';
31 }
32
33 let out = '';
34 let lastPos = 0;
35 let i = 0;
36
37 outer: for (; i < len; i++) {
38 let c = str.charCodeAt(i);
39
40 // ASCII
41 while (c < 0x80) {
42 if (noEscape[c] !== 1) {
43 if (lastPos < i) out += str.slice(lastPos, i);
44 lastPos = i + 1;
45 out += hexTable[c];
46 }
47
48 if (++i === len) break outer;
49
50 c = str.charCodeAt(i);
51 }
52
53 if (lastPos < i) out += str.slice(lastPos, i);
54
55 // Multi-byte characters ...
56 if (c < 0x800) {
57 lastPos = i + 1;
58 out += hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)];
59 continue;
60 }
61 if (c < 0xd800 || c >= 0xe000) {
62 lastPos = i + 1;
63 out +=
64 hexTable[0xe0 | (c >> 12)] +
65 hexTable[0x80 | ((c >> 6) & 0x3f)] +
66 hexTable[0x80 | (c & 0x3f)];
67 continue;
68 }
69 // Surrogate pair
70 ++i;
71
72 // This branch should never happen because all URLSearchParams entries
73 // should already be converted to USVString. But, included for
74 // completion's sake anyway.
75 if (i >= len) {
76 throw new Error('URI malformed');
77 }
78
79 const c2 = str.charCodeAt(i) & 0x3ff;
80
81 lastPos = i + 1;
82 c = 0x10000 + (((c & 0x3ff) << 10) | c2);
83 out +=
84 hexTable[0xf0 | (c >> 18)] +

Callers 3

stringifyObjectFunction · 0.85
defaultValueSerializerFunction · 0.85
stringify_test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…