More fixes from clang-tidy-7
[gromacs.git] / src / gromacs / utility / stringutil.cpp
blobad0f963bf39cd9f966a309770583e68f5014914b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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 <sstream>
53 #include <string>
54 #include <vector>
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/gmxassert.h"
59 namespace gmx
62 std::size_t
63 countWords(const char *s)
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);
69 for (std::size_t i = 0; i < length; i++)
71 // If we found a new word, increase counter and step through the word
72 if (std::isalnum(s[i]))
74 ++nWords;
75 // If we hit string end, '\0' is not alphanumerical
76 while (std::isalnum(s[i]))
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++;
85 return nWords;
89 std::size_t
90 countWords(const std::string &str)
92 // Under out beautiful C++ interface hides an ugly c-string implementation :-)
93 return countWords(str.c_str());
96 bool endsWith(const char *str, const char *suffix)
98 if (isNullOrEmpty(suffix))
100 return true;
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);
108 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
110 if (suffix != nullptr)
112 size_t suffixLength = std::strlen(suffix);
113 if (suffixLength > 0 && endsWith(str, suffix))
115 return str.substr(0, str.length() - suffixLength);
118 return str;
121 std::string stripString(const std::string &str)
123 std::string::const_iterator start = str.begin();
124 std::string::const_iterator end = str.end();
125 while (start != end && std::isspace(*start))
127 ++start;
129 while (start != end && std::isspace(*(end - 1)))
131 --end;
133 return std::string(start, end);
136 std::string formatString(const char *fmt, ...)
138 va_list ap;
139 va_start(ap, fmt);
140 std::string result = formatStringV(fmt, ap);
141 va_end(ap);
142 return result;
145 std::string formatStringV(const char *fmt, va_list ap)
147 va_list ap_copy;
148 char staticBuf[1024];
149 int length = 1024;
150 std::vector<char> dynamicBuf;
151 char *buf = staticBuf;
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)
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)
162 std::string result(buf);
163 return result;
165 if (n > -1)
167 length = n + 1;
169 else
171 length *= 2;
173 dynamicBuf.resize(length);
174 buf = dynamicBuf.data();
178 std::vector<std::string> splitString(const std::string &str)
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)
185 while (currPos != end && std::isspace(*currPos))
187 ++currPos;
189 const std::string::const_iterator startPos = currPos;
190 while (currPos != end && !std::isspace(*currPos))
192 ++currPos;
194 if (startPos != end)
196 result.emplace_back(startPos, currPos);
199 return result;
202 std::vector<std::string> splitDelimitedString(const std::string &str, char delim)
204 std::vector<std::string> result;
205 size_t currPos = 0;
206 const size_t len = str.length();
207 if (len > 0)
209 size_t nextDelim;
212 nextDelim = str.find(delim, currPos);
213 result.push_back(str.substr(currPos, nextDelim - currPos));
214 currPos = nextDelim < len ? nextDelim + 1 : len;
216 while (currPos < len || nextDelim < len);
218 return result;
221 std::vector<std::string> splitAndTrimDelimitedString(const std::string &str, char delim)
223 std::vector<std::string> result;
225 result = splitDelimitedString(str, delim);
226 std::transform(result.begin(), result.end(), result.begin(), stripString);
227 return result;
230 namespace
233 /*! \brief
234 * Helper function to identify word boundaries for replaceAllWords().
236 * \returns `true` if the character is considered part of a word.
238 * \ingroup module_utility
240 bool isWordChar(char c)
242 return (std::isalnum(c) != 0) || c == '-' || c == '_';
245 /*! \brief
246 * Common implementation for string replacement functions.
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.
255 * \ingroup module_utility
257 std::string
258 replaceInternal(const std::string &input, const char *from, const char *to,
259 bool bWholeWords)
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())
269 size_t matchEnd = matchPos + matchLength;
270 if (bWholeWords)
272 if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
273 && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
275 matchPos = input.find(from, matchPos + 1);
276 continue;
280 result.append(input, inputPos, matchPos - inputPos);
281 result.append(to);
282 inputPos = matchEnd;
283 matchPos = input.find(from, inputPos);
285 result.append(input, inputPos, matchPos - inputPos);
286 return result;
289 } // namespace
291 std::string
292 replaceAll(const std::string &input, const char *from, const char *to)
294 return replaceInternal(input, from, to, false);
297 std::string
298 replaceAll(const std::string &input, const std::string &from,
299 const std::string &to)
301 return replaceInternal(input, from.c_str(), to.c_str(), false);
304 std::string
305 replaceAllWords(const std::string &input, const char *from, const char *to)
307 return replaceInternal(input, from, to, true);
310 std::string
311 replaceAllWords(const std::string &input, const std::string &from,
312 const std::string &to)
314 return replaceInternal(input, from.c_str(), to.c_str(), true);
318 /********************************************************************
319 * TextLineWrapperSettings
322 TextLineWrapperSettings::TextLineWrapperSettings()
323 : maxLength_(0), indent_(0), firstLineIndent_(-1),
324 bKeepFinalSpaces_(false), continuationChar_('\0')
329 /********************************************************************
330 * TextLineWrapper
333 bool TextLineWrapper::isTrivial() const
335 return settings_.lineLength() == 0 && settings_.indent() == 0
336 && settings_.firstLineIndent_ <= 0;
339 size_t
340 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
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)
347 lineStart += std::strspn(input + lineStart, " ");
348 if (lineStart >= inputLength)
350 return inputLength;
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;
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)
369 break;
371 lineEnd = nextBreak + 1;
373 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
374 return (lineEnd < inputLength ? lineEnd : inputLength);
377 size_t
378 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
380 return findNextLine(input.c_str(), lineStart);
383 std::string
384 TextLineWrapper::formatLine(const std::string &input,
385 size_t lineStart, size_t lineEnd) const
387 size_t inputLength = input.length();
388 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
389 // Strip leading whitespace if necessary.
390 if (!bFirstLine)
392 lineStart = input.find_first_not_of(' ', lineStart);
393 if (lineStart >= inputLength)
395 return std::string();
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')
404 --lineEnd;
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_)
410 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
412 --lineEnd;
416 const size_t lineLength = lineEnd - lineStart;
417 if (lineLength == 0)
419 return std::string();
421 std::string result(indent, ' ');
422 result.append(input, lineStart, lineLength);
423 if (bContinuation && settings_.continuationChar_ != '\0')
425 result.append(1, ' ');
426 result.append(1, settings_.continuationChar_);
428 return result;
431 std::string
432 TextLineWrapper::wrapToString(const std::string &input) const
434 std::string result;
435 size_t lineStart = 0;
436 size_t length = input.length();
437 while (lineStart < length)
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'))
444 result.append("\n");
446 lineStart = nextLineStart;
448 return result;
451 std::vector<std::string>
452 TextLineWrapper::wrapToVector(const std::string &input) const
454 std::vector<std::string> result;
455 size_t lineStart = 0;
456 size_t length = input.length();
457 while (lineStart < length)
459 size_t nextLineStart = findNextLine(input, lineStart);
460 result.push_back(formatLine(input, lineStart, nextLineStart));
461 lineStart = nextLineStart;
463 return result;
466 } // namespace gmx