codex
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef CODEX_LUA_UTILS_H
2#define CODEX_LUA_UTILS_H
3
4#include <algorithm>
5#include <cstddef>
6#include <ranges>
7#include <string_view>
8#include <utility>
9
10#define FWD(x) std::forward<decltype(x)>(x)
11
12template<std::size_t N>
14 constexpr fixed_string(void) = default;
15 constexpr fixed_string(const char (&s_)[N + 1]);
16 constexpr operator std::string_view(void) const { return {this->s, N}; }
17 constexpr std::size_t size(void) const { return N; }
18 constexpr const char *data(void) const { return this->s; }
19 constexpr const char *begin(void) const { return this->s; }
20 constexpr const char *end(void) const { return this->s + N; }
21 constexpr char *begin(void) { return this->s; }
22 constexpr char *end(void) { return this->s + N; }
23 char s[N] = {};
24};
25
26template<typename ...Ts> struct types;
27
28auto front(std::tuple<>) -> types<>;
29auto pop_front(std::tuple<>) -> types<>;
30
31template<typename T, typename ...Ts>
32auto front(std::tuple<T, Ts...>) -> T;
33
34template<typename T, typename ...Ts>
35auto pop_front(std::tuple<T, Ts...>) -> types<Ts...>;
36
37template<typename ...Ts>
38struct types {
39 using front = decltype(::front(std::tuple<Ts...>{}));
40 template<typename T> using push_back = types<Ts..., T>;
41 using pop_front = decltype(::pop_front(std::tuple<Ts...>{}));
42 static constexpr void for_each(auto &&f) { (..., FWD(f)(Ts{})); }
43};
44
45template<std::size_t N>
46fixed_string(const char (&)[N]) -> fixed_string<N - 1>;
47
48template<std::size_t N>
49constexpr fixed_string<N>::fixed_string(const char (&s_)[N + 1]) {
50 std::copy(s_, s_ + N, this->s);
51}
52
53template<std::size_t N0, std::size_t N1>
55 auto ret = fixed_string<N0 + N1>{};
56 std::copy(s0.begin(), s0.end(), ret.begin());
57 std::copy(s1.begin(), s1.end(), ret.begin() + N0);
58 return ret;
59}
60
61template<fixed_string s, std::size_t b, std::size_t e>
63 constexpr explicit operator std::string_view(void) const;
64};
65
66template<fixed_string s, std::size_t b, std::size_t e>
67constexpr fixed_string_view<s, b, e>::operator std::string_view(void) const {
68 return std::string_view{s}.substr(b, e - b);
69}
70
71template<auto x>
72using constant = std::integral_constant<decltype(x), x>;
73
74constexpr auto slice(std::string_view s, std::size_t b, std::size_t e) {
75 return std::string_view{
76 begin(s) + std::min(b, s.size()),
77 begin(s) + std::min(e, s.size()),
78 };
79}
80
81#endif
#define f(x)
Definition 5.c:2
#define T(f, T, C)
Definition bench.cpp:136
#define x
Definition gcc14.c:1
Definition main.cpp:7
#define N
Definition queue_atomic.c:4
codex::refl::field_tuple_t< E > T
Definition soa.cpp:9
Definition mult_inh.c:26
Definition utils.hpp:62
Definition utils.hpp:13
char s[N]
Definition utils.hpp:23
constexpr char * end(void)
Definition utils.hpp:22
constexpr const char * data(void) const
Definition utils.hpp:18
constexpr fixed_string(void)=default
constexpr char * begin(void)
Definition utils.hpp:21
constexpr const char * end(void) const
Definition utils.hpp:20
constexpr const char * begin(void) const
Definition utils.hpp:19
constexpr std::size_t size(void) const
Definition utils.hpp:17
Definition utils.hpp:38
decltype(::front(std::tuple< Ts... >{})) front
Definition utils.hpp:39
decltype(::pop_front(std::tuple< Ts... >{})) pop_front
Definition utils.hpp:41
static constexpr void for_each(auto &&f)
Definition utils.hpp:42
constexpr fixed_string s
Definition test.cpp:6
fixed_string(const char(&)[N]) -> fixed_string< N - 1 >
constexpr auto operator+(fixed_string< N0 > s0, fixed_string< N1 > s1)
Definition utils.hpp:54
#define FWD(x)
Definition utils.hpp:10
auto front(std::tuple<>) -> types<>
auto pop_front(std::tuple<>) -> types<>
std::integral_constant< decltype(x), x > constant
Definition utils.hpp:72
constexpr auto slice(std::string_view s, std::size_t b, std::size_t e)
Definition utils.hpp:74