2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2022-05-20 10:17:20 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-06-08 20:58:22 +02:00
|
|
|
#include "CopyConst.hxx"
|
|
|
|
|
2022-06-01 21:39:56 +02:00
|
|
|
#include <cassert>
|
2022-05-20 10:17:20 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <span>
|
2022-05-24 13:50:04 +02:00
|
|
|
#include <string_view>
|
2022-05-20 10:17:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cast a std::span<std::byte> to a std::span<T>, rounding down to the
|
|
|
|
* next multiple of T's size.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
constexpr std::span<T>
|
2022-06-08 21:03:44 +02:00
|
|
|
FromBytesFloor(std::span<CopyConst<std::byte, T>> other) noexcept
|
2022-05-20 10:17:20 +02:00
|
|
|
{
|
|
|
|
static_assert(sizeof(T) > 0, "Empty base type");
|
|
|
|
|
2022-06-08 14:12:29 +02:00
|
|
|
/* TODO: the "void *" cast suppresses alignment
|
|
|
|
warnings, but should we really suppress them? */
|
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
return {
|
2022-06-08 21:03:44 +02:00
|
|
|
reinterpret_cast<T *>(reinterpret_cast<CopyConst<void, T> *>(other.data())),
|
2022-05-20 10:17:20 +02:00
|
|
|
other.size() / sizeof(T),
|
|
|
|
};
|
|
|
|
}
|
2022-05-24 13:50:04 +02:00
|
|
|
|
2022-06-01 21:39:56 +02:00
|
|
|
/**
|
|
|
|
* Like FromBytesFloor(), but assert that rounding is not necessary.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
constexpr std::span<T>
|
2022-06-08 21:03:44 +02:00
|
|
|
FromBytesStrict(std::span<CopyConst<std::byte, T>> other) noexcept
|
2022-06-01 21:39:56 +02:00
|
|
|
{
|
|
|
|
assert(other.size() % sizeof(T) == 0);
|
|
|
|
|
|
|
|
return FromBytesFloor<T>(other);
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:50:04 +02:00
|
|
|
constexpr std::span<const char>
|
|
|
|
ToSpan(std::string_view sv) noexcept
|
|
|
|
{
|
|
|
|
#if defined(__clang__) && __clang_major__ < 15
|
|
|
|
/* workaround for old clang/libc++ versions which can't cast
|
|
|
|
std::string_view to std::span */
|
|
|
|
return {sv.data(), sv.size()};
|
|
|
|
#else
|
|
|
|
return std::span{sv};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::span<const std::byte>
|
|
|
|
AsBytes(std::string_view sv) noexcept
|
|
|
|
{
|
|
|
|
return std::as_bytes(ToSpan(sv));
|
|
|
|
}
|
2022-06-08 14:10:58 +02:00
|
|
|
|
|
|
|
constexpr std::string_view
|
|
|
|
ToStringView(std::span<const char> s) noexcept
|
|
|
|
{
|
|
|
|
return {s.data(), s.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::string_view
|
|
|
|
ToStringView(std::span<const std::byte> s) noexcept
|
|
|
|
{
|
|
|
|
return ToStringView(FromBytesStrict<const char>(s));
|
|
|
|
}
|
2022-11-15 00:36:24 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr std::basic_string_view<T>
|
|
|
|
ToStringView(std::span<const T> s) noexcept
|
|
|
|
{
|
|
|
|
return {s.data(), s.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr std::basic_string_view<T>
|
|
|
|
ToStringView(std::span<T> s) noexcept
|
|
|
|
{
|
|
|
|
return {s.data(), s.size()};
|
|
|
|
}
|