zeusUnnamed repository; edit this file 'description' to name the repository. |
git clone Unknown |
Log | Files | Refs |
commit | 3617edd0fb10e5256c471d5df88bd43ed008e9d3 |
parent | c63b26e6334cfaab952e88af5234077be1bf0790 |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Tue, 10 Jun 2025 08:36:12 +0200 |
Consteval string manipulation
A | conseval_string.cpp | | | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/ conseval_string.cpp b/ conseval_string.cpp
@@ -0,0 +1,73 @@
#include <array>
#include <string_view>
// max size object we can convert
static constexpr std::size_t oversized_size = 10 * 1024;
template<typename Value>
concept is_iterable = requires(const Value &value)
{
std::begin(value);
std::end(value);
};
template<typename Callable>
concept creates_iterable = requires(const Callable &callable)
{
requires is_iterable<std::decay_t<decltype(callable())>>;
};
template<typename Callable>
concept creates_string_like = requires(const Callable &callable)
{
requires creates_iterable<Callable>;
// TODO this check needs to be better
typename std::decay_t<decltype(callable())>::traits_type;
};
template<typename Value> struct oversized_array
{
std::array<Value, oversized_size> data{};
std::size_t size{};
using value_type = Value;
constexpr auto begin() const noexcept { return std::begin(data); }
constexpr auto end() const noexcept { return std::next(std::begin(data), static_cast<std::ptrdiff_t>(size)); }
};
template<typename Data> constexpr auto to_oversized_array(const Data &str)
{
oversized_array<typename Data::value_type> result;
std::copy(std::begin(str), std::end(str), std::begin(result.data));
result.size = str.size();
return result;
}
consteval auto to_right_sized_array(creates_iterable auto callable)
{
constexpr auto oversized = to_oversized_array(callable());
using Value_Type = typename std::decay_t<decltype(oversized)>::value_type;
std::array<Value_Type, oversized.size> result;
std::copy(std::begin(oversized), std::end(oversized), std::begin(result));
return result;
}
template<auto Data> inline constexpr const auto &make_static = Data;
consteval auto to_string_view(creates_string_like auto callable)
{
constexpr auto &static_data = make_static<to_right_sized_array(callable)>;
using Value_Type = typename std::decay_t<decltype(static_data)>::value_type;
std::basic_string_view<Value_Type> result(std::begin(static_data), std::end(static_data));
return result;
}
int main() {
auto sv = to_string_view([] -> std::string_view { return "Hello there!"; });
return 0;
}