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