2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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 formatString(const char *fmt, ...)
87 std::vector<char> dynamicBuf;
88 char *buf = staticBuf;
90 // TODO: There may be a better way of doing this on Windows, Microsoft
91 // provides their own way of doing things...
95 int n = vsnprintf(buf, length, fmt, ap);
97 if (n > -1 && n < length)
99 std::string result(buf);
110 dynamicBuf.resize(length);
111 buf = &dynamicBuf[0];
115 std::string concatenateStrings(const char *const *sarray, size_t count)
119 for (size_t i = 0; i < count && sarray[i] != NULL; ++i)
121 if (sarray[i][0] != '\0')
123 result.append(sarray[i]);
124 char lastchar = sarray[i][std::strlen(sarray[i])-1];
125 if (!std::isspace(lastchar))
131 result.resize(result.find_last_not_of(" \n\r\t") + 1);
139 * Common implementation for string replacement functions.
141 * \param[in] input Input string.
142 * \param[in] from String to find.
143 * \param[in] to String to use to replace \p from.
144 * \param[in] bWholeWords Whether to only consider matches to whole words.
145 * \returns \p input with all occurrences of \p from replaced with \p to.
146 * \throws std::bad_alloc if out of memory.
149 replaceInternal(const std::string &input, const char *from, const char *to,
152 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
153 "Replacement strings must not be NULL");
154 size_t matchLength = std::strlen(from);
157 size_t matchPos = input.find(from);
158 while (matchPos < input.length())
160 size_t matchEnd = matchPos + matchLength;
163 if (!((matchPos == 0 || !std::isalnum(input[matchPos-1]))
164 && (matchEnd == input.length() || !std::isalnum(input[matchEnd]))))
166 matchPos = input.find(from, matchPos + 1);
171 result.append(input, inputPos, matchPos - inputPos);
174 matchPos = input.find(from, inputPos);
176 result.append(input, inputPos, matchPos - inputPos);
183 replaceAll(const std::string &input, const char *from, const char *to)
185 return replaceInternal(input, from, to, false);
189 replaceAllWords(const std::string &input, const char *from, const char *to)
191 return replaceInternal(input, from, to, true);
195 /********************************************************************
196 * TextLineWrapperSettings
199 TextLineWrapperSettings::TextLineWrapperSettings()
200 : maxLength_(0), indent_(0), firstLineIndent_(-1),
201 bStripLeadingWhitespace_(true), continuationChar_('\0')
206 /********************************************************************
211 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
213 size_t inputLength = std::strlen(input);
214 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
215 // Ignore leading whitespace if necessary.
216 if (!bFirstLine || settings_.bStripLeadingWhitespace_)
218 lineStart += std::strspn(input + lineStart, " ");
219 if (lineStart >= inputLength)
225 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
226 size_t lastAllowedBreakPoint
227 = (settings_.lineLength() > 0
228 ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
230 // Ignore trailing whitespace.
231 lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
232 size_t lineEnd = lineStart;
235 const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
237 = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
238 if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
242 lineEnd = nextBreak + 1;
244 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
245 return (lineEnd < inputLength ? lineEnd : inputLength);
249 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
251 return findNextLine(input.c_str(), lineStart);
255 TextLineWrapper::formatLine(const std::string &input,
256 size_t lineStart, size_t lineEnd) const
258 size_t inputLength = input.length();
259 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
260 // Strip leading whitespace if necessary.
261 if (!bFirstLine || settings_.bStripLeadingWhitespace_)
263 lineStart = input.find_first_not_of(' ', lineStart);
264 if (lineStart >= inputLength)
266 return std::string();
269 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
270 bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
271 // Strip trailing whitespace.
272 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
277 size_t lineLength = lineEnd - lineStart;
278 std::string result(indent, ' ');
279 result.append(input, lineStart, lineLength);
280 if (bContinuation && settings_.continuationChar_ != '\0')
282 result.append(1, ' ');
283 result.append(1, settings_.continuationChar_);
289 TextLineWrapper::wrapToString(const std::string &input) const
292 size_t lineStart = 0;
293 size_t length = input.length();
294 while (lineStart < length)
296 size_t nextLineStart = findNextLine(input, lineStart);
297 result.append(formatLine(input, lineStart, nextLineStart));
298 if (nextLineStart < length
299 || (nextLineStart == length && input[length - 1] == '\n'))
303 lineStart = nextLineStart;
308 std::vector<std::string>
309 TextLineWrapper::wrapToVector(const std::string &input) const
311 std::vector<std::string> result;
312 size_t lineStart = 0;
313 size_t length = input.length();
314 while (lineStart < length)
316 size_t nextLineStart = findNextLine(input, lineStart);
317 result.push_back(formatLine(input, lineStart, nextLineStart));
318 lineStart = nextLineStart;