Skip to content

Commit

Permalink
Merge pull request #17165 from calixteman/improve_removenullchars
Browse files Browse the repository at this point in the history
Slightly improve the performance of removeNullCharacters
  • Loading branch information
calixteman authored Oct 25, 2023
2 parents 5d3823a + 651057c commit f27f2bb
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,20 @@ function parseQueryString(query) {
return params;
}

const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
const InvisibleCharactersRegExp = /[\x00-\x1F]/g;

/**
* @param {string} str
* @param {boolean} [replaceInvisible]
*/
function removeNullCharacters(str, replaceInvisible = false) {
if (typeof str !== "string") {
console.error(`The argument must be a string.`);
if (!InvisibleCharactersRegExp.test(str)) {
return str;
}
if (replaceInvisible) {
str = str.replaceAll(InvisibleCharactersRegExp, " ");
return str.replaceAll(InvisibleCharactersRegExp, m => {
return m === "\x00" ? "" : " ";
});
}
return str.replaceAll("\x00", "");
}
Expand Down

0 comments on commit f27f2bb

Please sign in to comment.