3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements functions and classes in stringutil.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
38 #include "stringutil.h"
49 #include "gromacs/utility/gmxassert.h"
54 bool endsWith(const std::string &str, const char *suffix)
56 if (suffix == NULL || suffix[0] == '\0')
60 size_t length = std::strlen(suffix);
61 return (str.length() >= length
62 && str.compare(str.length() - length, length, suffix) == 0);
65 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
69 size_t suffixLength = std::strlen(suffix);
70 if (suffixLength > 0 && endsWith(str, suffix))
72 return str.substr(0, str.length() - suffixLength);
78 std::string formatString(const char *fmt, ...)
83 std::vector<char> dynamicBuf;
84 char *buf = staticBuf;
86 // TODO: There may be a better way of doing this on Windows, Microsoft
87 // provides their own way of doing things...
91 int n = vsnprintf(buf, length, fmt, ap);
93 if (n > -1 && n < length)
95 std::string result(buf);
106 dynamicBuf.resize(length);
107 buf = &dynamicBuf[0];
111 std::string concatenateStrings(const char *const *sarray, size_t count)
115 for (size_t i = 0; i < count && sarray[i] != NULL; ++i)
117 if (sarray[i][0] != '\0')
119 result.append(sarray[i]);
120 char lastchar = sarray[i][std::strlen(sarray[i])-1];
121 if (!std::isspace(lastchar))
127 result.resize(result.find_last_not_of(" \n\r\t") + 1);
135 replaceInternal(const std::string &input, const char *from, const char *to,
138 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
139 "Replacement strings must not be NULL");
140 size_t matchLength = std::strlen(from);
143 size_t matchPos = input.find(from);
144 while (matchPos < input.length())
146 size_t matchEnd = matchPos + matchLength;
149 if (!((matchPos == 0 || !std::isalnum(input[matchPos-1]))
150 && (matchEnd == input.length() || !std::isalnum(input[matchEnd]))))
152 matchPos = input.find(from, matchPos + 1);
157 result.append(input, inputPos, matchPos - inputPos);
160 matchPos = input.find(from, inputPos);
162 result.append(input, inputPos, matchPos - inputPos);
169 replaceAll(const std::string &input, const char *from, const char *to)
171 return replaceInternal(input, from, to, false);
175 replaceAllWords(const std::string &input, const char *from, const char *to)
177 return replaceInternal(input, from, to, true);
180 /********************************************************************
181 * TextLineWrapper::Impl
185 * Private implementation class for TextLineWrapper.
187 * \ingroup module_utility
189 class TextLineWrapper::Impl
192 //! Initialize default values for the wrapper.
193 Impl() : maxLength_(0) {}
196 * Helper method to find the next wrapped line.
198 * \param[in] input Full input string.
199 * \param[in,out] lineStartPtr
200 * Index of first character for the line to wrap.
201 * On exit, index of the first character of the next line.
202 * \returns Next output line.
204 std::string wrapNextLine(const std::string &input,
205 size_t *lineStartPtr) const;
207 //! Maximum length of output lines, or <= 0 if no limit.
212 TextLineWrapper::Impl::wrapNextLine(const std::string &input,
213 size_t *lineStartPtr) const
215 // Strip leading whitespace.
216 size_t lineStart = input.find_first_not_of(' ', *lineStartPtr);
217 if (lineStart == std::string::npos)
219 *lineStartPtr = lineStart;
220 return std::string();
223 size_t lineEnd = std::string::npos;
225 = std::min(input.find('\n', lineStart), input.length());
226 if (maxLength_ <= 0 || nextNewline <= lineStart + maxLength_)
228 lineEnd = nextNewline;
232 size_t bestSpace = input.rfind(' ', lineStart + maxLength_);
233 if (bestSpace < lineStart || bestSpace == std::string::npos)
235 bestSpace = input.find(' ', lineStart);
237 lineEnd = std::min(bestSpace, nextNewline);
240 if (lineEnd == std::string::npos)
242 lineEnd = input.length();
244 *lineStartPtr = lineEnd + 1;
245 // Strip trailing whitespace.
246 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
251 size_t lineLength = lineEnd - lineStart;
252 return input.substr(lineStart, lineLength);
255 /********************************************************************
259 TextLineWrapper::TextLineWrapper()
264 TextLineWrapper::~TextLineWrapper()
268 TextLineWrapper &TextLineWrapper::setLineLength(int length)
270 impl_->maxLength_ = length;
275 TextLineWrapper::wrapToString(const std::string &input) const
278 size_t lineStart = 0;
279 size_t length = input.length();
280 while (lineStart < length)
282 result.append(impl_->wrapNextLine(input, &lineStart));
283 if (lineStart < length
284 || (lineStart == length && input[length - 1] == '\n'))
292 std::vector<std::string>
293 TextLineWrapper::wrapToVector(const std::string &input) const
295 std::vector<std::string> result;
296 size_t lineStart = 0;
297 while (lineStart < input.length())
299 result.push_back(impl_->wrapNextLine(input, &lineStart));