add join_words() utility function

This commit is contained in:
Axel Kohlmeyer
2022-07-04 18:26:59 -04:00
parent e13027787b
commit de08609634
3 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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.

View File

@ -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,