-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixef_strings.hpp
More file actions
145 lines (112 loc) · 3.65 KB
/
fixef_strings.hpp
File metadata and controls
145 lines (112 loc) · 3.65 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <array>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std::string_view_literals;
constexpr std::size_t operator""_MB(unsigned long long mbs){
return mbs*1024*1024;
}
struct FixedStringWrapper{
const char* data_;
constexpr std::size_t size(){
std::size_t count = 0;
for (int i = 0; data_[i] != 0; i++){
count++;
}
return count;
}
constexpr const char* end(){
std::size_t count = 0;
for (int i = 0; data_[i] != 0; i++){
count++;
}
return data_ + count;
}
constexpr const char* begin(){
return data_;
}
};
//keeps up to 1mb size string
struct FIxedSizeArray{
std::array<char, 1_MB> data_{};
std::size_t size_;
};
constexpr auto getFixedSizeArray(const std::string& string) {
FIxedSizeArray res;
std::copy(string.begin(), string.end(), res.data_.data());
res.size_ = string.size();
return res;
}
constexpr auto concatFixedString(auto generator){
constexpr auto fixedSize = getFixedSizeArray(generator());
std::array<char, fixedSize.size_> res;
std::copy(fixedSize.data_.begin(), fixedSize.data_.begin() + fixedSize.size_, res.begin());
return res;
}
template <auto Data>
constexpr const auto &StaticData(){
return Data;
}
constexpr auto FixedStaticString(auto generator){
constexpr auto& static_data = StaticData<concatFixedString(generator)>();
return std::string_view(static_data.begin(), static_data.size());
}
/*constexpr auto make_crazy_string(std::string_view sc){
std::string s(sc);
return s+s+s;
}*/
/*another attemp without constexpr string*/
template <std::size_t Size>
struct fixed_string {
char _data[Size + 1]{0};
static constexpr std::size_t _size = Size;
constexpr explicit(false) fixed_string(char const* str) {
std::copy_n(str, Size + 1, _data);
}
constexpr explicit(false) fixed_string(auto const*... strs) {
auto size = 0;
([&size, this](char const* str){
auto s = FixedStringWrapper{str}.size();
std::copy_n(str, s, _data+size);
size += s;
}(strs), ...);
}
constexpr explicit(false) fixed_string() = default;
constexpr explicit(false) operator std::string_view() const {
return {_data, _size};
}
constexpr std::size_t size() const {
return _size;
}
constexpr const char* data() const {
return _data;
}
constexpr auto operator<=>(const fixed_string&) const = default;
constexpr auto operator+(const fixed_string& other) const{
return fixed_string<_size+std::decay_t<decltype(other)>::_size>(_data, other._data);
}
constexpr auto as_array() const{
std::array<char, _size+1> res;
std::copy_n(_data, _size+1, res.data());
return res;
}
};
template <unsigned int Size>
fixed_string(char const (&)[Size]) -> fixed_string<Size - 1>;
template<fixed_string Name>
constexpr auto operator""_fs() { return Name; };
int main(){
//constexpr auto generator1 = []{return make_crazy_string("Hello World! ");};
constexpr auto x = "*Hello* "_fs;
constexpr auto y = "*World!*"_fs;
//constexpr auto static_z = StaticData<x.data>();
//constexpr auto xx = std::string_view(z);
constexpr auto generator2 = [x, y](){
return StaticData<(x+y)>();};
constexpr auto zs = generator2();
//constexpr auto svc = FixedStaticString(generator2);
//constexpr static auto sv1 = FixedStaticString(generator1);
//std::cout << sv1;
std::cout << std::string_view(std::string_view(zs));
//std::cout << std::string_view(z).size() << (std::string_view(z) == "*Hello* *World!*"sv);
}