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.
37 * Implements functions and classes in stringutil.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
44 #include "stringutil.h"
55 #include "gromacs/utility/gmxassert.h"
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
]))
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.
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
))
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
)
110 size_t suffixLength
= std::strlen(suffix
);
111 if (suffixLength
> 0 && endsWith(str
, suffix
))
113 return str
.substr(0, str
.length() - suffixLength
);
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
))
127 while (start
!= end
&& std::isspace(*(end
- 1)))
131 return std::string(start
, end
);
134 std::string
formatString(const char *fmt
, ...)
137 char staticBuf
[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...
147 int n
= vsnprintf(buf
, length
, fmt
, ap
);
149 if (n
> -1 && n
< length
)
151 std::string
result(buf
);
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
))
178 const std::string::const_iterator startPos
= currPos
;
179 while (currPos
!= end
&& !std::isspace(*currPos
))
185 result
.push_back(std::string(startPos
, currPos
));
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
== '_';
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
219 replaceInternal(const std::string
&input
, const char *from
, const char *to
,
222 GMX_RELEASE_ASSERT(from
!= NULL
&& to
!= NULL
,
223 "Replacement strings must not be NULL");
224 size_t matchLength
= std::strlen(from
);
227 size_t matchPos
= input
.find(from
);
228 while (matchPos
< input
.length())
230 size_t matchEnd
= matchPos
+ matchLength
;
233 if (!((matchPos
== 0 || !isWordChar(input
[matchPos
-1]))
234 && (matchEnd
== input
.length() || !isWordChar(input
[matchEnd
]))))
236 matchPos
= input
.find(from
, matchPos
+ 1);
241 result
.append(input
, inputPos
, matchPos
- inputPos
);
244 matchPos
= input
.find(from
, inputPos
);
246 result
.append(input
, inputPos
, matchPos
- inputPos
);
253 replaceAll(const std::string
&input
, const char *from
, const char *to
)
255 return replaceInternal(input
, from
, to
, false);
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);
266 replaceAllWords(const std::string
&input
, const char *from
, const char *to
)
268 return replaceInternal(input
, from
, to
, true);
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 /********************************************************************
294 bool TextLineWrapper::isTrivial() const
296 return settings_
.lineLength() == 0 && settings_
.indent() == 0
297 && settings_
.firstLineIndent_
<= 0;
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.
308 lineStart
+= std::strspn(input
+ lineStart
, " ");
309 if (lineStart
>= 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
)
320 // Ignore trailing whitespace.
321 lastAllowedBreakPoint
+= std::strspn(input
+ lastAllowedBreakPoint
, " ");
322 size_t lineEnd
= lineStart
;
325 const char *nextBreakPtr
= std::strpbrk(input
+ lineEnd
, " \n");
327 = (nextBreakPtr
!= NULL
? nextBreakPtr
- input
: inputLength
);
328 if (nextBreak
> lastAllowedBreakPoint
&& lineEnd
> lineStart
)
332 lineEnd
= nextBreak
+ 1;
334 while (lineEnd
< lastAllowedBreakPoint
&& input
[lineEnd
- 1] != '\n');
335 return (lineEnd
< inputLength
? lineEnd
: inputLength
);
339 TextLineWrapper::findNextLine(const std::string
&input
, size_t lineStart
) const
341 return findNextLine(input
.c_str(), lineStart
);
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.
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]))
370 const size_t lineLength
= lineEnd
- lineStart
;
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_
);
386 TextLineWrapper::wrapToString(const std::string
&input
) const
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'))
400 lineStart
= nextLineStart
;
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
;