lib: move encodeStr function to internal for reusable

PR-URL: https://github.com/nodejs/node/pull/24242
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
ZYSzys authored and Rod Vagg committed 2018-11-28 11:25:30 +11:00
1 parent 91494bf023
commit daeb34809a
3 files changed
+70 -117

No files matched your search

+2 -53
View File
@@ -24,8 +24,8 @@
'use strict';
const { Buffer } = require('buffer');
const { ERR_INVALID_URI } = require('internal/errors').codes;
const {
encodeStr,
hexTable,
isHexTable
} = require('internal/querystring');
@@ -140,59 +140,8 @@ function qsEscape(str) {
else
str += '';
}
var out = '';
var lastPos = 0;
for (var i = 0; i < str.length; ++i) {
var c = str.charCodeAt(i);
// ASCII
if (c < 0x80) {
if (noEscape[c] === 1)
continue;
if (lastPos < i)
out += str.slice(lastPos, i);
lastPos = i + 1;
out += hexTable[c];
continue;
}
if (lastPos < i)
out += str.slice(lastPos, i);
// Multi-byte characters ...
if (c < 0x800) {
lastPos = i + 1;
out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)];
continue;
}
if (c < 0xD800 || c >= 0xE000) {
lastPos = i + 1;
out += hexTable[0xE0 | (c >> 12)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
continue;
}
// Surrogate pair
++i;
if (i >= str.length)
throw new ERR_INVALID_URI();
var c2 = str.charCodeAt(i) & 0x3FF;
lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += hexTable[0xF0 | (c >> 18)] +
hexTable[0x80 | ((c >> 12) & 0x3F)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
hexTable[0x80 | (c & 0x3F)];
}
if (lastPos === 0)
return str;
if (lastPos < str.length)
return out + str.slice(lastPos);
return out;
return encodeStr(str, noEscape, hexTable);
}
function stringifyPrimitive(v) {