Sygaldry
Loading...
Searching...
No Matches
sygbp-spelling.hpp
1#pragma once
2#include <array>
3#include "sygah-consteval.hpp"
4
5namespace sygaldry { namespace sygbp {
10
11template<typename Device>
12 requires requires {Device::name();}
13_consteval auto name_length()
14{
15 size_t ret = 0;
16 while (Device::name()[ret] != 0) ret++;
17 return ret;
18}
19typedef char (*char_mapping)(char);
20template<char_mapping...mappings> struct compose;
21
22template<char_mapping mapping> struct compose<mapping>
23{
24 constexpr char operator()(char c) { return mapping(c); }
25};
26
27template<char_mapping mapping, char_mapping... mappings> struct compose<mapping, mappings...>
28{
29 constexpr char operator()(char c) { return compose<mappings...>{}(mapping(c)); }
30};
31constexpr char snake(char c) {return c == ' ' ? '_' : c;}
32constexpr char kebab(char c) {return c == ' ' ? '-' : c;}
33constexpr char lower(char c)
34{
35 if ('A' <= c && c <= 'Z') return c+('a'-'A');
36 return c;
37}
38constexpr char upper(char c)
39{
40 if ('a' <= c && c <= 'z') return c-('a'-'A');
41 return c;
42}
43
44template<typename NamedType, char_mapping... Mappings>
46{
47 static constexpr size_t N = name_length<NamedType>() + 1; // + 1 for null terminator
48 static constexpr std::array<char, N> value = [](const char * s)
49 {
50 auto mapping = compose<Mappings...>{}; // construct the composition of mappings
51 std::array<char, N> ret{};
52 for (size_t i = 0; i < N; ++i)
53 {
54 char c = s[i];
55 ret[i] = mapping(c); // apply the mapping
56 }
57 return ret;
58 }(NamedType::name());
59
60 respeller(NamedType) {}
61 constexpr operator const char *() noexcept { return value.data(); }
62};
63template<typename NamedType>
64struct respeller<NamedType>
65{
66 respeller(NamedType) {}
67 constexpr operator const char *() noexcept { return NamedType::name(); }
68};
69template<typename NamedType>
71{
72 passthrough_spelling([[maybe_unused]] NamedType x) : respeller<NamedType>{x} {}
73};
74
75template<typename NamedType>
76struct snake_case : respeller<NamedType, snake>
77{
78 snake_case([[maybe_unused]] NamedType x) : respeller<NamedType, snake>{x} {}
79};
80
81template<typename NamedType>
82struct upper_snake_case : respeller<NamedType, snake, upper>
83{
84 upper_snake_case([[maybe_unused]] NamedType x) : respeller<NamedType, snake, upper>{x} {}
85};
86
87template<typename NamedType>
88struct lower_snake_case : respeller<NamedType, snake, lower>
89{
90 lower_snake_case([[maybe_unused]] NamedType x) : respeller<NamedType, snake, lower>{x} {}
91};
92
93template<typename NamedType>
94struct kebab_case : respeller<NamedType, kebab>
95{
96 kebab_case([[maybe_unused]] NamedType x) : respeller<NamedType, kebab>{x} {}
97};
98
99template<typename NamedType>
100struct upper_kebab_case : respeller<NamedType, kebab, upper>
101{
102 upper_kebab_case([[maybe_unused]] NamedType x) : respeller<NamedType, kebab, upper>{x} {}
103};
104
105template<typename NamedType>
106struct lower_kebab_case : respeller<NamedType, kebab, lower>
107{
108 lower_kebab_case([[maybe_unused]] NamedType x) : respeller<NamedType, kebab, lower>{x} {}
109};
110template<typename NamedType> constexpr const char * snake_case_v = snake_case<NamedType>::value.data();
111template<typename NamedType> constexpr const char * upper_snake_case_v = upper_snake_case<NamedType>::value.data();
112template<typename NamedType> constexpr const char * lower_snake_case_v = lower_snake_case<NamedType>::value.data();
113template<typename NamedType> constexpr const char * kebab_case_v = kebab_case<NamedType>::value.data();
114template<typename NamedType> constexpr const char * upper_kebab_case_v = upper_kebab_case<NamedType>::value.data();
115template<typename NamedType> constexpr const char * lower_kebab_case_v = lower_kebab_case<NamedType>::value.data();
116
119} }
Definition sygbp-spelling.hpp:20
Definition sygbp-spelling.hpp:95
Definition sygbp-spelling.hpp:107
Definition sygbp-spelling.hpp:89
Definition sygbp-spelling.hpp:71
Definition sygbp-spelling.hpp:46
Definition sygbp-spelling.hpp:77
Definition sygbp-spelling.hpp:101
Definition sygbp-spelling.hpp:83