diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index 0932276a58..43f12ef377 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -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 diff --git a/src/utils.cpp b/src/utils.cpp index 87de538dfb..eb9e48985a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -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 &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. diff --git a/src/utils.h b/src/utils.h index 6726240d0d..a88244b694 100644 --- a/src/utils.h +++ b/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 &words, const std::string &sep); + /*! Take text and split into non-whitespace words. * * This can handle strings with single and double quotes, escaped quotes,