1a1b1c1e224e4ef6e64da0e16aac0e13693e32dd
[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,2016, 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     va_start(ap, fmt);
138     std::string result = formatStringV(fmt, ap);
139     va_end(ap);
140     return result;
141 }
142
143 std::string formatStringV(const char *fmt, va_list ap)
144 {
145     va_list           ap_copy;
146     char              staticBuf[1024];
147     int               length = 1024;
148     std::vector<char> dynamicBuf;
149     char             *buf = staticBuf;
150
151     // TODO: There may be a better way of doing this on Windows, Microsoft
152     // provides their own way of doing things...
153     while (1)
154     {
155         va_copy(ap_copy, ap);
156         int n = vsnprintf(buf, length, fmt, ap_copy);
157         va_end(ap_copy);
158         if (n > -1 && n < length)
159         {
160             std::string result(buf);
161             return result;
162         }
163         if (n > -1)
164         {
165             length = n + 1;
166         }
167         else
168         {
169             length *= 2;
170         }
171         dynamicBuf.resize(length);
172         buf = dynamicBuf.data();
173     }
174 }
175
176 std::vector<std::string> splitString(const std::string &str)
177 {
178     std::vector<std::string>          result;
179     std::string::const_iterator       currPos = str.begin();
180     const std::string::const_iterator end     = str.end();
181     while (currPos != end)
182     {
183         while (currPos != end && std::isspace(*currPos))
184         {
185             ++currPos;
186         }
187         const std::string::const_iterator startPos = currPos;
188         while (currPos != end && !std::isspace(*currPos))
189         {
190             ++currPos;
191         }
192         if (startPos != end)
193         {
194             result.emplace_back(startPos, currPos);
195         }
196     }
197     return result;
198 }
199
200 std::vector<std::string> splitDelimitedString(const std::string &str, char delim)
201 {
202     std::vector<std::string> result;
203     size_t                   currPos = 0;
204     const size_t             len     = str.length();
205     if (len > 0)
206     {
207         size_t nextDelim;
208         do
209         {
210             nextDelim = str.find(delim, currPos);
211             result.push_back(str.substr(currPos, nextDelim - currPos));
212             currPos = nextDelim < len ? nextDelim + 1 : len;
213         }
214         while (currPos < len || nextDelim < len);
215     }
216     return result;
217 }
218
219 namespace
220 {
221
222 /*! \brief
223  * Helper function to identify word boundaries for replaceAllWords().
224  *
225  * \returns  `true` if the character is considered part of a word.
226  *
227  * \ingroup module_utility
228  */
229 bool isWordChar(char c)
230 {
231     return std::isalnum(c) || c == '-' || c == '_';
232 }
233
234 /*! \brief
235  * Common implementation for string replacement functions.
236  *
237  * \param[in] input  Input string.
238  * \param[in] from   String to find.
239  * \param[in] to     String to use to replace \p from.
240  * \param[in] bWholeWords  Whether to only consider matches to whole words.
241  * \returns   \p input with all occurrences of \p from replaced with \p to.
242  * \throws    std::bad_alloc if out of memory.
243  *
244  * \ingroup module_utility
245  */
246 std::string
247 replaceInternal(const std::string &input, const char *from, const char *to,
248                 bool bWholeWords)
249 {
250     GMX_RELEASE_ASSERT(from != NULL && to != NULL,
251                        "Replacement strings must not be NULL");
252     size_t      matchLength = std::strlen(from);
253     std::string result;
254     size_t      inputPos = 0;
255     size_t      matchPos = input.find(from);
256     while (matchPos < input.length())
257     {
258         size_t matchEnd = matchPos + matchLength;
259         if (bWholeWords)
260         {
261             if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
262                   && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
263             {
264                 matchPos = input.find(from, matchPos + 1);
265                 continue;
266             }
267
268         }
269         result.append(input, inputPos, matchPos - inputPos);
270         result.append(to);
271         inputPos = matchEnd;
272         matchPos = input.find(from, inputPos);
273     }
274     result.append(input, inputPos, matchPos - inputPos);
275     return result;
276 }
277
278 }   // namespace
279
280 std::string
281 replaceAll(const std::string &input, const char *from, const char *to)
282 {
283     return replaceInternal(input, from, to, false);
284 }
285
286 std::string
287 replaceAll(const std::string &input, const std::string &from,
288            const std::string &to)
289 {
290     return replaceInternal(input, from.c_str(), to.c_str(), false);
291 }
292
293 std::string
294 replaceAllWords(const std::string &input, const char *from, const char *to)
295 {
296     return replaceInternal(input, from, to, true);
297 }
298
299 std::string
300 replaceAllWords(const std::string &input, const std::string &from,
301                 const std::string &to)
302 {
303     return replaceInternal(input, from.c_str(), to.c_str(), true);
304 }
305
306
307 /********************************************************************
308  * TextLineWrapperSettings
309  */
310
311 TextLineWrapperSettings::TextLineWrapperSettings()
312     : maxLength_(0), indent_(0), firstLineIndent_(-1),
313       bKeepFinalSpaces_(false), continuationChar_('\0')
314 {
315 }
316
317
318 /********************************************************************
319  * TextLineWrapper
320  */
321
322 bool TextLineWrapper::isTrivial() const
323 {
324     return settings_.lineLength() == 0 && settings_.indent() == 0
325            && settings_.firstLineIndent_ <= 0;
326 }
327
328 size_t
329 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
330 {
331     size_t inputLength = std::strlen(input);
332     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
333     // Ignore leading whitespace if necessary.
334     if (!bFirstLine)
335     {
336         lineStart += std::strspn(input + lineStart, " ");
337         if (lineStart >= inputLength)
338         {
339             return inputLength;
340         }
341     }
342
343     int    indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
344     size_t lastAllowedBreakPoint
345         = (settings_.lineLength() > 0
346            ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
347            : inputLength);
348     // Ignore trailing whitespace.
349     lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
350     size_t lineEnd = lineStart;
351     do
352     {
353         const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
354         size_t      nextBreak
355             = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
356         if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
357         {
358             break;
359         }
360         lineEnd = nextBreak + 1;
361     }
362     while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
363     return (lineEnd < inputLength ? lineEnd : inputLength);
364 }
365
366 size_t
367 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
368 {
369     return findNextLine(input.c_str(), lineStart);
370 }
371
372 std::string
373 TextLineWrapper::formatLine(const std::string &input,
374                             size_t lineStart, size_t lineEnd) const
375 {
376     size_t inputLength = input.length();
377     bool   bFirstLine  = (lineStart == 0 || input[lineStart - 1] == '\n');
378     // Strip leading whitespace if necessary.
379     if (!bFirstLine)
380     {
381         lineStart = input.find_first_not_of(' ', lineStart);
382         if (lineStart >= inputLength)
383         {
384             return std::string();
385         }
386     }
387     int  indent        = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
388     bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
389     // Strip trailing whitespace.
390     if (!settings_.bKeepFinalSpaces_ || lineEnd < inputLength || input[inputLength - 1] == '\n')
391     {
392         while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
393         {
394             --lineEnd;
395         }
396     }
397
398     const size_t lineLength = lineEnd - lineStart;
399     if (lineLength == 0)
400     {
401         return std::string();
402     }
403     std::string result(indent, ' ');
404     result.append(input, lineStart, lineLength);
405     if (bContinuation && settings_.continuationChar_ != '\0')
406     {
407         result.append(1, ' ');
408         result.append(1, settings_.continuationChar_);
409     }
410     return result;
411 }
412
413 std::string
414 TextLineWrapper::wrapToString(const std::string &input) const
415 {
416     std::string result;
417     size_t      lineStart = 0;
418     size_t      length    = input.length();
419     while (lineStart < length)
420     {
421         size_t nextLineStart = findNextLine(input, lineStart);
422         result.append(formatLine(input, lineStart, nextLineStart));
423         if (nextLineStart < length
424             || (nextLineStart == length && input[length - 1] == '\n'))
425         {
426             result.append("\n");
427         }
428         lineStart = nextLineStart;
429     }
430     return result;
431 }
432
433 std::vector<std::string>
434 TextLineWrapper::wrapToVector(const std::string &input) const
435 {
436     std::vector<std::string> result;
437     size_t                   lineStart = 0;
438     size_t                   length    = input.length();
439     while (lineStart < length)
440     {
441         size_t nextLineStart = findNextLine(input, lineStart);
442         result.push_back(formatLine(input, lineStart, nextLineStart));
443         lineStart = nextLineStart;
444     }
445     return result;
446 }
447
448 } // namespace gmx