From e3a366a103ca699aaf57595c396ea205a50d2b16 Mon Sep 17 00:00:00 2001
From: gilcu3 <828241+gilcu3@users.noreply.github.com>
Date: Fri, 14 Sep 2018 10:19:24 -0400
Subject: [PATCH 07/23] Making remove_tags function more efficient (from
 quadratic to linear time worst case)

---
 src/misc.cc | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/src/misc.cc b/src/misc.cc
index db088c2..bc3fdb9 100644
--- a/src/misc.cc
+++ b/src/misc.cc
@@ -961,26 +961,37 @@ const xstring& shell_encode(const char *string,int len)
 void remove_tags(char *buf)
 {
    int len=strlen(buf);
-   for(;;)
+   int less = -1;
+   for(int i = 0; i < len;i++)
    {
-      char *less=strchr(buf,'<');
-      char *amp=strstr(buf,"&nbsp;");
-      if(!less && !amp)
-	 break;
-      if(amp && (!less || amp<less))
-      {
-	 amp[0]=' ';
-	 memmove(amp+1,amp+6,len-(amp+6-buf)+1);
-	 len-=amp+6-buf;
-	 buf=amp+1;
-	 continue;
+      if(strcmp(buf + i, "&nbsp;") == 0){
+        for(int j = 0; j < 6; j++)buf[i + j] = 0;
+        buf[i] = ' ';
+        i += 6;
+        i--;
+        continue;
       }
-      char *more=strchr(less+1,'>');
-      if(!more)
-	 break;
-      memmove(less,more+1,len-(more+1-buf)+1);
-      len-=more+1-buf;
-      buf=less;
+      if(buf[i] == '<'){
+        less = i;
+        continue;
+
+      }
+      if(buf[i] == '>'){
+        if(less != -1){
+          for(int j = less; j <= i; j++)buf[j] = 0;
+          less = -1;
+        }
+
+      }
+   }
+   int zero = 0;
+   for(int i = 0; i < len;i++)
+   {
+     while(zero < i && buf[zero] != 0)zero++;
+     if(buf[i] != 0 and zero != i){
+      buf[zero] = buf[i];
+      buf[i] = 0;
+     }
    }
 }
 void rtrim(char *s)
-- 
1.7.12.4

