[{"id":4049,"web_url":"https://patchwork.libcamera.org/comment/4049/","msgid":"<20200317120659.mggk5sujvmrax2ko@uno.localdomain>","date":"2020-03-17T12:06:59","subject":"Re: [libcamera-devel] [PATCH v2 02/10] libcamera: utils: Add string\n\tjoin function","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent,\n\nOn Mon, Mar 16, 2020 at 11:43:02PM +0200, Laurent Pinchart wrote:\n> Add a utils::join() function to join elements of a container into a\n> string, with a separator and an optional conversion function if the\n> elements are not implicitly convertible to std::string.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/libcamera/include/utils.h | 44 +++++++++++++++++++++++++++++++++++\n>  src/libcamera/utils.cpp       | 16 +++++++++++++\n>  test/utils.cpp                |  7 +++++-\n>  3 files changed, 66 insertions(+), 1 deletion(-)\n>\n> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> index 940597760ee2..74ceed760cb3 100644\n> --- a/src/libcamera/include/utils.h\n> +++ b/src/libcamera/include/utils.h\n> @@ -11,6 +11,7 @@\n>  #include <chrono>\n>  #include <memory>\n>  #include <ostream>\n> +#include <sstream>\n>  #include <string>\n>  #include <string.h>\n>  #include <sys/time.h>\n> @@ -109,6 +110,49 @@ inline _hex hex<uint64_t>(uint64_t value, unsigned int width)\n>\n>  size_t strlcpy(char *dst, const char *src, size_t size);\n>\n> +#ifndef __DOXYGEN__\n> +template<typename Container, typename UnaryOp>\n> +std::string join(const Container &items, const std::string &sep, UnaryOp op)\n> +{\n> +\tstd::ostringstream ss;\n> +\tbool first = true;\n> +\n> +\tfor (typename Container::const_iterator it = std::begin(items);\n> +\t     it != std::end(items); ++it) {\n> +\t\tif (!first)\n> +\t\t\tss << sep;\n> +\t\telse\n> +\t\t\tfirst = false;\n> +\n> +\t\tss << op(*it);\n> +\t}\n> +\n> +\treturn ss.str();\n> +}\n> +\n> +template<typename Container>\n> +std::string join(const Container &items, const std::string &sep)\n> +{\n> +\tstd::ostringstream ss;\n> +\tbool first = true;\n> +\n> +\tfor (typename Container::const_iterator it = std::begin(items);\n> +\t     it != std::end(items); ++it) {\n> +\t\tif (!first)\n> +\t\t\tss << sep;\n> +\t\telse\n> +\t\t\tfirst = false;\n> +\n> +\t\tss << *it;\n> +\t}\n> +\n> +\treturn ss.str();\n> +}\n> +#else\n> +template<typename Container, typename UnaryOp>\n> +std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);\n\nWhat does thi give us ?\n\nop is defaulted to nullptr, but it requires the template argument to\nbe specified anyway...\n\nApart from this\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nThanks\n  j\n\n> +#endif\n> +\n>  namespace details {\n>\n>  class StringSplitter\n> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> index f566e88cec5b..1d0e583756cf 100644\n> --- a/src/libcamera/utils.cpp\n> +++ b/src/libcamera/utils.cpp\n> @@ -291,6 +291,22 @@ details::StringSplitter::iterator details::StringSplitter::end() const\n>  \treturn iterator(this, std::string::npos);\n>  }\n>\n> +/**\n> + * \\fn template<typename Container, typename UnaryOp> \\\n> + * std::string utils::join(const Container &items, const std::string &sep, UnaryOp op)\n> + * \\brief Join elements of a container in a string with a separator\n> + * \\param[in] items The container\n> + * \\param[in] sep The separator to add between elements\n> + * \\param[in] op A function that converts individual elements to strings\n> + *\n> + * This function joins all elements in the \\a items container into a string and\n> + * returns it. The \\a sep separator is added between elements. If the container\n> + * elements are not implicitly convertible to std::string, the \\a op function\n> + * shall be provided to perform conversion of elements to std::string.\n> + *\n> + * \\return A string that concatenates all elements in the container\n> + */\n> +\n>  /**\n>   * \\fn split(const std::string &str, const std::string &delim)\n>   * \\brief Split a string based on a delimiter\n> diff --git a/test/utils.cpp b/test/utils.cpp\n> index 58816f153066..2fca89ef3278 100644\n> --- a/test/utils.cpp\n> +++ b/test/utils.cpp\n> @@ -99,7 +99,7 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>\n> -\t\t/* utils::split() test. */\n> +\t\t/* utils::join() and utils::split() test. */\n>  \t\tstd::vector<std::string> elements = {\n>  \t\t\t\"/bin\",\n>  \t\t\t\"/usr/bin\",\n> @@ -111,6 +111,11 @@ protected:\n>  \t\tfor (const auto &element : elements)\n>  \t\t\tpath += (path.empty() ? \"\" : \":\") + element;\n>\n> +\t\tif (path != utils::join(elements, \":\")) {\n> +\t\t\tcerr << \"utils::join() test failed\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n>  \t\tstd::vector<std::string> dirs;\n>\n>  \t\tfor (const auto &dir : utils::split(path, \":\"))\n> --\n> Regards,\n>\n> Laurent Pinchart\n>\n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay10.mail.gandi.net (relay10.mail.gandi.net\n\t[217.70.178.230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3B95762923\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Mar 2020 13:04:07 +0100 (CET)","from uno.localdomain (2-224-242-101.ip172.fastwebnet.it\n\t[2.224.242.101]) (Authenticated sender: jacopo@jmondi.org)\n\tby relay10.mail.gandi.net (Postfix) with ESMTPSA id 02E6E240003;\n\tTue, 17 Mar 2020 12:04:03 +0000 (UTC)"],"Date":"Tue, 17 Mar 2020 13:06:59 +0100","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, Martijn Braam <martijn@brixit.nl>, \n\tMickael GUENE <mickael.guene@st.com>,\n\tBenjamin GAIGNARD <benjamin.gaignard@st.com>","Message-ID":"<20200317120659.mggk5sujvmrax2ko@uno.localdomain>","References":"<20200316214310.27665-1-laurent.pinchart@ideasonboard.com>\n\t<20200316214310.27665-3-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200316214310.27665-3-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 02/10] libcamera: utils: Add string\n\tjoin function","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Tue, 17 Mar 2020 12:04:07 -0000"}},{"id":4054,"web_url":"https://patchwork.libcamera.org/comment/4054/","msgid":"<20200317200548.GE2527@pendragon.ideasonboard.com>","date":"2020-03-17T20:05:48","subject":"Re: [libcamera-devel] [PATCH v2 02/10] libcamera: utils: Add string\n\tjoin function","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Tue, Mar 17, 2020 at 01:06:59PM +0100, Jacopo Mondi wrote:\n> On Mon, Mar 16, 2020 at 11:43:02PM +0200, Laurent Pinchart wrote:\n> > Add a utils::join() function to join elements of a container into a\n> > string, with a separator and an optional conversion function if the\n> > elements are not implicitly convertible to std::string.\n> >\n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> > ---\n> >  src/libcamera/include/utils.h | 44 +++++++++++++++++++++++++++++++++++\n> >  src/libcamera/utils.cpp       | 16 +++++++++++++\n> >  test/utils.cpp                |  7 +++++-\n> >  3 files changed, 66 insertions(+), 1 deletion(-)\n> >\n> > diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> > index 940597760ee2..74ceed760cb3 100644\n> > --- a/src/libcamera/include/utils.h\n> > +++ b/src/libcamera/include/utils.h\n> > @@ -11,6 +11,7 @@\n> >  #include <chrono>\n> >  #include <memory>\n> >  #include <ostream>\n> > +#include <sstream>\n> >  #include <string>\n> >  #include <string.h>\n> >  #include <sys/time.h>\n> > @@ -109,6 +110,49 @@ inline _hex hex<uint64_t>(uint64_t value, unsigned int width)\n> >\n> >  size_t strlcpy(char *dst, const char *src, size_t size);\n> >\n> > +#ifndef __DOXYGEN__\n> > +template<typename Container, typename UnaryOp>\n> > +std::string join(const Container &items, const std::string &sep, UnaryOp op)\n> > +{\n> > +\tstd::ostringstream ss;\n> > +\tbool first = true;\n> > +\n> > +\tfor (typename Container::const_iterator it = std::begin(items);\n> > +\t     it != std::end(items); ++it) {\n> > +\t\tif (!first)\n> > +\t\t\tss << sep;\n> > +\t\telse\n> > +\t\t\tfirst = false;\n> > +\n> > +\t\tss << op(*it);\n> > +\t}\n> > +\n> > +\treturn ss.str();\n> > +}\n> > +\n> > +template<typename Container>\n> > +std::string join(const Container &items, const std::string &sep)\n> > +{\n> > +\tstd::ostringstream ss;\n> > +\tbool first = true;\n> > +\n> > +\tfor (typename Container::const_iterator it = std::begin(items);\n> > +\t     it != std::end(items); ++it) {\n> > +\t\tif (!first)\n> > +\t\t\tss << sep;\n> > +\t\telse\n> > +\t\t\tfirst = false;\n> > +\n> > +\t\tss << *it;\n> > +\t}\n> > +\n> > +\treturn ss.str();\n> > +}\n> > +#else\n> > +template<typename Container, typename UnaryOp>\n> > +std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);\n> \n> What does thi give us ?\n> \n> op is defaulted to nullptr, but it requires the template argument to\n> be specified anyway...\n\nThis is for doxygen only, in order to avoid documenting two functions\nthat are otherwise identical. I haven't found a way to merge both\nimplementations into a single template function that would make the\ncompiler happy.\n\n> Apart from this\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> \n> > +#endif\n> > +\n> >  namespace details {\n> >\n> >  class StringSplitter\n> > diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> > index f566e88cec5b..1d0e583756cf 100644\n> > --- a/src/libcamera/utils.cpp\n> > +++ b/src/libcamera/utils.cpp\n> > @@ -291,6 +291,22 @@ details::StringSplitter::iterator details::StringSplitter::end() const\n> >  \treturn iterator(this, std::string::npos);\n> >  }\n> >\n> > +/**\n> > + * \\fn template<typename Container, typename UnaryOp> \\\n> > + * std::string utils::join(const Container &items, const std::string &sep, UnaryOp op)\n> > + * \\brief Join elements of a container in a string with a separator\n> > + * \\param[in] items The container\n> > + * \\param[in] sep The separator to add between elements\n> > + * \\param[in] op A function that converts individual elements to strings\n> > + *\n> > + * This function joins all elements in the \\a items container into a string and\n> > + * returns it. The \\a sep separator is added between elements. If the container\n> > + * elements are not implicitly convertible to std::string, the \\a op function\n> > + * shall be provided to perform conversion of elements to std::string.\n> > + *\n> > + * \\return A string that concatenates all elements in the container\n> > + */\n> > +\n> >  /**\n> >   * \\fn split(const std::string &str, const std::string &delim)\n> >   * \\brief Split a string based on a delimiter\n> > diff --git a/test/utils.cpp b/test/utils.cpp\n> > index 58816f153066..2fca89ef3278 100644\n> > --- a/test/utils.cpp\n> > +++ b/test/utils.cpp\n> > @@ -99,7 +99,7 @@ protected:\n> >  \t\t\treturn TestFail;\n> >  \t\t}\n> >\n> > -\t\t/* utils::split() test. */\n> > +\t\t/* utils::join() and utils::split() test. */\n> >  \t\tstd::vector<std::string> elements = {\n> >  \t\t\t\"/bin\",\n> >  \t\t\t\"/usr/bin\",\n> > @@ -111,6 +111,11 @@ protected:\n> >  \t\tfor (const auto &element : elements)\n> >  \t\t\tpath += (path.empty() ? \"\" : \":\") + element;\n> >\n> > +\t\tif (path != utils::join(elements, \":\")) {\n> > +\t\t\tcerr << \"utils::join() test failed\" << endl;\n> > +\t\t\treturn TestFail;\n> > +\t\t}\n> > +\n> >  \t\tstd::vector<std::string> dirs;\n> >\n> >  \t\tfor (const auto &dir : utils::split(path, \":\"))","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5E41962923\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Mar 2020 21:05:54 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BA213F9;\n\tTue, 17 Mar 2020 21:05:53 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1584475553;\n\tbh=Rnsz3UgGVZrEFJQs91VAk1AEdYvh5sMQdSi7nXzqwbY=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ZNstrPgFufMc2h4zhHJS7KSncW7KZRKxIHKPm8BCPLdxgESJkiJ5rJEae15PEWQjn\n\tkkYtKwoWC/VM2yc1WRbtTEKg7bGvkb9yCM1jjNl4VJNszkEr1/vGd3Cw+vtQHacWJb\n\t7DY5hvwJf4penHPfWzybcmdY8LUZnGTbBEv3eC1o=","Date":"Tue, 17 Mar 2020 22:05:48 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org, Martijn Braam <martijn@brixit.nl>, \n\tMickael GUENE <mickael.guene@st.com>,\n\tBenjamin GAIGNARD <benjamin.gaignard@st.com>","Message-ID":"<20200317200548.GE2527@pendragon.ideasonboard.com>","References":"<20200316214310.27665-1-laurent.pinchart@ideasonboard.com>\n\t<20200316214310.27665-3-laurent.pinchart@ideasonboard.com>\n\t<20200317120659.mggk5sujvmrax2ko@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200317120659.mggk5sujvmrax2ko@uno.localdomain>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v2 02/10] libcamera: utils: Add string\n\tjoin function","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Tue, 17 Mar 2020 20:05:54 -0000"}}]