Merge pull request #17165 from calixteman/improve_removenullchars

Slightly improve the performance of removeNullCharacters
This commit is contained in:
calixteman 2023-10-25 15:01:32 +02:00 committed by GitHub
commit f27f2bb403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,19 +206,20 @@ function parseQueryString(query) {
return params; return params;
} }
const InvisibleCharactersRegExp = /[\x01-\x1F]/g; const InvisibleCharactersRegExp = /[\x00-\x1F]/g;
/** /**
* @param {string} str * @param {string} str
* @param {boolean} [replaceInvisible] * @param {boolean} [replaceInvisible]
*/ */
function removeNullCharacters(str, replaceInvisible = false) { function removeNullCharacters(str, replaceInvisible = false) {
if (typeof str !== "string") { if (!InvisibleCharactersRegExp.test(str)) {
console.error(`The argument must be a string.`);
return str; return str;
} }
if (replaceInvisible) { if (replaceInvisible) {
str = str.replaceAll(InvisibleCharactersRegExp, " "); return str.replaceAll(InvisibleCharactersRegExp, m => {
return m === "\x00" ? "" : " ";
});
} }
return str.replaceAll("\x00", ""); return str.replaceAll("\x00", "");
} }