Use v.data() instead of &v[0] for C interop
[gromacs.git] / src / gromacs / utility / stringutil.cpp
blobcc3b1bfa3b9928779c80ce1dd4f21360730453ee
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2011,2012,2013,2014,2015, 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 != NULL)
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 char staticBuf[1024];
138 int length = 1024;
139 std::vector<char> dynamicBuf;
140 char *buf = staticBuf;
142 // TODO: There may be a better way of doing this on Windows, Microsoft
143 // provides their own way of doing things...
144 while (1)
146 va_start(ap, fmt);
147 int n = vsnprintf(buf, length, fmt, ap);
148 va_end(ap);
149 if (n > -1 && n < length)
151 std::string result(buf);
152 return result;
154 if (n > -1)
156 length = n + 1;
158 else
160 length *= 2;
162 dynamicBuf.resize(length);
163 buf = dynamicBuf.data();
167 std::vector<std::string> splitString(const std::string &str)
169 std::vector<std::string> result;
170 std::string::const_iterator currPos = str.begin();
171 const std::string::const_iterator end = str.end();
172 while (currPos != end)
174 while (currPos != end && std::isspace(*currPos))
176 ++currPos;
178 const std::string::const_iterator startPos = currPos;
179 while (currPos != end && !std::isspace(*currPos))
181 ++currPos;
183 if (startPos != end)
185 result.push_back(std::string(startPos, currPos));
188 return result;
191 namespace
194 /*! \brief
195 * Helper function to identify word boundaries for replaceAllWords().
197 * \returns `true` if the character is considered part of a word.
199 * \ingroup module_utility
201 bool isWordChar(char c)
203 return std::isalnum(c) || c == '-' || c == '_';
206 /*! \brief
207 * Common implementation for string replacement functions.
209 * \param[in] input Input string.
210 * \param[in] from String to find.
211 * \param[in] to String to use to replace \p from.
212 * \param[in] bWholeWords Whether to only consider matches to whole words.
213 * \returns \p input with all occurrences of \p from replaced with \p to.
214 * \throws std::bad_alloc if out of memory.
216 * \ingroup module_utility
218 std::string
219 replaceInternal(const std::string &input, const char *from, const char *to,
220 bool bWholeWords)
222 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
223 "Replacement strings must not be NULL");
224 size_t matchLength = std::strlen(from);
225 std::string result;
226 size_t inputPos = 0;
227 size_t matchPos = input.find(from);
228 while (matchPos < input.length())
230 size_t matchEnd = matchPos + matchLength;
231 if (bWholeWords)
233 if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
234 && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
236 matchPos = input.find(from, matchPos + 1);
237 continue;
241 result.append(input, inputPos, matchPos - inputPos);
242 result.append(to);
243 inputPos = matchEnd;
244 matchPos = input.find(from, inputPos);
246 result.append(input, inputPos, matchPos - inputPos);
247 return result;
250 } // namespace
252 std::string
253 replaceAll(const std::string &input, const char *from, const char *to)
255 return replaceInternal(input, from, to, false);
258 std::string
259 replaceAll(const std::string &input, const std::string &from,
260 const std::string &to)
262 return replaceInternal(input, from.c_str(), to.c_str(), false);
265 std::string
266 replaceAllWords(const std::string &input, const char *from, const char *to)
268 return replaceInternal(input, from, to, true);
271 std::string
272 replaceAllWords(const std::string &input, const std::string &from,
273 const std::string &to)
275 return replaceInternal(input, from.c_str(), to.c_str(), true);
279 /********************************************************************
280 * TextLineWrapperSettings
283 TextLineWrapperSettings::TextLineWrapperSettings()
284 : maxLength_(0), indent_(0), firstLineIndent_(-1),
285 bKeepFinalSpaces_(false), continuationChar_('\0')
290 /********************************************************************
291 * TextLineWrapper
294 bool TextLineWrapper::isTrivial() const
296 return settings_.lineLength() == 0 && settings_.indent() == 0
297 && settings_.firstLineIndent_ <= 0;
300 size_t
301 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
303 size_t inputLength = std::strlen(input);
304 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
305 // Ignore leading whitespace if necessary.
306 if (!bFirstLine)
308 lineStart += std::strspn(input + lineStart, " ");
309 if (lineStart >= inputLength)
311 return inputLength;
315 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
316 size_t lastAllowedBreakPoint
317 = (settings_.lineLength() > 0
318 ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
319 : inputLength);
320 // Ignore trailing whitespace.
321 lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
322 size_t lineEnd = lineStart;
325 const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
326 size_t nextBreak
327 = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
328 if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
330 break;
332 lineEnd = nextBreak + 1;
334 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
335 return (lineEnd < inputLength ? lineEnd : inputLength);
338 size_t
339 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
341 return findNextLine(input.c_str(), lineStart);
344 std::string
345 TextLineWrapper::formatLine(const std::string &input,
346 size_t lineStart, size_t lineEnd) const
348 size_t inputLength = input.length();
349 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
350 // Strip leading whitespace if necessary.
351 if (!bFirstLine)
353 lineStart = input.find_first_not_of(' ', lineStart);
354 if (lineStart >= inputLength)
356 return std::string();
359 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
360 bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
361 // Strip trailing whitespace.
362 if (!settings_.bKeepFinalSpaces_ || lineEnd < inputLength || input[inputLength - 1] == '\n')
364 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
366 --lineEnd;
370 const size_t lineLength = lineEnd - lineStart;
371 if (lineLength == 0)
373 return std::string();
375 std::string result(indent, ' ');
376 result.append(input, lineStart, lineLength);
377 if (bContinuation && settings_.continuationChar_ != '\0')
379 result.append(1, ' ');
380 result.append(1, settings_.continuationChar_);
382 return result;
385 std::string
386 TextLineWrapper::wrapToString(const std::string &input) const
388 std::string result;
389 size_t lineStart = 0;
390 size_t length = input.length();
391 while (lineStart < length)
393 size_t nextLineStart = findNextLine(input, lineStart);
394 result.append(formatLine(input, lineStart, nextLineStart));
395 if (nextLineStart < length
396 || (nextLineStart == length && input[length - 1] == '\n'))
398 result.append("\n");
400 lineStart = nextLineStart;
402 return result;
405 std::vector<std::string>
406 TextLineWrapper::wrapToVector(const std::string &input) const
408 std::vector<std::string> result;
409 size_t lineStart = 0;
410 size_t length = input.length();
411 while (lineStart < length)
413 size_t nextLineStart = findNextLine(input, lineStart);
414 result.push_back(formatLine(input, lineStart, nextLineStart));
415 lineStart = nextLineStart;
417 return result;
420 } // namespace gmx