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