From 651057c039df95e81338f860dab40565161ea887 Mon Sep 17 00:00:00 2001
From: Calixte Denizet <calixte.denizet@gmail.com>
Date: Tue, 24 Oct 2023 23:10:08 +0200
Subject: [PATCH] Slightly improve the performance of removeNullCharacters

Most of the strings shouldn't contain special chars (<= 0x1F) so we can
have a fast path which just checks if the string contains at least one such
a char.
---
 web/ui_utils.js | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/web/ui_utils.js b/web/ui_utils.js
index 0aebb5a40..a2d58993e 100644
--- a/web/ui_utils.js
+++ b/web/ui_utils.js
@@ -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", "");
 }