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 std::string formatString(const char *fmt, ...)
59 std::vector<char> dynamicBuf;
60 char *buf = staticBuf;
62 // TODO: There may be a better way of doing this on Windows, Microsoft
63 // provides their own way of doing things...
67 int n = vsnprintf(buf, length, fmt, ap);
69 if (n > -1 && n < length)
71 std::string result(buf);
82 dynamicBuf.resize(length);
87 std::string concatenateStrings(const char *const *sarray, size_t count)
91 for (size_t i = 0; i < count && sarray[i] != NULL; ++i)
93 if (sarray[i][0] != '\0')
95 result.append(sarray[i]);
96 char lastchar = sarray[i][std::strlen(sarray[i])-1];
97 if (!std::isspace(lastchar))
103 result.resize(result.find_last_not_of(" \n\r\t") + 1);
111 replaceInternal(const std::string &input, const char *from, const char *to,
114 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
115 "Replacement strings must not be NULL");
116 size_t matchLength = std::strlen(from);
119 size_t matchPos = input.find(from);
120 while (matchPos < input.length())
122 size_t matchEnd = matchPos + matchLength;
125 if (!((matchPos == 0 || !std::isalnum(input[matchPos-1]))
126 && (matchEnd == input.length() || !std::isalnum(input[matchEnd]))))
128 matchPos = input.find(from, matchPos + 1);
133 result.append(input, inputPos, matchPos - inputPos);
136 matchPos = input.find(from, inputPos);
138 result.append(input, inputPos, matchPos - inputPos);
145 replaceAll(const std::string &input, const char *from, const char *to)
147 return replaceInternal(input, from, to, false);
151 replaceAllWords(const std::string &input, const char *from, const char *to)
153 return replaceInternal(input, from, to, true);
156 /********************************************************************
157 * TextLineWrapper::Impl
161 * Private implementation class for TextLineWrapper.
163 * \ingroup module_utility
165 class TextLineWrapper::Impl
168 //! Initialize default values for the wrapper.
169 Impl() : maxLength_(0) {}
172 * Helper method to find the next wrapped line.
174 * \param[in] input Full input string.
175 * \param[in,out] lineStartPtr
176 * Index of first character for the line to wrap.
177 * On exit, index of the first character of the next line.
178 * \returns Next output line.
180 std::string wrapNextLine(const std::string &input,
181 size_t *lineStartPtr) const;
183 //! Maximum length of output lines, or <= 0 if no limit.
188 TextLineWrapper::Impl::wrapNextLine(const std::string &input,
189 size_t *lineStartPtr) const
191 // Strip leading whitespace.
192 size_t lineStart = input.find_first_not_of(' ', *lineStartPtr);
193 if (lineStart == std::string::npos)
195 *lineStartPtr = lineStart;
196 return std::string();
199 size_t lineEnd = std::string::npos;
201 = std::min(input.find('\n', lineStart), input.length());
202 if (maxLength_ <= 0 || nextNewline <= lineStart + maxLength_)
204 lineEnd = nextNewline;
208 size_t bestSpace = input.rfind(' ', lineStart + maxLength_);
209 if (bestSpace < lineStart || bestSpace == std::string::npos)
211 bestSpace = input.find(' ', lineStart);
213 lineEnd = std::min(bestSpace, nextNewline);
216 if (lineEnd == std::string::npos)
218 lineEnd = input.length();
220 *lineStartPtr = lineEnd + 1;
221 // Strip trailing whitespace.
222 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
227 size_t lineLength = lineEnd - lineStart;
228 return input.substr(lineStart, lineLength);
231 /********************************************************************
235 TextLineWrapper::TextLineWrapper()
240 TextLineWrapper::~TextLineWrapper()
244 TextLineWrapper &TextLineWrapper::setLineLength(int length)
246 impl_->maxLength_ = length;
251 TextLineWrapper::wrapToString(const std::string &input) const
254 size_t lineStart = 0;
255 size_t length = input.length();
256 while (lineStart < length)
258 result.append(impl_->wrapNextLine(input, &lineStart));
259 if (lineStart < length
260 || (lineStart == length && input[length - 1] == '\n'))
268 std::vector<std::string>
269 TextLineWrapper::wrapToVector(const std::string &input) const
271 std::vector<std::string> result;
272 size_t lineStart = 0;
273 while (lineStart < input.length())
275 result.push_back(impl_->wrapNextLine(input, &lineStart));