Tidy: modernize-use-nullptr
[gromacs.git] / src / gromacs / utility / stringutil.cpp
blob9b9cc1a2a12b5b4c2894939802ae0c6c0791ab5d
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013,2014,2015,2016,2017, 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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief
37 * Implements functions and classes in stringutil.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
42 #include "gmxpre.h"
44 #include "stringutil.h"
46 #include <cctype>
47 #include <cstdarg>
48 #include <cstdio>
49 #include <cstring>
51 #include <algorithm>
52 #include <string>
53 #include <vector>
55 #include "gromacs/utility/gmxassert.h"
57 namespace gmx
60 std::size_t
61 countWords(const char *s)
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);
67 for (std::size_t i = 0; i < length; i++)
69 // If we found a new word, increase counter and step through the word
70 if (std::isalnum(s[i]))
72 ++nWords;
73 // If we hit string end, '\0' is not alphanumerical
74 while (std::isalnum(s[i]))
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++;
83 return nWords;
87 std::size_t
88 countWords(const std::string &str)
90 // Under out beautiful C++ interface hides an ugly c-string implementation :-)
91 return countWords(str.c_str());
94 bool endsWith(const char *str, const char *suffix)
96 if (isNullOrEmpty(suffix))
98 return true;
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);
106 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
108 if (suffix != nullptr)
110 size_t suffixLength = std::strlen(suffix);
111 if (suffixLength > 0 && endsWith(str, suffix))
113 return str.substr(0, str.length() - suffixLength);
116 return str;
119 std::string stripString(const std::string &str)
121 std::string::const_iterator start = str.begin();
122 std::string::const_iterator end = str.end();
123 while (start != end && std::isspace(*start))
125 ++start;
127 while (start != end && std::isspace(*(end - 1)))
129 --end;
131 return std::string(start, end);
134 std::string formatString(const char *fmt, ...)
136 va_list ap;
137 va_start(ap, fmt);
138 std::string result = formatStringV(fmt, ap);
139 va_end(ap);
140 return result;
143 std::string formatStringV(const char *fmt, va_list ap)
145 va_list ap_copy;
146 char staticBuf[1024];
147 int length = 1024;
148 std::vector<char> dynamicBuf;
149 char *buf = staticBuf;
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)
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)
160 std::string result(buf);
161 return result;
163 if (n > -1)
165 length = n + 1;
167 else
169 length *= 2;
171 dynamicBuf.resize(length);
172 buf = dynamicBuf.data();
176 std::vector<std::string> splitString(const std::string &str)
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)
183 while (currPos != end && std::isspace(*currPos))
185 ++currPos;
187 const std::string::const_iterator startPos = currPos;
188 while (currPos != end && !std::isspace(*currPos))
190 ++currPos;
192 if (startPos != end)
194 result.emplace_back(startPos, currPos);
197 return result;
200 std::vector<std::string> splitDelimitedString(const std::string &str, char delim)
202 std::vector<std::string> result;
203 size_t currPos = 0;
204 const size_t len = str.length();
205 if (len > 0)
207 size_t nextDelim;
210 nextDelim = str.find(delim, currPos);
211 result.push_back(str.substr(currPos, nextDelim - currPos));
212 currPos = nextDelim < len ? nextDelim + 1 : len;
214 while (currPos < len || nextDelim < len);
216 return result;
219 namespace
222 /*! \brief
223 * Helper function to identify word boundaries for replaceAllWords().
225 * \returns `true` if the character is considered part of a word.
227 * \ingroup module_utility
229 bool isWordChar(char c)
231 return std::isalnum(c) || c == '-' || c == '_';
234 /*! \brief
235 * Common implementation for string replacement functions.
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.
244 * \ingroup module_utility
246 std::string
247 replaceInternal(const std::string &input, const char *from, const char *to,
248 bool bWholeWords)
250 GMX_RELEASE_ASSERT(from != nullptr && to != nullptr,
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())
258 size_t matchEnd = matchPos + matchLength;
259 if (bWholeWords)
261 if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
262 && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
264 matchPos = input.find(from, matchPos + 1);
265 continue;
269 result.append(input, inputPos, matchPos - inputPos);
270 result.append(to);
271 inputPos = matchEnd;
272 matchPos = input.find(from, inputPos);
274 result.append(input, inputPos, matchPos - inputPos);
275 return result;
278 } // namespace
280 std::string
281 replaceAll(const std::string &input, const char *from, const char *to)
283 return replaceInternal(input, from, to, false);
286 std::string
287 replaceAll(const std::string &input, const std::string &from,
288 const std::string &to)
290 return replaceInternal(input, from.c_str(), to.c_str(), false);
293 std::string
294 replaceAllWords(const std::string &input, const char *from, const char *to)
296 return replaceInternal(input, from, to, true);
299 std::string
300 replaceAllWords(const std::string &input, const std::string &from,
301 const std::string &to)
303 return replaceInternal(input, from.c_str(), to.c_str(), true);
307 /********************************************************************
308 * TextLineWrapperSettings
311 TextLineWrapperSettings::TextLineWrapperSettings()
312 : maxLength_(0), indent_(0), firstLineIndent_(-1),
313 bKeepFinalSpaces_(false), continuationChar_('\0')
318 /********************************************************************
319 * TextLineWrapper
322 bool TextLineWrapper::isTrivial() const
324 return settings_.lineLength() == 0 && settings_.indent() == 0
325 && settings_.firstLineIndent_ <= 0;
328 size_t
329 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
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)
336 lineStart += std::strspn(input + lineStart, " ");
337 if (lineStart >= inputLength)
339 return inputLength;
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;
353 const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
354 size_t nextBreak
355 = (nextBreakPtr != nullptr ? nextBreakPtr - input : inputLength);
356 if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
358 break;
360 lineEnd = nextBreak + 1;
362 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
363 return (lineEnd < inputLength ? lineEnd : inputLength);
366 size_t
367 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
369 return findNextLine(input.c_str(), lineStart);
372 std::string
373 TextLineWrapper::formatLine(const std::string &input,
374 size_t lineStart, size_t lineEnd) const
376 size_t inputLength = input.length();
377 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
378 // Strip leading whitespace if necessary.
379 if (!bFirstLine)
381 lineStart = input.find_first_not_of(' ', lineStart);
382 if (lineStart >= inputLength)
384 return std::string();
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')
392 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
394 --lineEnd;
398 const size_t lineLength = lineEnd - lineStart;
399 if (lineLength == 0)
401 return std::string();
403 std::string result(indent, ' ');
404 result.append(input, lineStart, lineLength);
405 if (bContinuation && settings_.continuationChar_ != '\0')
407 result.append(1, ' ');
408 result.append(1, settings_.continuationChar_);
410 return result;
413 std::string
414 TextLineWrapper::wrapToString(const std::string &input) const
416 std::string result;
417 size_t lineStart = 0;
418 size_t length = input.length();
419 while (lineStart < length)
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'))
426 result.append("\n");
428 lineStart = nextLineStart;
430 return result;
433 std::vector<std::string>
434 TextLineWrapper::wrapToVector(const std::string &input) const
436 std::vector<std::string> result;
437 size_t lineStart = 0;
438 size_t length = input.length();
439 while (lineStart < length)
441 size_t nextLineStart = findNextLine(input, lineStart);
442 result.push_back(formatLine(input, lineStart, nextLineStart));
443 lineStart = nextLineStart;
445 return result;
448 } // namespace gmx