Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / stringutil.cpp
index 638d10b7109757828971968da8567db2eb7154ec..5b2c725017a6e585b14e9d6d3832f76a58571d8a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2011,2012,2013, by the GROMACS development team, led by
+ * Copyright (c) 2011,2012,2013,2014, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
  * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_utility
  */
+#include "gmxpre.h"
+
 #include "stringutil.h"
 
 #include <cctype>
-#include <cstdio>
 #include <cstdarg>
+#include <cstdio>
 #include <cstring>
 
 #include <algorithm>
@@ -79,6 +81,21 @@ std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
     return str;
 }
 
+std::string stripString(const std::string &str)
+{
+    std::string::const_iterator start = str.begin();
+    std::string::const_iterator end   = str.end();
+    while (start != end && std::isspace(*start))
+    {
+        ++start;
+    }
+    while (start != end && std::isspace(*(end - 1)))
+    {
+        --end;
+    }
+    return std::string(start, end);
+}
+
 std::string formatString(const char *fmt, ...)
 {
     va_list           ap;
@@ -112,6 +129,30 @@ std::string formatString(const char *fmt, ...)
     }
 }
 
+std::vector<std::string> splitString(const std::string &str)
+{
+    std::vector<std::string>          result;
+    std::string::const_iterator       currPos = str.begin();
+    const std::string::const_iterator end     = str.end();
+    while (currPos != end)
+    {
+        while (currPos != end && std::isspace(*currPos))
+        {
+            ++currPos;
+        }
+        const std::string::const_iterator startPos = currPos;
+        while (currPos != end && !std::isspace(*currPos))
+        {
+            ++currPos;
+        }
+        if (startPos != end)
+        {
+            result.push_back(std::string(startPos, currPos));
+        }
+    }
+    return result;
+}
+
 std::string concatenateStrings(const char *const *sarray, size_t count)
 {
     std::string result;
@@ -135,6 +176,18 @@ std::string concatenateStrings(const char *const *sarray, size_t count)
 namespace
 {
 
+/*! \brief
+ * Helper function to identify word boundaries for replaceAllWords().
+ *
+ * \returns  `true` if the character is considered part of a word.
+ *
+ * \ingroup module_utility
+ */
+bool isWordChar(char c)
+{
+    return std::isalnum(c) || c == '-' || c == '_';
+}
+
 /*! \brief
  * Common implementation for string replacement functions.
  *
@@ -162,8 +215,8 @@ replaceInternal(const std::string &input, const char *from, const char *to,
         size_t matchEnd = matchPos + matchLength;
         if (bWholeWords)
         {
-            if (!((matchPos == 0 || !std::isalnum(input[matchPos-1]))
-                  && (matchEnd == input.length() || !std::isalnum(input[matchEnd]))))
+            if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
+                  && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
             {
                 matchPos = input.find(from, matchPos + 1);
                 continue;