From bab5b1f28f8fe4d30b4ecb3f29a839a97502dcb6 Mon Sep 17 00:00:00 2001 From: Ben Boeckel <ben.boeckel@kitware.com> Date: Thu, 31 Jul 2014 13:14:33 -0400 Subject: [PATCH] SystemTools: add string overload for ReplaceString Change-Id: I5f0adcf5dc6013b7f643c99ec233ac76c622c4ac --- SystemTools.cxx | 37 +++++++++++++++++++++++++++++-------- SystemTools.hxx.in | 11 +++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/SystemTools.cxx b/SystemTools.cxx index e9af15c..c0c5baf 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -701,8 +701,35 @@ bool SystemTools::MakeDirectory(const kwsys_stl::string& path) // replace replace with with as many times as it shows up in source. // write the result into source. void SystemTools::ReplaceString(kwsys_stl::string& source, - const char* replace, - const char* with) + const kwsys_stl::string& replace, + const kwsys_stl::string& with) +{ + // do while hangs if replaceSize is 0 + if (replace.empty()) + { + return; + } + + SystemTools::ReplaceString(source, replace.c_str(), replace.size(), with); +} + +void SystemTools::ReplaceString(kwsys_stl::string& source, + const char* replace, + const char* with) +{ + // do while hangs if replaceSize is 0 + if (!*replace) + { + return; + } + + SystemTools::ReplaceString(source, replace, strlen(replace), with ? with : ""); +} + +void SystemTools::ReplaceString(kwsys_stl::string& source, + const char* replace, + size_t replaceSize, + const kwsys_stl::string& with) { const char *src = source.c_str(); char *searchPos = const_cast<char *>(strstr(src,replace)); @@ -714,12 +741,6 @@ void SystemTools::ReplaceString(kwsys_stl::string& source, } // perform replacements until done - size_t replaceSize = strlen(replace); - // do while hangs if replaceSize is 0 - if(replaceSize == 0) - { - return; - } char *orig = strdup(src); char *currentPos = orig; searchPos = searchPos - src + orig; diff --git a/SystemTools.hxx.in b/SystemTools.hxx.in index 338009e..2e45fa1 100644 --- a/SystemTools.hxx.in +++ b/SystemTools.hxx.in @@ -102,6 +102,9 @@ public: static void ReplaceString(kwsys_stl::string& source, const char* replace, const char* with); + static void ReplaceString(kwsys_stl::string& source, + const kwsys_stl::string& replace, + const kwsys_stl::string& with); /** * Return a capitalized string (i.e the first letter is uppercased, @@ -907,6 +910,14 @@ private: return &SystemToolsManagerInstance; } + /** + * Actual implementation of ReplaceString. + */ + static void ReplaceString(kwsys_stl::string& source, + const char* replace, + size_t replaceSize, + const kwsys_stl::string& with); + /** * Actual implementation of FileIsFullPath. */ -- GitLab