Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / stringutil.cpp
index 0e5e377f0671a0e7d950e590944caba7f1be7b71..5b2c725017a6e585b14e9d6d3832f76a58571d8a 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2011,2012, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * 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.
  *
  * GROMACS is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * \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.
  *
@@ -144,6 +197,8 @@ namespace
  * \param[in] bWholeWords  Whether to only consider matches to whole words.
  * \returns   \p input with all occurrences of \p from replaced with \p to.
  * \throws    std::bad_alloc if out of memory.
+ *
+ * \ingroup module_utility
  */
 std::string
 replaceInternal(const std::string &input, const char *from, const char *to,
@@ -160,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;
@@ -185,12 +240,26 @@ replaceAll(const std::string &input, const char *from, const char *to)
     return replaceInternal(input, from, to, false);
 }
 
+std::string
+replaceAll(const std::string &input, const std::string &from,
+           const std::string &to)
+{
+    return replaceInternal(input, from.c_str(), to.c_str(), false);
+}
+
 std::string
 replaceAllWords(const std::string &input, const char *from, const char *to)
 {
     return replaceInternal(input, from, to, true);
 }
 
+std::string
+replaceAllWords(const std::string &input, const std::string &from,
+                const std::string &to)
+{
+    return replaceInternal(input, from.c_str(), to.c_str(), true);
+}
+
 
 /********************************************************************
  * TextLineWrapperSettings