/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2011,2012,2013, 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>
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;
}
}
+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;
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.
*
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;
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