changeset 570:dcef88285f8c

Misc: add join function
author David Demelier <markand@malikania.fr>
date Wed, 29 Jun 2016 19:12:10 +0200
parents b1c4933afcd0
children c79a501782b0
files misc/join.hpp misc/test-all.cpp misc/test-join.cpp
diffstat 3 files changed, 113 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/join.hpp	Wed Jun 29 19:12:10 2016 +0200
@@ -0,0 +1,53 @@
+/*
+ * join.hpp -- join lists into a string
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/**
+ * Join values by a separator and return a string.
+ *
+ * \param first the first iterator
+ * \param last the last iterator
+ * \param delim the optional delimiter
+ * \return the string
+ */
+template <typename InputIt, typename DelimType = char>
+std::string join(InputIt first, InputIt last, DelimType delim = ':')
+{
+    std::ostringstream oss;
+
+    if (first != last) {
+        oss << *first;
+
+        while (++first != last)
+            oss << delim << *first;
+    }
+
+    return oss.str();
+}
+
+/**
+ * Convenient overload.
+ *
+ * \param list the initializer list
+ * \param delim the delimiter
+ * \return the string
+ */
+template <typename T, typename DelimType = char>
+inline std::string join(std::initializer_list<T> list, DelimType delim = ':')
+{
+    return join(list.begin(), list.end(), delim);
+}
\ No newline at end of file
--- a/misc/test-all.cpp	Wed Jun 29 19:07:40 2016 +0200
+++ b/misc/test-all.cpp	Wed Jun 29 19:12:10 2016 +0200
@@ -19,6 +19,7 @@
 #include <gtest/gtest.h>
 
 #include "test-clamp.cpp"
+#include "test-join.cpp"
 #include "test-strip.cpp"
 
 int main(int argc, char **argv)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/test-join.cpp	Wed Jun 29 19:12:10 2016 +0200
@@ -0,0 +1,59 @@
+/*
+ * test-join.cpp -- test join function
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "join.hpp"
+
+TEST(Join, empty)
+{
+    std::string expected = "";
+    std::string result = join<int>({});
+
+    ASSERT_EQ(expected, result);
+}
+
+TEST(Join, one)
+{
+    std::string expected = "1";
+    std::string result = join({1});
+
+    ASSERT_EQ(expected, result);
+}
+
+TEST(Join, two)
+{
+    std::string expected = "1:2";
+    std::string result = join({1, 2});
+
+    ASSERT_EQ(expected, result);
+}
+
+TEST(Join, delimiterString)
+{
+    std::string expected = "1;;2;;3";
+    std::string result = join({1, 2, 3}, ";;");
+
+    ASSERT_EQ(expected, result);
+}
+
+TEST(Join, delimiterChar)
+{
+    std::string expected = "1@2@3@4";
+    std::string result = join({1, 2, 3, 4}, '@');
+
+    ASSERT_EQ(expected, result);
+}
\ No newline at end of file