[cmake-developers] [PATCH 1/3] Add SystemTools::TrimWhitespace function

Peter Collingbourne peter at pcc.me.uk
Fri Nov 11 21:36:05 EST 2011


---
 Source/kwsys/SystemTools.cxx    |   15 +++++++++++++++
 Source/kwsys/SystemTools.hxx.in |    5 +++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx
index 1bf19c0..c14eb38 100644
--- a/Source/kwsys/SystemTools.cxx
+++ b/Source/kwsys/SystemTools.cxx
@@ -1195,6 +1195,21 @@ kwsys_stl::string SystemTools::UpperCase(const kwsys_stl::string& s)
   return n;
 }
 
+// Returns a string that has whitespace removed from the start and the end.
+kwsys_stl::string SystemTools::TrimWhitespace(const kwsys_stl::string& s)
+{
+  kwsys_stl::string::const_iterator start = s.begin();
+  while(start != s.end() && *start == ' ')
+    ++start;
+  if (start == s.end())
+    return "";
+
+  kwsys_stl::string::const_iterator stop = s.end()-1;
+  while(*stop == ' ')
+    --stop;
+  return kwsys_stl::string(start, stop+1);
+}
+
 // Count char in string
 size_t SystemTools::CountChar(const char* str, char c)
 {
diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in
index 04f1978..c03b8bc 100644
--- a/Source/kwsys/SystemTools.hxx.in
+++ b/Source/kwsys/SystemTools.hxx.in
@@ -129,6 +129,11 @@ public:
   static kwsys_stl::string UpperCase(const kwsys_stl::string&);
   
   /**
+   * Returns a string that has whitespace removed from the start and the end.
+   */
+  static kwsys_stl::string TrimWhitespace(const kwsys_stl::string& s);
+
+  /**
    * Count char in string
    */
   static size_t CountChar(const char* str, char c);
-- 
1.7.5.3




More information about the cmake-developers mailing list