Merge branch release-5-1
[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,2014,2015, 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.
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 "gmxpre.h"
43
44 #include "stringutil.h"
45
46 #include <cctype>
47 #include <cstdarg>
48 #include <cstdio>
49 #include <cstring>
50
51 #include <algorithm>
52 #include <string>
53 #include <vector>
54
55 #include "gromacs/utility/gmxassert.h"
56
57 namespace gmx
58 {
59
60 bool endsWith(const char *str, const char *suffix)
61 {
62     if (isNullOrEmpty(suffix))
63     {
64         return true;
65     }
66     const size_t strLength    = std::strlen(str);
67     const size_t suffixLength = std::strlen(suffix);
68     return (strLength >= suffixLength
69             && std::strcmp(&str[strLength - suffixLength], suffix) == 0);
70 }
71
72 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
73 {
74     if (suffix != NULL)
75     {
76         size_t suffixLength = std::strlen(suffix);
77         if (suffixLength > 0 && endsWith(str, suffix))
78         {
79             return str.substr(0, str.length() - suffixLength);
80         }
81     }
82     return str;
83 }
84
85 std::string stripString(const std::string &str)
86 {
87     std::string::const_iterator start = str.begin();
88     std::string::const_iterator end   = str.end();
89     while (start != end && std::isspace(*start))
90     {
91         ++start;
92     }
93     while (start != end && std::isspace(*(end - 1)))
94     {
95         --end;
96     }
97     return std::string(start, end);
98 }
99
100 std::string formatString(const char *fmt, ...)
101 {
102     va_list           ap;
103     char              staticBuf[1024];
104     int               length = 1024;
105     std::vector<char> dynamicBuf;
106     char             *buf = staticBuf;
107
108     // TODO: There may be a better way of doing this on Windows, Microsoft
109     // provides their own way of doing things...
110     while (1)
111     {
112         va_start(ap, fmt);
113         int n = vsnprintf(buf, length, fmt, ap);
114         va_end(ap);
115         if (n > -1 && n < length)
116         {
117             std::string result(buf);
118             return result;
119         }
120         if (n > -1)
121         {
122             length = n + 1;
123         }
124         else
125         {
126             length *= 2;
127         }
128         dynamicBuf.resize(length);
129         buf = &dynamicBuf[0];
130     }
131 }
132
133 std::vector<std::string> splitString(const std::string &str)
134 {
135     std::vector<std::string>          result;
136     std::string::const_iterator       currPos = str.begin();
137     const std::string::const_iterator end     = str.end();
138     while (currPos != end)
139     {
140         while (currPos != end && std::isspace(*currPos))
141         {
142             ++currPos;
143         }
144         const std::string::const_iterator startPos = currPos;
145         while (currPos != end && !std::isspace(*currPos))
146         {
147             ++currPos;
148         }
149         if (startPos != end)
150         {
151             result.push_back(std::string(startPos, currPos));
152         }
153     }
154     return result;
155 }
156
157 namespace
158 {
159
160 /*! \brief
161  * Helper function to identify word boundaries for replaceAllWords().
162  *
163  * \returns  `true` if the character is considered part of a word.
164  *
165  * \ingroup module_utility
166  */
167 bool isWordChar(char c)
168 {
169     return std::isalnum(c) || c == '-' || c == '_';
170 }
171
172 /*! \brief
173  * Common implementation for string replacement functions.
174  *
175  * \param[in] input  Input string.
176  * \param[in] from   String to find.
177  * \param[in] to     String to use to replace \p from.
178  * \param[in] bWholeWords  Whether to only consider matches to whole words.
179  * \returns   \p input with all occurrences of \p from replaced with \p to.
180  * \throws    std::bad_alloc if out of memory.
181  *
182  * \ingroup module_utility
183  */
184 std::string
185 replaceInternal(const std::string &input, const char *from, const char *to,
186                 bool bWholeWords)
187 {
188     GMX_RELEASE_ASSERT(from != NULL && to != NULL,
189                        "Replacement strings must not be NULL");
190     size_t      matchLength = std::strlen(from);
191     std::string result;
192     size_t      inputPos = 0;
193     size_t      matchPos = input.find(from);
194     while (matchPos < input.length())
195     {
196         size_t matchEnd = matchPos + matchLength;
197         if (bWholeWords)
198         {
199             if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
200                   && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
201             {
202                 matchPos = input.find(from, matchPos + 1);
203                 continue;
204             }
205
206         }
207         result.append(input, inputPos, matchPos - inputPos);
208         result.append(to);
209         inputPos = matchEnd;
210         matchPos = input.find(from, inputPos);
211     }
212     result.append(input, inputPos, matchPos - inputPos);
213     return result;
214 }
215
216 }   // namespace
217
218 std::string
219 replaceAll(const std::string &input, const char *from, const char *to)
220 {
221     return replaceInternal(input, from, to, false);
222 }
223
224 std::string
225 replaceAll(const std::string &input, const std::string &from,
226            const std::string &to)
227 {
228     return replaceInternal(input, from.c_str(), to.c_str(), false);
229 }
230
231 std::string
232 replaceAllWords(const std::string &input, const char *from, const char *to)
233 {
234     return replaceInternal(input, from, to, true);
235 }
236
237 std::string
238 replaceAllWords(const std::string &input, const std::string &from,
239                 const std::string &to)
240 {
241     return replaceInternal(input, from.c_str(), to.c_str(), true);
242 }
243
244
245 /********************************************************************
246  * TextLineWrapperSettings
247  */
248
249 TextLineWrapperSettings::TextLineWrapperSettings()
250     : maxLength_(0), indent_(0), firstLineIndent_(-1),
251       bKeepFinalSpaces_(false), continuationChar_('\0')
252 {
253 }
254
255
256 /********************************************************************
257  * TextLineWrapper
258  */
259
260 bool TextLineWrapper::isTrivial() const
261 {
262     return settings_.lineLength() == 0 && settings_.indent() == 0
263            && settings_.firstLineIndent_ <= 0;
264 }
265
266 size_t
267 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
268 {
269     size_t inputLength = std::strlen(input);
270     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
271     // Ignore leading whitespace if necessary.
272     if (!bFirstLine)
273     {
274         lineStart += std::strspn(input + lineStart, " ");
275         if (lineStart >= inputLength)
276         {
277             return inputLength;
278         }
279     }
280
281     int    indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
282     size_t lastAllowedBreakPoint
283         = (settings_.lineLength() > 0
284            ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
285            : inputLength);
286     // Ignore trailing whitespace.
287     lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
288     size_t lineEnd = lineStart;
289     do
290     {
291         const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
292         size_t      nextBreak
293             = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
294         if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
295         {
296             break;
297         }
298         lineEnd = nextBreak + 1;
299     }
300     while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
301     return (lineEnd < inputLength ? lineEnd : inputLength);
302 }
303
304 size_t
305 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
306 {
307     return findNextLine(input.c_str(), lineStart);
308 }
309
310 std::string
311 TextLineWrapper::formatLine(const std::string &input,
312                             size_t lineStart, size_t lineEnd) const
313 {
314     size_t inputLength = input.length();
315     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
316     // Strip leading whitespace if necessary.
317     if (!bFirstLine)
318     {
319         lineStart = input.find_first_not_of(' ', lineStart);
320         if (lineStart >= inputLength)
321         {
322             return std::string();
323         }
324     }
325     int  indent        = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
326     bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
327     // Strip trailing whitespace.
328     if (!settings_.bKeepFinalSpaces_ || lineEnd < inputLength || input[inputLength - 1] == '\n')
329     {
330         while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
331         {
332             --lineEnd;
333         }
334     }
335
336     const size_t lineLength = lineEnd - lineStart;
337     if (lineLength == 0)
338     {
339         return std::string();
340     }
341     std::string result(indent, ' ');
342     result.append(input, lineStart, lineLength);
343     if (bContinuation && settings_.continuationChar_ != '\0')
344     {
345         result.append(1, ' ');
346         result.append(1, settings_.continuationChar_);
347     }
348     return result;
349 }
350
351 std::string
352 TextLineWrapper::wrapToString(const std::string &input) const
353 {
354     std::string result;
355     size_t      lineStart = 0;
356     size_t      length    = input.length();
357     while (lineStart < length)
358     {
359         size_t nextLineStart = findNextLine(input, lineStart);
360         result.append(formatLine(input, lineStart, nextLineStart));
361         if (nextLineStart < length
362             || (nextLineStart == length && input[length - 1] == '\n'))
363         {
364             result.append("\n");
365         }
366         lineStart = nextLineStart;
367     }
368     return result;
369 }
370
371 std::vector<std::string>
372 TextLineWrapper::wrapToVector(const std::string &input) const
373 {
374     std::vector<std::string> result;
375     size_t                   lineStart = 0;
376     size_t                   length    = input.length();
377     while (lineStart < length)
378     {
379         size_t nextLineStart = findNextLine(input, lineStart);
380         result.push_back(formatLine(input, lineStart, nextLineStart));
381         lineStart = nextLineStart;
382     }
383     return result;
384 }
385
386 } // namespace gmx