Move remaining C files in utility to C++
[gromacs.git] / src / gromacs / utility / stringutil.cpp
blob54b32214926a49b5cd98658a72dbbbc0fe5f284b
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 bool endsWith(const char *str, const char *suffix)
62 if (isNullOrEmpty(suffix))
64 return true;
66 const size_t strLength = std::strlen(str);
67 const size_t suffixLength = std::strlen(suffix);
68 return (strLength >= suffixLength
69 && std::strcmp(&str[strLength - suffixLength], suffix) == 0);
72 std::string stripSuffixIfPresent(const std::string &str, const char *suffix)
74 if (suffix != NULL)
76 size_t suffixLength = std::strlen(suffix);
77 if (suffixLength > 0 && endsWith(str, suffix))
79 return str.substr(0, str.length() - suffixLength);
82 return str;
85 std::string stripString(const std::string &str)
87 std::string::const_iterator start = str.begin();
88 std::string::const_iterator end = str.end();
89 while (start != end && std::isspace(*start))
91 ++start;
93 while (start != end && std::isspace(*(end - 1)))
95 --end;
97 return std::string(start, end);
100 std::string formatString(const char *fmt, ...)
102 va_list ap;
103 char staticBuf[1024];
104 int length = 1024;
105 std::vector<char> dynamicBuf;
106 char *buf = staticBuf;
108 // TODO: There may be a better way of doing this on Windows, Microsoft
109 // provides their own way of doing things...
110 while (1)
112 va_start(ap, fmt);
113 int n = vsnprintf(buf, length, fmt, ap);
114 va_end(ap);
115 if (n > -1 && n < length)
117 std::string result(buf);
118 return result;
120 if (n > -1)
122 length = n + 1;
124 else
126 length *= 2;
128 dynamicBuf.resize(length);
129 buf = &dynamicBuf[0];
133 std::vector<std::string> splitString(const std::string &str)
135 std::vector<std::string> result;
136 std::string::const_iterator currPos = str.begin();
137 const std::string::const_iterator end = str.end();
138 while (currPos != end)
140 while (currPos != end && std::isspace(*currPos))
142 ++currPos;
144 const std::string::const_iterator startPos = currPos;
145 while (currPos != end && !std::isspace(*currPos))
147 ++currPos;
149 if (startPos != end)
151 result.push_back(std::string(startPos, currPos));
154 return result;
157 namespace
160 /*! \brief
161 * Helper function to identify word boundaries for replaceAllWords().
163 * \returns `true` if the character is considered part of a word.
165 * \ingroup module_utility
167 bool isWordChar(char c)
169 return std::isalnum(c) || c == '-' || c == '_';
172 /*! \brief
173 * Common implementation for string replacement functions.
175 * \param[in] input Input string.
176 * \param[in] from String to find.
177 * \param[in] to String to use to replace \p from.
178 * \param[in] bWholeWords Whether to only consider matches to whole words.
179 * \returns \p input with all occurrences of \p from replaced with \p to.
180 * \throws std::bad_alloc if out of memory.
182 * \ingroup module_utility
184 std::string
185 replaceInternal(const std::string &input, const char *from, const char *to,
186 bool bWholeWords)
188 GMX_RELEASE_ASSERT(from != NULL && to != NULL,
189 "Replacement strings must not be NULL");
190 size_t matchLength = std::strlen(from);
191 std::string result;
192 size_t inputPos = 0;
193 size_t matchPos = input.find(from);
194 while (matchPos < input.length())
196 size_t matchEnd = matchPos + matchLength;
197 if (bWholeWords)
199 if (!((matchPos == 0 || !isWordChar(input[matchPos-1]))
200 && (matchEnd == input.length() || !isWordChar(input[matchEnd]))))
202 matchPos = input.find(from, matchPos + 1);
203 continue;
207 result.append(input, inputPos, matchPos - inputPos);
208 result.append(to);
209 inputPos = matchEnd;
210 matchPos = input.find(from, inputPos);
212 result.append(input, inputPos, matchPos - inputPos);
213 return result;
216 } // namespace
218 std::string
219 replaceAll(const std::string &input, const char *from, const char *to)
221 return replaceInternal(input, from, to, false);
224 std::string
225 replaceAll(const std::string &input, const std::string &from,
226 const std::string &to)
228 return replaceInternal(input, from.c_str(), to.c_str(), false);
231 std::string
232 replaceAllWords(const std::string &input, const char *from, const char *to)
234 return replaceInternal(input, from, to, true);
237 std::string
238 replaceAllWords(const std::string &input, const std::string &from,
239 const std::string &to)
241 return replaceInternal(input, from.c_str(), to.c_str(), true);
245 /********************************************************************
246 * TextLineWrapperSettings
249 TextLineWrapperSettings::TextLineWrapperSettings()
250 : maxLength_(0), indent_(0), firstLineIndent_(-1),
251 bKeepFinalSpaces_(false), continuationChar_('\0')
256 /********************************************************************
257 * TextLineWrapper
260 bool TextLineWrapper::isTrivial() const
262 return settings_.lineLength() == 0 && settings_.indent() == 0
263 && settings_.firstLineIndent_ <= 0;
266 size_t
267 TextLineWrapper::findNextLine(const char *input, size_t lineStart) const
269 size_t inputLength = std::strlen(input);
270 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
271 // Ignore leading whitespace if necessary.
272 if (!bFirstLine)
274 lineStart += std::strspn(input + lineStart, " ");
275 if (lineStart >= inputLength)
277 return inputLength;
281 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
282 size_t lastAllowedBreakPoint
283 = (settings_.lineLength() > 0
284 ? std::min(lineStart + settings_.lineLength() - indent, inputLength)
285 : inputLength);
286 // Ignore trailing whitespace.
287 lastAllowedBreakPoint += std::strspn(input + lastAllowedBreakPoint, " ");
288 size_t lineEnd = lineStart;
291 const char *nextBreakPtr = std::strpbrk(input + lineEnd, " \n");
292 size_t nextBreak
293 = (nextBreakPtr != NULL ? nextBreakPtr - input : inputLength);
294 if (nextBreak > lastAllowedBreakPoint && lineEnd > lineStart)
296 break;
298 lineEnd = nextBreak + 1;
300 while (lineEnd < lastAllowedBreakPoint && input[lineEnd - 1] != '\n');
301 return (lineEnd < inputLength ? lineEnd : inputLength);
304 size_t
305 TextLineWrapper::findNextLine(const std::string &input, size_t lineStart) const
307 return findNextLine(input.c_str(), lineStart);
310 std::string
311 TextLineWrapper::formatLine(const std::string &input,
312 size_t lineStart, size_t lineEnd) const
314 size_t inputLength = input.length();
315 bool bFirstLine = (lineStart == 0 || input[lineStart - 1] == '\n');
316 // Strip leading whitespace if necessary.
317 if (!bFirstLine)
319 lineStart = input.find_first_not_of(' ', lineStart);
320 if (lineStart >= inputLength)
322 return std::string();
325 int indent = (bFirstLine ? settings_.firstLineIndent() : settings_.indent());
326 bool bContinuation = (lineEnd < inputLength && input[lineEnd - 1] != '\n');
327 // Strip trailing whitespace.
328 if (!settings_.bKeepFinalSpaces_ || lineEnd < inputLength || input[inputLength - 1] == '\n')
330 while (lineEnd > lineStart && std::isspace(input[lineEnd - 1]))
332 --lineEnd;
336 const size_t lineLength = lineEnd - lineStart;
337 if (lineLength == 0)
339 return std::string();
341 std::string result(indent, ' ');
342 result.append(input, lineStart, lineLength);
343 if (bContinuation && settings_.continuationChar_ != '\0')
345 result.append(1, ' ');
346 result.append(1, settings_.continuationChar_);
348 return result;
351 std::string
352 TextLineWrapper::wrapToString(const std::string &input) const
354 std::string result;
355 size_t lineStart = 0;
356 size_t length = input.length();
357 while (lineStart < length)
359 size_t nextLineStart = findNextLine(input, lineStart);
360 result.append(formatLine(input, lineStart, nextLineStart));
361 if (nextLineStart < length
362 || (nextLineStart == length && input[length - 1] == '\n'))
364 result.append("\n");
366 lineStart = nextLineStart;
368 return result;
371 std::vector<std::string>
372 TextLineWrapper::wrapToVector(const std::string &input) const
374 std::vector<std::string> result;
375 size_t lineStart = 0;
376 size_t length = input.length();
377 while (lineStart < length)
379 size_t nextLineStart = findNextLine(input, lineStart);
380 result.push_back(formatLine(input, lineStart, nextLineStart));
381 lineStart = nextLineStart;
383 return result;
386 } // namespace gmx