-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_parse.hpp
326 lines (288 loc) · 10.5 KB
/
string_parse.hpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
#include <algorithm>
#include <array>
#include <cstddef>
#include <utility>
#include <type_traits>
#include <tuple>
#include <string>
#include "fixed_string.hpp"
#include "constant_parameter.hpp"
namespace mlib
{
template <fixed_string str, char c, int index>
constexpr auto find_index_arr() {
if constexpr (str.template nth_element<index>() == c) {
return index;
}
else {
if constexpr (str.size() <= index)
{
return -1;
}
else
{
return find_index_arr<str, c, index + 1>();
}
}
}
template <fixed_string str, char c>
constexpr auto find_index_arr() {
if constexpr (str.template nth_element<0>() == c) {
return 0;
}
else {
return find_index_arr<str, c, 1>();
}
}
template<char c, std::size_t>
constexpr auto function_return()
{
return c;
}
template<bool condition, auto value, char return_>
struct if_is
{
constexpr if_is() {}
constexpr auto operator()() const noexcept
{
if constexpr (condition)
{
return value;
}
else
{
return return_;
}
}
};
template<typename T, std::size_t I>
constexpr auto array_to_fixed_string(std::array<T, I> arr)
{
return fixed_string<I>{arr.data()};
}
template<auto blur, auto other, auto until, auto index>
constexpr auto when_less_than_idx()
{
if constexpr (index < until)
{
return blur;
}
else
{
return other;
}
}
template<auto blur, auto other, auto until, auto index>
constexpr auto when_greater_than_idx()
{
if constexpr (index > until)
{
return blur;
}
else
{
return other;
}
}
template<auto... Ts>
struct container;
template <fixed_string str>
struct string_parse {
template <auto A, auto B>
constexpr auto substr() const noexcept {
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>) {
constexpr char result[] = { (str.template nth_element<A + indexes>())..., '\0' };
return fixed_string{ result };
}(std::make_index_sequence<B - A>{});
}
template<std::size_t... indexes>
constexpr auto take() const noexcept
{
constexpr char result[] = { (str.template nth_element<indexes>())..., '\0' };
return fixed_string{ result };
}
template <char c>
constexpr auto consume_until() const noexcept {
constexpr int index = find_index_arr<str, c>();
return substr<0, index>();
}
template<char c>
constexpr auto find() const noexcept
{
return find_index_arr<str, c>();
}
// make this private
template<auto Char, auto Index>
constexpr auto function_return_index() const noexcept
{
return Char;
}
template<char c, auto lambda>
constexpr auto if_string_has_change() const noexcept
{
return change_with<c, lambda>();
}
template<char c, auto lamda>
constexpr auto if_string_has() const noexcept
{
if constexpr (occurences<[](auto x){x == c;}>() > 0)
{
return change_with<lamda>();
}
}
template<char c>
constexpr auto blur_with() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
const char result[] = { (function_return_index<c, indexes>())..., '\0' };
return fixed_string{ result };
}(std::make_index_sequence<str.size()>{});
}
template<auto lambda>
constexpr auto change_with() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
constexpr char result[] = { (lambda(str.template nth_element<indexes>()))..., '\0' };
return fixed_string{ result };
}(std::make_index_sequence<str.size()>{});
}
template<auto lambda>
constexpr auto occurences() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return (lambda.template operator() < str.template nth_element<indexes>() > () + ...);
}(std::make_index_sequence<str.size()>{});
}
template<char c, int X>
constexpr auto blur_until() const noexcept
{
return [&] <std::size_t... indexes>(std::index_sequence<indexes...>)
{
const char result[] = { (when_less_than_idx<c, str.data_[indexes], X, indexes>())..., '\0' };
return fixed_string{ result };
}(std::make_index_sequence<str.size()>{});
}
template<char c, int X>
constexpr auto blur_from() const noexcept
{
return[&] <std::size_t... indexes>(std::index_sequence<indexes...>)
{
const char result[] = { (when_greater_than_idx<c, str.data_[indexes], X, indexes>())..., '\0' };
return fixed_string{ result };
}(std::make_index_sequence<str.size()>{});
}
template<auto lambda>
constexpr auto remove_if() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
constexpr char array[] = { if_is<!lambda(str.template nth_element<indexes>()), str.template nth_element<indexes>(), ' '>{}()..., '\0' };
return fixed_string{ array };
}(std::make_index_sequence<str.size()>{});
}
template<auto lambda>
constexpr auto remove_if_not() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
constexpr char array[] = { if_is<lambda(str.template nth_element<indexes>()), str.template nth_element<indexes>(), ' '>{}()..., '\0' };
return fixed_string{ array };
}(std::make_index_sequence<str.size()>{});
}
template<auto lambda, char c>
constexpr auto replace_with_if() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
constexpr char array[] = { if_is<!lambda(str.template nth_element<indexes>()), str.template nth_element<indexes>(), c>{}()..., '\0' };
return fixed_string{ array };
}(std::make_index_sequence<str.size()>{});
}
template<auto character>
constexpr auto character_occurences() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((str.data[indexes] == character) + ...);
}(std::make_index_sequence<str.size()>{});
}
template<auto character, auto from>
constexpr auto character_occurences_from() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((str.data[indexes + from] == character) + ...);
}(std::make_index_sequence<str.size() - from>{});
}
template<auto character, auto until>
constexpr auto character_occurences_until() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((if_is<(indexes < until), (str.data[indexes] == character), 0>{}()) + ...);
}(std::make_index_sequence<str.size()>{});
}
template<auto from, auto character>
constexpr auto from_up_to() const noexcept
{
constexpr auto index = find<character>();
return substr<from, index>();
}
template<auto from>
constexpr auto from_to_end() const noexcept
{
constexpr auto index = find<from>();
return substr<index, size()>();
}
template<auto first, auto second>
constexpr auto alternate_blur() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
constexpr char arr[] = {(if_is<((indexes % 2) == 0), first, second>{}())..., '\n'};
return fixed_string{arr};
}(std::make_index_sequence<str.size() - 1>{});
}
constexpr auto non_recuring() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((character_occurences<str.template nth_element<indexes>()>() < 2) + ...);
}(std::make_index_sequence<str.size()>{});
}
constexpr auto to_container()
{
return [] <std::size_t... indexes>(std::index_sequence<indexes...>)
{
return container<(str.template nth_element<indexes>())...>{};
}(std::make_index_sequence<str.size()>{});
}
template<char c>
constexpr auto all_characters_are() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((c == str.template nth_element<indexes>()) && ...);
}(std::make_index_sequence<str.size()>{});
}
template<char c>
constexpr auto all_characters_are_not() const noexcept
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return ((c != str.template nth_element<indexes>()) && ...);
}(std::make_index_sequence<str.size()>{});
}
constexpr auto data() const noexcept { return str.data; }
constexpr auto string_view() const noexcept { return std::string_view{ str.data }; }
constexpr auto string() const noexcept { return str; }
constexpr auto number_of_characters() const noexcept { return str.size(); }
constexpr auto size() const noexcept { return str.size(); }
constexpr auto stdstring() const noexcept { return std::string{ str.data }; }
};
} // namespace mlib
// https://godbolt.org/z/Ghee85T1n