From 4877408f02ee98cf4bd201d3d0a8bfe0c3c10782 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Wed, 16 Sep 2026 16:07:18 +0530 Subject: [PATCH 1/2] [core] Support string views and C strings in Join --- core/foundation/inc/ROOT/StringUtils.hxx | 8 ++++++-- core/foundation/test/testStringUtils.cxx | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/core/foundation/inc/ROOT/StringUtils.hxx b/core/foundation/inc/ROOT/StringUtils.hxx index 60cc23aa87e35..b770673ccb70a 100644 --- a/core/foundation/inc/ROOT/StringUtils.hxx +++ b/core/foundation/inc/ROOT/StringUtils.hxx @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -48,7 +47,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 result(*begin++); + for (; begin != end; ++begin) { + result += sep; + result += *begin; + } + return 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"); From b521e13eb8b19976b27f1d94f9a098dbdf8141e0 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Wed, 16 Sep 2026 23:43:48 +0530 Subject: [PATCH 2/2] [core] Use accumulate for string joins --- core/foundation/inc/ROOT/StringUtils.hxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/foundation/inc/ROOT/StringUtils.hxx b/core/foundation/inc/ROOT/StringUtils.hxx index b770673ccb70a..472edcf8e70ba 100644 --- a/core/foundation/inc/ROOT/StringUtils.hxx +++ b/core/foundation/inc/ROOT/StringUtils.hxx @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace ROOT { @@ -47,12 +48,12 @@ std::string Join(const std::string &sep, InputIt_t begin, InputIt_t end) if (begin == end) return ""; - std::string result(*begin++); - for (; begin != end; ++begin) { + std::string first(*begin++); + return std::accumulate(begin, end, std::move(first), [&sep](auto &&result, const auto &value) { result += sep; - result += *begin; - } - return result; + result += value; + return std::move(result); + }); } /**