diff --git a/core/foundation/inc/ROOT/StringUtils.hxx b/core/foundation/inc/ROOT/StringUtils.hxx index 60cc23aa87e35..472edcf8e70ba 100644 --- a/core/foundation/inc/ROOT/StringUtils.hxx +++ b/core/foundation/inc/ROOT/StringUtils.hxx @@ -17,8 +17,8 @@ #include #include -#include #include +#include #include namespace ROOT { @@ -48,7 +48,12 @@ std::string Join(const std::string &sep, InputIt_t begin, InputIt_t end) if (begin == end) return ""; - return std::accumulate(std::next(begin), end, *begin, [&sep](auto const &a, auto const &b) { return a + sep + b; }); + std::string first(*begin++); + return std::accumulate(begin, end, std::move(first), [&sep](auto &&result, const auto &value) { + result += sep; + result += value; + return std::move(result); + }); } /** diff --git a/core/foundation/test/testStringUtils.cxx b/core/foundation/test/testStringUtils.cxx index 2264f2134f7b8..25b635b738cb9 100644 --- a/core/foundation/test/testStringUtils.cxx +++ b/core/foundation/test/testStringUtils.cxx @@ -42,6 +42,22 @@ TEST(StringUtils, Join) test("", ";;", {""}); } +TEST(StringUtils, JoinStringViews) +{ + const std::string source = "appleorangebanana"; + const std::vector strings = {std::string_view(source).substr(0, 5), + std::string_view(source).substr(5, 6), + std::string_view(source).substr(11)}; + EXPECT_EQ(ROOT::Join("::", strings), "apple::orange::banana"); + EXPECT_EQ(ROOT::Join("", strings.begin(), strings.end()), source); +} + +TEST(StringUtils, JoinCStringArray) +{ + const char *strings[] = {"apple", "", "banana"}; + EXPECT_EQ(ROOT::Join(",", strings), "apple,,banana"); +} + TEST(StringUtils, Round) { EXPECT_EQ(ROOT::Round(0.000000014, 0.000000024), "(10#pm20)*1e-9");