Merge branch release-4-6
[alexxy/gromacs.git] / src / gromacs / utility / stringutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements functions and classes in stringutil.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "stringutil.h"
43
44 #include <cctype>
45 #include <cstdio>
46 #include <cstdarg>
47 #include <cstring>
48
49 #include <algorithm>
50 #include <string>
51 #include <vector>
52
53 #include "gromacs/utility/gmxassert.h"
54
55 namespace gmx
56 {
57
58 bool endsWith(const std::string &str, const char *suffix)
59 {
60     if (suffix == NULL || suffix[0] == '\0')
61     {
62         return true;
63     }
64     size_t length = std::strlen(suffix);
65     return (str.length() >= length
66             && str.compare(str.length() - length, length, suffix) == 0);
67 }
68
69 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
70 {
71     if (suffix != NULL)
72     {
73         size_t suffixLength = std::strlen(suffix);
74         if (suffixLength > 0 && endsWith(str, suffix))
75         {
76             return str.substr(0, str.length() - suffixLength);
77         }
78     }
79     return str;
80 }
81
82 std::string formatString(const char *fmt, ...)
83 {
84     va_list           ap;
85     char              staticBuf[1024];
86     int               length = 1024;
87     std::vector<char> dynamicBuf;
88     char             *buf = staticBuf;
89
90     // TODO: There may be a better way of doing this on Windows, Microsoft
91     // provides their own way of doing things...
92     while (1)
93     {
94         va_start(ap, fmt);
95         int n = vsnprintf(buf, length, fmt, ap);
96         va_end(ap);
97         if (n > -1 && n < length)
98         {
99             std::string result(buf);
100             return result;
101         }
102         if (n > -1)
103         {
104             length = n + 1;
105         }
106         else
107         {
108             length *= 2;
109         }
110         dynamicBuf.resize(length);
111         buf = &dynamicBuf[0];
112     }
113 }
114
115 std::string concatenateStrings(const char *const *sarray, size_t count)
116 {
117     std::string result;
118
119     for (size_t i = 0; i < count && sarray[i] != NULL; ++i)
120     {
121         if (sarray[i][0] != '\0')
122         {
123             result.append(sarray[i]);
124             char lastchar = sarray[i][std::strlen(sarray[i])-1];
125             if (!std::isspace(lastchar))
126             {
127                 result.append(" ");
128             }
129         }
130     }
131     result.resize(result.find_last_not_of(" \n\r\t") + 1);
132     return result;
133 }
134
135 namespace
136 {
137
138 /*! \brief
139  * Common implementation for string replacement functions.
140  *
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.
147  *
148  * \ingroup module_utility
149  */
150 std::string
151 replaceInternal(const std::string &input, const char *from, const char *to,
152                 bool bWholeWords)
153 {
154     GMX_RELEASE_ASSERT(from != NULL && to != NULL,
155                        "Replacement strings must not be NULL");
156     size_t      matchLength = std::strlen(from);
157     std::string result;
158     size_t      inputPos = 0;
159     size_t      matchPos = input.find(from);
160     while (matchPos < input.length())
161     {
162         size_t matchEnd = matchPos + matchLength;
163         if (bWholeWords)
164         {
165             if (!((matchPos == 0 || !std::isalnum(input[matchPos-1]))
166                   && (matchEnd == input.length() || !std::isalnum(input[matchEnd]))))
167             {
168                 matchPos = input.find(from, matchPos + 1);
169                 continue;
170             }
171
172         }
173         result.append(input, inputPos, matchPos - inputPos);
174         result.append(to);
175         inputPos = matchEnd;
176         matchPos = input.find(from, inputPos);
177     }
178     result.append(input, inputPos, matchPos - inputPos);
179     return result;
180 }
181
182 }   // namespace
183
184 std::string
185 replaceAll(const std::string &input, const char *from, const char *to)
186 {
187     return replaceInternal(input, from, to, false);
188 }
189
190 std::string
191 replaceAll(const std::string &input, const std::string &from,
192            const std::string &to)
193 {
194     return replaceInternal(input, from.c_str(), to.c_str(), false);
195 }
196
197 std::string
198 replaceAllWords(const std::string &input, const char *from, const char *to)
199 {
200     return replaceInternal(input, from, to, true);
201 }
202
203 std::string
204 replaceAllWords(const std::string &input, const std::string &from,
205                 const std::string &to)
206 {
207     return replaceInternal(input, from.c_str(), to.c_str(), true);
208 }
209
210
211 /********************************************************************
212  * TextLineWrapperSettings
213  */
214
215 TextLineWrapperSettings::TextLineWrapperSettings()
216     : maxLength_(0), indent_(0), firstLineIndent_(-1),
217       bStripLeadingWhitespace_(true), continuationChar_('\0')
218 {
219 }
220
221
222 /********************************************************************
223  * TextLineWrapper
224  */
225
226 size_t
227 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
228 {
229     size_t inputLength = std::strlen(input);
230     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
231     // Ignore leading whitespace if necessary.
232     if (!bFirstLine || settings_.bStripLeadingWhitespace_)
233     {
234         lineStart += std::strspn(input + lineStart, " ");
235         if (lineStart >= inputLength)
236         {
237             return inputLength;
238         }
239     }
240
241     int    indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
242     size_t lastAllowedBreakPoint
243         = (settings_.lineLength() > 0
244            ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
245            : inputLength);
246     // Ignore trailing whitespace.
247     lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
248     size_t lineEnd = lineStart;
249     do
250     {
251         const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
252         size_t      nextBreak
253             = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
254         if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
255         {
256             break;
257         }
258         lineEnd = nextBreak + 1;
259     }
260     while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
261     return (lineEnd < inputLength ? lineEnd : inputLength);
262 }
263
264 size_t
265 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
266 {
267     return findNextLine(input.c_str(), lineStart);
268 }
269
270 std::string
271 TextLineWrapper::formatLine(const std::string &input,
272                             size_t lineStart, size_t lineEnd) const
273 {
274     size_t inputLength = input.length();
275     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
276     // Strip leading whitespace if necessary.
277     if (!bFirstLine || settings_.bStripLeadingWhitespace_)
278     {
279         lineStart = input.find_first_not_of(' ', lineStart);
280         if (lineStart >= inputLength)
281         {
282             return std::string();
283         }
284     }
285     int  indent        = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
286     bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
287     // Strip trailing whitespace.
288     while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
289     {
290         --lineEnd;
291     }
292
293     size_t      lineLength = lineEnd - lineStart;
294     std::string result(indent, ' ');
295     result.append(input, lineStart, lineLength);
296     if (bContinuation && settings_.continuationChar_ != '\0')
297     {
298         result.append(1, ' ');
299         result.append(1, settings_.continuationChar_);
300     }
301     return result;
302 }
303
304 std::string
305 TextLineWrapper::wrapToString(const std::string &input) const
306 {
307     std::string result;
308     size_t      lineStart = 0;
309     size_t      length    = input.length();
310     while (lineStart < length)
311     {
312         size_t nextLineStart = findNextLine(input, lineStart);
313         result.append(formatLine(input, lineStart, nextLineStart));
314         if (nextLineStart < length
315             || (nextLineStart == length && input[length - 1] == '\n'))
316         {
317             result.append("\n");
318         }
319         lineStart = nextLineStart;
320     }
321     return result;
322 }
323
324 std::vector<std::string>
325 TextLineWrapper::wrapToVector(const std::string &input) const
326 {
327     std::vector<std::string> result;
328     size_t                   lineStart = 0;
329     size_t                   length    = input.length();
330     while (lineStart < length)
331     {
332         size_t nextLineStart = findNextLine(input, lineStart);
333         result.push_back(formatLine(input, lineStart, nextLineStart));
334         lineStart = nextLineStart;
335     }
336     return result;
337 }
338
339 } // namespace gmx