aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-09-21 21:36:50 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-09-21 21:36:50 +0000
commit0f48cefea53ccd11d68255c5d52cf620b121303b (patch)
tree4551564085703b37324b1b4a9daeb1c90a4099f9
parent367ff7ba37f97c4b639159ad4e558f1d1df13cfa (diff)
downloadpaludis-0f48cefea53ccd11d68255c5d52cf620b121303b.tar.gz
paludis-0f48cefea53ccd11d68255c5d52cf620b121303b.tar.xz
Allow join to use a function other than stringify
-rw-r--r--paludis/util/join.hh36
-rw-r--r--paludis/util/join_TEST.cc46
2 files changed, 80 insertions, 2 deletions
diff --git a/paludis/util/join.hh b/paludis/util/join.hh
index 47d54db..a2a399c 100644
--- a/paludis/util/join.hh
+++ b/paludis/util/join.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
+ * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -21,6 +21,7 @@
#define PALUDIS_GUARD_PALUDIS_JOIN_HH 1
#include <paludis/util/stringify.hh>
+#include <iterator>
#include <string>
/** \file
@@ -52,6 +53,27 @@ namespace paludis
}
/**
+ * Join together the items from i to end using joiner, using
+ * a function other than stringify.
+ *
+ * \ingroup grpjoin
+ */
+ template <typename I_, typename T_, typename F_>
+ T_ join(I_ i, I_ end, const T_ & joiner, const F_ & f)
+ {
+ T_ result;
+ if (i != end)
+ while (true)
+ {
+ result += (f)(*i);
+ if (++i == end)
+ break;
+ result += joiner;
+ }
+ return result;
+ }
+
+ /**
* Convenience alternative join allowing a char * to be used for a
* string.
*
@@ -62,6 +84,18 @@ namespace paludis
{
return join(begin, end, std::string(t));
}
+
+ /**
+ * Convenience alternative join allowing a char * to be used for a
+ * string, using a function other than stringify.
+ *
+ * \ingroup grpjoin
+ */
+ template <typename I_, typename F_>
+ std::string join(I_ begin, const I_ end, const char * const t, const F_ & f)
+ {
+ return join(begin, end, std::string(t), f);
+ }
}
#endif
diff --git a/paludis/util/join_TEST.cc b/paludis/util/join_TEST.cc
index 42eca4c..0661294 100644
--- a/paludis/util/join_TEST.cc
+++ b/paludis/util/join_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
+ * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -31,6 +31,14 @@ using namespace test;
*
*/
+namespace
+{
+ std::string make_purdy(const int x)
+ {
+ return "<" + stringify(x) + ">";
+ }
+}
+
namespace test_cases
{
/**
@@ -74,6 +82,26 @@ namespace test_cases
} test_join_list;
/**
+ * \test Test join on a list of ints.
+ *
+ */
+ struct JoinListIntTest : TestCase
+ {
+ JoinListIntTest() : TestCase("join list int") { }
+
+ void run()
+ {
+ std::list<int> v;
+ v.push_back(1);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/"), "1");
+ v.push_back(2);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/"), "1/2");
+ v.push_back(3);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/"), "1/2/3");
+ }
+ } test_join_list_int;
+
+ /**
* \test Test join with empty things.
*
*/
@@ -94,5 +122,21 @@ namespace test_cases
TEST_CHECK_EQUAL(join(v.begin(), v.end(), "*"), "*");
}
} test_join_empty;
+
+ struct JoinFunctionTest : TestCase
+ {
+ JoinFunctionTest() : TestCase("join function") { }
+
+ void run()
+ {
+ std::list<int> v;
+ v.push_back(1);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/", &make_purdy), "<1>");
+ v.push_back(2);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/", &make_purdy), "<1>/<2>");
+ v.push_back(3);
+ TEST_CHECK_EQUAL(join(v.begin(), v.end(), "/", &make_purdy), "<1>/<2>/<3>");
+ }
+ } test_join_function;
}