Libmagicxx v5.1.1
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2024 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef UTILITY_HPP
5#define UTILITY_HPP
6
7#include <string>
8#include <ranges>
9#include <concepts>
10#include <algorithm>
11#include <functional>
12
13namespace utility {
14
27template <typename ContainerType, typename StringConverterType>
28requires std::ranges::range<ContainerType> && requires (ContainerType c){c.empty(); typename ContainerType::value_type;}
29[[nodiscard]]
30inline std::string to_string(
31 const ContainerType& container,
32 const std::string& value_separator, StringConverterType string_converter)
33{
34 static_assert(
35 std::same_as<std::invoke_result_t<StringConverterType, typename ContainerType::value_type>, std::string>,
36 "StringConverterType must return std::string"
37 );
38 if (container.empty()){
39 return {};
40 }
41 return std::ranges::fold_left(
42 std::ranges::next(std::ranges::begin(container)),
43 std::ranges::end(container),
44 std::invoke(string_converter, *std::ranges::begin(container)),
45 [&](const auto& left, const auto& right){
46 return left + value_separator + std::invoke(string_converter, right);
47 }
48 );
49}
50
51} /* namespace utility */
52
53#endif /* UTILITY_HPP */
Definition utility.hpp:13
std::string to_string(const ContainerType &container, const std::string &value_separator, StringConverterType string_converter)
Convert any container to string using the string_converter.
Definition utility.hpp:30