add join_words() utility function
This commit is contained in:
@ -154,6 +154,9 @@ and parsing files or arguments.
|
||||
.. doxygenfunction:: trim_and_count_words
|
||||
:project: progguide
|
||||
|
||||
.. doxygenfunction:: join_words
|
||||
:project: progguide
|
||||
|
||||
.. doxygenfunction:: split_words
|
||||
:project: progguide
|
||||
|
||||
|
||||
@ -984,6 +984,19 @@ size_t utils::trim_and_count_words(const std::string &text, const std::string &s
|
||||
return utils::count_words(trim_comment(text), separators);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
combine words in vector to single string with separator added between words
|
||||
------------------------------------------------------------------------- */
|
||||
std::string utils::join_words(const std::vector<std::string> &words, const std::string &sep)
|
||||
{
|
||||
std::string result;
|
||||
|
||||
if (words.size() > 0) result = words[0];
|
||||
for (std::size_t i = 1; i < words.size(); ++i) result += sep + words[i];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
Convert string into words on whitespace while handling single and
|
||||
double quotes.
|
||||
|
||||
11
src/utils.h
11
src/utils.h
@ -486,6 +486,17 @@ namespace utils {
|
||||
|
||||
size_t trim_and_count_words(const std::string &text, const std::string &separators = " \t\r\n\f");
|
||||
|
||||
/*! Take list of words and join them with a given separator text.
|
||||
*
|
||||
* This is the inverse operation of what the split_words() function
|
||||
* Tokenizer classes do.
|
||||
*
|
||||
* \param words STL vector with strings
|
||||
* \param sep separator string (may be empty)
|
||||
* \return string with the concatenated words and separators */
|
||||
|
||||
std::string join_words(const std::vector<std::string> &words, const std::string &sep);
|
||||
|
||||
/*! Take text and split into non-whitespace words.
|
||||
*
|
||||
* This can handle strings with single and double quotes, escaped quotes,
|
||||
|
||||
Reference in New Issue
Block a user