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