From b4cf2669a2a71f3d34e36ca036b9af596f5b782e Mon Sep 17 00:00:00 2001
From: Ken Martin <ken.martin@kitware.com>
Date: Tue, 24 Jun 2003 15:23:34 -0400
Subject: [PATCH] performance improvements

---
 SystemTools.cxx | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/SystemTools.cxx b/SystemTools.cxx
index 12fabb4..706ab48 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -252,23 +252,37 @@ void SystemTools::ReplaceString(kwsys_std::string& source,
                                    const char* replace,
                                    const char* with)
 {
+  const char *src = source.c_str();
+  char *searchPos = strstr(src,replace);
+  
   // get out quick if string is not found
-  kwsys_std::string::size_type start = source.find(replace);
-  if(start ==  kwsys_std::string::npos)
+  if (!searchPos)
     {
     return;
     }
-  kwsys_std::string rest;
-  kwsys_std::string::size_type lengthReplace = strlen(replace);
-  kwsys_std::string::size_type lengthWith = strlen(with);
-  while(start != kwsys_std::string::npos)
-    {
-    rest = source.substr(start+lengthReplace);
-    source = source.substr(0, start);
+
+  // perform replacements until done
+  size_t replaceSize = strlen(replace);
+  char *orig = strdup(src);
+  char *currentPos = orig;
+  searchPos = searchPos - src + orig;
+  
+  // initialize the result
+  source.clear();
+  do
+    {
+    *searchPos = '\0';
+    source += currentPos;
+    currentPos = searchPos + replaceSize;
+    // replace
     source += with;
-    source += rest;
-    start = source.find(replace, start + lengthWith );
+    searchPos = strstr(currentPos,replace);
     }
+  while (searchPos);
+
+  // copy any trailing text
+  source += currentPos;
+  free(orig);
 }
 
 // Read a registry value.
-- 
GitLab