2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions and classes in stringutil.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
42 #include "stringutil.h"
53 #include "gromacs/utility/gmxassert.h"
58 bool endsWith(const std::string &str, const char *suffix)
60 if (suffix == NULL || suffix[0] == '\0')
64 size_t length = std::strlen(suffix);
65 return (str.length() >= length
66 && str.compare(str.length() - length, length, suffix) == 0);
69 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
73 size_t suffixLength = std::strlen(suffix);
74 if (suffixLength > 0 && endsWith(str, suffix))
76 return str.substr(0, str.length() - suffixLength);
82 std::string stripString(const std::string &str)
84 std::string::const_iterator start = str.begin();
85 std::string::const_iterator end = str.end();
86 while (start != end && std::isspace(*start))
90 while (start != end && std::isspace(*(end - 1)))
94 return std::string(start, end);
97 std::string formatString(const char *fmt, ...)
100 char staticBuf[1024];
102 std::vector<char> dynamicBuf;
103 char *buf = staticBuf;
105 // TODO: There may be a better way of doing this on Windows, Microsoft
106 // provides their own way of doing things...
110 int n = vsnprintf(buf, length, fmt, ap);
112 if (n > -1 && n < length)
114 std::string result(buf);
125 dynamicBuf.resize(length);
126 buf = &dynamicBuf[0];
130 std::vector<std::string> splitString(const std::string &str)
132 std::vector<std::string> result;
133 std::string::const_iterator currPos = str.begin();
134 const std::string::const_iterator end = str.end();
135 while (currPos != end)
137 while (currPos != end && std::isspace(*currPos))
141 const std::string::const_iterator startPos = currPos;
142 while (currPos != end && !std::isspace(*currPos))
148 result.push_back(std::string(startPos, currPos));
154 std::string concatenateStrings(const char *const *sarray, size_t count)
158 for (size_t i = 0; i < count && sarray[i] != NULL; ++i)
160 if (sarray[i][0] != '\0')
162 result.append(sarray[i]);
163 char lastchar = sarray[i][std::strlen(sarray[i])-1];
164 if (!std::isspace(lastchar))
170 result.resize(result.find_last_not_of(" \n\r\t") + 1);
178 * Helper function to identify word boundaries for replaceAllWords().
180 * \returns `true` if the character is considered part of a word.
182 * \ingroup module_utility
184 bool isWordChar(char c)
186 return std::isalnum(c) || c == '-' || c == '_';
190 * Common implementation for string replacement functions.
192 * \param[in] input Input string.
193 * \param[in] from String to find.
194 * \param[in] to String to use to replace \p from.
195 * \param[in] bWholeWords Whether to only consider matches to whole words.
196 * \returns \p input with all occurrences of \p from replaced with \p to.
197 * \throws std::bad_alloc if out of memory.
199 * \ingroup module_utility
202 replaceInternal(const std::string &input, const char *from, const char *to,
205 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
206 "Replacement strings must not be NULL");
207 size_t matchLength = std::strlen(from);
210 size_t matchPos = input.find(from);
211 while (matchPos < input.length())
213 size_t matchEnd = matchPos + matchLength;
216 if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
217 && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
219 matchPos = input.find(from, matchPos + 1);
224 result.append(input, inputPos, matchPos - inputPos);
227 matchPos = input.find(from, inputPos);
229 result.append(input, inputPos, matchPos - inputPos);
236 replaceAll(const std::string &input, const char *from, const char *to)
238 return replaceInternal(input, from, to, false);
242 replaceAll(const std::string &input, const std::string &from,
243 const std::string &to)
245 return replaceInternal(input, from.c_str(), to.c_str(), false);
249 replaceAllWords(const std::string &input, const char *from, const char *to)
251 return replaceInternal(input, from, to, true);
255 replaceAllWords(const std::string &input, const std::string &from,
256 const std::string &to)
258 return replaceInternal(input, from.c_str(), to.c_str(), true);
262 /********************************************************************
263 * TextLineWrapperSettings
266 TextLineWrapperSettings::TextLineWrapperSettings()
267 : maxLength_(0), indent_(0), firstLineIndent_(-1),
268 bStripLeadingWhitespace_(true), continuationChar_('\0')
273 /********************************************************************
278 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
280 size_t inputLength = std::strlen(input);
281 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
282 // Ignore leading whitespace if necessary.
283 if (!bFirstLine || settings_.bStripLeadingWhitespace_)
285 lineStart += std::strspn(input + lineStart, " ");
286 if (lineStart >= inputLength)
292 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
293 size_t lastAllowedBreakPoint
294 = (settings_.lineLength() > 0
295 ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
297 // Ignore trailing whitespace.
298 lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
299 size_t lineEnd = lineStart;
302 const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
304 = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
305 if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
309 lineEnd = nextBreak + 1;
311 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
312 return (lineEnd < inputLength ? lineEnd : inputLength);
316 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
318 return findNextLine(input.c_str(), lineStart);
322 TextLineWrapper::formatLine(const std::string &input,
323 size_t lineStart, size_t lineEnd) const
325 size_t inputLength = input.length();
326 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
327 // Strip leading whitespace if necessary.
328 if (!bFirstLine || settings_.bStripLeadingWhitespace_)
330 lineStart = input.find_first_not_of(' ', lineStart);
331 if (lineStart >= inputLength)
333 return std::string();
336 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
337 bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
338 // Strip trailing whitespace.
339 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
344 size_t lineLength = lineEnd - lineStart;
345 std::string result(indent, ' ');
346 result.append(input, lineStart, lineLength);
347 if (bContinuation && settings_.continuationChar_ != '\0')
349 result.append(1, ' ');
350 result.append(1, settings_.continuationChar_);
356 TextLineWrapper::wrapToString(const std::string &input) const
359 size_t lineStart = 0;
360 size_t length = input.length();
361 while (lineStart < length)
363 size_t nextLineStart = findNextLine(input, lineStart);
364 result.append(formatLine(input, lineStart, nextLineStart));
365 if (nextLineStart < length
366 || (nextLineStart == length && input[length - 1] == '\n'))
370 lineStart = nextLineStart;
375 std::vector<std::string>
376 TextLineWrapper::wrapToVector(const std::string &input) const
378 std::vector<std::string> result;
379 size_t lineStart = 0;
380 size_t length = input.length();
381 while (lineStart < length)
383 size_t nextLineStart = findNextLine(input, lineStart);
384 result.push_back(formatLine(input, lineStart, nextLineStart));
385 lineStart = nextLineStart;