Repository: mesos Updated Branches: refs/heads/master 5d46ce9b1 -> 38d8a6db9
Added variadic strings join. There is a second version of the variadic join which takes a reference to a stringstream as a parameter. This is handy when strings::join is just a part of a larger string manipulation. Review: https://reviews.apache.org/r/25789 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/38d8a6db Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/38d8a6db Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/38d8a6db Branch: refs/heads/master Commit: 38d8a6db9e4d86a8f57e3793ae940313ef2ea75e Parents: 5d46ce9 Author: Joris Van Remoortere <[email protected]> Authored: Thu Sep 25 12:35:03 2014 -0700 Committer: Niklas Q. Nielsen <[email protected]> Committed: Thu Sep 25 12:35:45 2014 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/strings.hpp | 109 +++++++++++++------ .../3rdparty/stout/tests/strings_tests.cpp | 17 +++ 2 files changed, 95 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/38d8a6db/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp index a1702cd..14567f1 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp @@ -15,6 +15,7 @@ #define __STOUT_STRINGS_HPP__ #include <algorithm> +#include <sstream> #include <string> #include <map> #include <vector> @@ -188,54 +189,100 @@ inline std::map<std::string, std::vector<std::string> > pairs( } -inline std::string join(const std::string& separator, - const std::string& s1, - const std::string& s2) +namespace internal { + +inline std::stringstream& append( + std::stringstream& stream, + const std::string& value) +{ + stream << value; + return stream; +} + + +inline std::stringstream& append( + std::stringstream& stream, + std::string&& value) +{ + stream << value; + return stream; +} + + +inline std::stringstream& append( + std::stringstream& stream, + const char*&& value) { - return s1 + separator + s2; + stream << value; + return stream; } -inline std::string join(const std::string& separator, - const std::string& s1, - const std::string& s2, - const std::string& s3) +template <typename T> +std::stringstream& append( + std::stringstream& stream, + T&& value) { - return s1 + separator + s2 + separator + s3; + stream << ::stringify(std::forward<T>(value)); + return stream; } -inline std::string join(const std::string& separator, - const std::string& s1, - const std::string& s2, - const std::string& s3, - const std::string& s4) +template <typename T> +std::stringstream& join( + std::stringstream& stream, + const std::string& separator, + T&& tail) { - return s1 + separator + s2 + separator + s3 + separator + s4; + return append(stream, std::forward<T>(tail)); } -inline std::string join(const std::string& separator, - const std::string& s1, - const std::string& s2, - const std::string& s3, - const std::string& s4, - const std::string& s5) +template <typename THead, typename ...TTail> +std::stringstream& join( + std::stringstream& stream, + const std::string& separator, + THead&& head, + TTail&&... tail) +{ + append(stream, std::forward<THead>(head)) << separator; + internal::join(stream, separator, std::forward<TTail>(tail)...); + return stream; +} + +} // namespace internal { + + +template <typename ...T> +std::stringstream& join( + std::stringstream& stream, + const std::string& separator, + T&&... args) { - return s1 + separator + s2 + separator + s3 + separator + s4 + separator + s5; + internal::join(stream, separator, std::forward<T>(args)...); + return stream; } -inline std::string join(const std::string& separator, - const std::string& s1, - const std::string& s2, - const std::string& s3, - const std::string& s4, - const std::string& s5, - const std::string& s6) +// Use 2 heads here to disambiguate variadic argument join from the +// templatized Iterable join below. This means this implementation of +// strings::join() is only activated if there are 2 or more things to +// join. +template <typename THead1, typename THead2, typename ...TTail> +std::string join( + const std::string& separator, + THead1&& head1, + THead2&& head2, + TTail&&... tail) { - return s1 + separator + s2 + separator + s3 + separator + s4 + separator + - s5 + separator + s6; + std::stringstream stream; + internal::join( + stream, + separator, + std::forward<THead1>(head1), + std::forward<THead2>(head2), + std::forward<TTail>(tail)...); + return stream.str(); } http://git-wip-us.apache.org/repos/asf/mesos/blob/38d8a6db/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp index 51008e5..b900579 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp @@ -359,6 +359,23 @@ TEST(StringsTest, Pairs) } +TEST(StringsTest, Join) +{ + EXPECT_EQ("a/b", strings::join("/", "a", "b")); + EXPECT_EQ("a/b/c", strings::join("/", "a", "b", "c")); + EXPECT_EQ("a\nb\nc\nd", strings::join("\n", "a", "b", "c", "d")); + std::stringstream ss; + EXPECT_EQ("a, b, c", strings::join(ss, ", ", "a", "b", "c").str()); + const std::string gnarly("gnarly"); + EXPECT_EQ("a/gnarly/c", strings::join("/", "a", gnarly, "c")); + const bool is_true = true; + const std::set<int32_t> my_set {1, 2, 3}; + EXPECT_EQ( + "a/gnarly/true/{ 1, 2, 3 }/c", + strings::join("/", "a", gnarly, is_true, my_set, "c")); +} + + TEST(StringsTest, StartsWith) { EXPECT_TRUE(strings::startsWith("hello world", "hello"));
