Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:02:03

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <format>
0012 #include <ranges>
0013 
0014 namespace Acts {
0015 
0016 /// Utuility to join a range of strings with a delimiter.
0017 /// Accepts any range of elements convertible to `std::string_view`.
0018 /// @param strings Range of strings to join
0019 /// @param delimiter Delimiter to insert between elements
0020 /// @returns Joined string
0021 template <std::ranges::range R>
0022   requires std::convertible_to<std::ranges::range_value_t<R>, std::string_view>
0023 std::string joinStrings(R&& strings, std::string_view delimiter) {
0024   std::string result;
0025   bool first = true;
0026 
0027   for (auto&& item :
0028        strings | std::views::transform(
0029                      [](const auto& s) -> std::string_view { return s; })) {
0030     if (!first) {
0031       result += delimiter;
0032     }
0033     result += item;
0034     first = false;
0035   }
0036 
0037   return result;
0038 }
0039 
0040 namespace detail {
0041 /// This mimics the signature of C++23's std::formattable concept
0042 template <typename T, typename>
0043 concept formattable = requires(T t) { std::format("{}", t); };
0044 }  // namespace detail
0045 
0046 /// Utility to join a range of formattable elements with a delimiter and custom
0047 /// format string.
0048 /// @param values Range of values to join
0049 /// @param delimiter Delimiter to insert between elements
0050 /// @param format Format string to apply to each element
0051 /// @returns Joined string
0052 template <std::ranges::range R>
0053   requires detail::formattable<std::ranges::range_value_t<R>, char>
0054 std::string joinStrings(
0055     R&& values, std::string_view delimiter,
0056     std::format_string<const std::ranges::range_value_t<R>&> format) {
0057   std::string result;
0058   bool first = true;
0059 
0060   for (const auto& value : values) {
0061     if (!first) {
0062       result += delimiter;
0063     }
0064     result += std::format(format, value);
0065     first = false;
0066   }
0067 
0068   return result;
0069 }
0070 
0071 /// Utility to join a range of formattable elements with a delimiter.
0072 /// @note This overload is selected if the range values are not directly
0073 ///       convertible to `std::string view`, and need to be default-formatted
0074 ///       first.
0075 /// @param values Range of values to join
0076 /// @param delimiter Delimiter to insert between elements
0077 /// @returns Joined string
0078 template <std::ranges::range R>
0079   requires(
0080       detail::formattable<std::ranges::range_value_t<R>, char> &&
0081       !std::convertible_to<std::ranges::range_value_t<R>, std::string_view>)
0082 std::string joinStrings(R&& values, std::string_view delimiter) {
0083   std::string result;
0084   bool first = true;
0085 
0086   for (const auto& value : values) {
0087     if (!first) {
0088       result += delimiter;
0089     }
0090     result += std::format("{}", value);
0091     first = false;
0092   }
0093 
0094   return result;
0095 }
0096 
0097 }  // namespace Acts