7#ifndef BOTAN_CONCAT_UTIL_H_
8#define BOTAN_CONCAT_UTIL_H_
10#include <botan/assert.h>
11#include <botan/concepts.h>
12#include <botan/mem_ops.h>
13#include <botan/range_concepts.h>
14#include <botan/strong_type_helpers.h>
33template <ranges::spanable_range OutR, ranges::spanable_range... Rs>
43 [[maybe_unused]]
auto fill_fn = [&] {
46 const size_t total_size = (
ranges.size() + ... + 0);
47 result.reserve(total_size);
51 return [&result](
auto&& range) {
53 out.insert(out.end(), std::ranges::begin(range), std::ranges::end(range));
59 [[maybe_unused]]
constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
60 static_assert(result.size() == total_size,
"size of result buffer does not match the sum of input buffers");
63 const size_t total_size = (
ranges.size() + ... + 0);
65 "result buffer has static extent that does not match the sum of input buffers");
69 return [itr = std::ranges::begin(result)](
auto&& range)
mutable {
70 std::copy(std::ranges::begin(range), std::ranges::end(range), itr);
71 std::advance(itr, std::ranges::size(range));
77 (fill_fn(std::forward<Rs>(
ranges)), ...);
94template <
typename OutR = detail::AutoDetect, ranges::spanable_range... Rs>
96 requires(all_same_v<std::ranges::range_value_t<Rs>...>)
98 if constexpr(std::same_as<detail::AutoDetect, OutR>) {
100 static_assert(
sizeof...(Rs) > 0,
"Cannot auto-detect the output type if not a single input range is provided.");
101 using candidate_result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Rs...>>>;
102 using result_range_value_t = std::remove_cvref_t<std::ranges::range_value_t<candidate_result_t>>;
107 constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
108 using out_array_t = std::array<result_range_value_t, total_size>;
115 "First input range has static extent, but a dynamically allocated output range is required. Please explicitly specify a dynamically allocatable output type.");
130template <
typename T,
typename Alloc>
131 requires std::is_trivially_copyable_v<T>
132void append(std::vector<T, Alloc>& out, std::type_identity_t<std::span<const T>> in) {
133 const size_t offset = out.size();
134 out.resize(offset + in.size());
135 copy_mem(std::span{out}.subspan(offset), in);
#define BOTAN_ARG_CHECK(expr, msg)
constexpr OutR concatenate(Rs &&... ranges)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
void append(std::vector< T, Alloc > &out, std::type_identity_t< std::span< const T > > in)
constexpr auto concat(Rs &&... ranges)