Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2026 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
44
45#ifndef UTILITY_HPP
46#define UTILITY_HPP
47
48#include <algorithm>
49#include <concepts>
50#include <filesystem>
51#include <functional>
52#include <ranges>
53#include <string>
54
55namespace Recognition {
98namespace Utility {
126template <typename ContainerT>
127concept RangeContainer = std::ranges::range<ContainerT>
128 && requires(ContainerT c) {
129 typename ContainerT::value_type;
130 };
131
158template <typename ValueT, typename StringConverterT>
159concept StringConverter = std::same_as<
160 std::invoke_result_t<StringConverterT, ValueT>,
161 std::string
162>;
163
190template <RangeContainer ContainerT, typename StringConverterT>
192[[nodiscard]] inline std::string ToString(
193 const ContainerT& container,
194 const std::string& value_separator,
195 StringConverterT string_converter
196)
197{
198 if (container.empty()) {
199 return {};
200 }
201 return std::ranges::fold_left(
202 std::ranges::next(std::ranges::begin(container)),
203 std::ranges::end(container),
204 std::invoke(string_converter, *std::ranges::begin(container)),
205 [&](const auto& left, const auto& right) {
206 return left
207 + value_separator
208 + std::invoke(string_converter, right);
209 }
210 );
211}
212
249template <typename ContainerT>
250concept FileContainer = std::ranges::range<ContainerT>
251 && std::default_initializable<ContainerT>
252 && std::same_as<
253 typename ContainerT::value_type,
254 std::filesystem::path
255 >
256 && requires(ContainerT c, std::filesystem::path p) {
257 c.push_back(p);
258 c.empty();
259 typename ContainerT::value_type;
260 };
261
290[[nodiscard]] inline std::string ToString(
291 const FileContainer auto& container,
292 const std::string& separator = ", "
293)
294{
295 return ToString(
296 container,
297 separator,
298 [](const std::filesystem::path& path) {
299 return path.string();
300 }
301 );
302}
303} /* namespace Utility */
304} /* namespace Recognition */
305
306#endif /* UTILITY_HPP */
Concept for containers that can hold filesystem paths.
Definition utility.hpp:250
Concept for types that are ranges with an accessible value_type.
Definition utility.hpp:127
Concept for callables that convert a value to std::string.
Definition utility.hpp:159
std::string ToString(const ContainerT &container, const std::string &value_separator, StringConverterT string_converter)
Convert a container to a string using a custom converter.
Definition utility.hpp:192
Utility components for the libmagicxx library.
Definition percentage.hpp:39
Root namespace for the libmagicxx library.