-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticStringTest.cpp
59 lines (58 loc) · 2.49 KB
/
StaticStringTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// test for static_string
// require C++17
#include "StaticString.hpp"
constexpr static_string a{"1"};
constexpr static_string b{""};
constexpr auto c = a + b;
template <std::make_signed_t<std::size_t> n, typename = void>
constexpr std::size_t width = 1 + width<n / 10>;
template <std::make_signed_t<std::size_t> n>
constexpr std::size_t width<n, std::enable_if_t<(n > 0 && n < 10)>> = 1;
template <> constexpr auto width<0> = 1;
template <std::make_signed_t<std::size_t> n>
constexpr std::size_t width<n, std::enable_if_t<(n < 0)>> = 1 + width<-n>;
template <std::make_signed_t<std::size_t> n, std::size_t i>
constexpr std::make_signed_t<std::size_t> cpow_imp_0 = n;
template <std::make_signed_t<std::size_t> n, std::size_t... I>
constexpr std::make_signed_t<std::size_t>
cpow_imp_1(std::index_sequence<I...>) {
return (cpow_imp_0<n, I> * ...);
}
template <std::make_signed_t<std::size_t> n, std::size_t i>
constexpr std::make_signed_t<std::size_t> cpow =
cpow_imp_1<n>(std::make_index_sequence<i>());
template <std::make_signed_t<std::size_t> n>
constexpr std::make_signed_t<std::size_t> cpow<n, 0> = 1;
template <std::make_signed_t<std::size_t> n, std::size_t i,
typename = std::enable_if_t<(i < width<n>)>>
constexpr static_string<1> digit = {
static_cast<char>('0' + static_cast<char>(n / cpow<10, i> % 10))};
template <std::make_signed_t<std::size_t> n, std::size_t... I>
constexpr static_string<width<n>> str_imp(std::index_sequence<I...>) {
return (digit<n, width<n> - 1 - I> + ...);
}
template <std::make_signed_t<std::size_t> n, typename = void>
constexpr static_string<width<n>> str =
str_imp<n>(std::make_index_sequence<width<n>>());
template <std::make_signed_t<std::size_t> n>
constexpr static_string<width<n>> str<n, std::enable_if_t<(n < 0)>> =
static_string{"-"} + str<-n>;
template <std::size_t l, std::size_t i>
constexpr auto make_expr =
str<i> + static_string{"*"} + str<l> + static_string{"="} + str<i * l> +
static_string{"\t"};
template <std::size_t l, std::size_t... I>
constexpr auto make_line_imp(std::index_sequence<I...>) {
return (make_expr<l, 1 + I> + ...);
}
template <std::size_t l>
constexpr auto make_line =
make_line_imp<l>(std::make_index_sequence<l>()) + static_string{"\n"};
template <std::size_t... I>
constexpr auto make_table_imp(std::index_sequence<I...>) {
return (make_line<1 + I> + ...);
}
template <std::size_t l>
constexpr auto make_table = make_table_imp(std::make_index_sequence<l>());
#include <cstdio>
int main() { std::puts(make_table<50>.data); }