-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.hpp
More file actions
506 lines (433 loc) · 11.9 KB
/
Copy pathstring.hpp
File metadata and controls
506 lines (433 loc) · 11.9 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**
* \file string.hpp
* Consteval String Library
* \version 0.1.0
* \copyright Copyright (c) 2026 Justin Singh-Mohudpur.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.md or copy at https://www.boost.org/LICENSE_1_0.txt)
*/
#pragma once
#include <cstddef>
#include <tuple>
namespace jsm {
/**
* compile-time (consteval) string
*
* \note The underlying buffer is null-terminated.
* \tparam N Length of the string, not including the null-terminator.
*/
template<std::size_t N>
struct string
{
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using value_type = char;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using iterator = pointer;
using const_iterator = const_pointer;
static constexpr size_type npos = static_cast<size_type>(-1);
consteval string() noexcept = default;
/// Construct a string from a string literal.
consteval string(const char (&s)[N + 1]) noexcept
{
for (size_type i = 0; i < N + 1; ++i) {
storage[i] = s[i];
}
}
/// The size (or length) of the string.
consteval size_type size() const noexcept
{
return N;
}
/// The size (or length) of the string.
consteval size_type length() const noexcept
{
return N;
}
/// Get a pointer to the start of the string buffer.
consteval pointer data() noexcept
{
return storage;
}
/// Get a const pointer to the start of the string buffer.
consteval const_pointer data() const noexcept
{
return storage;
}
consteval reference operator[](size_type n) noexcept
{
return storage[n];
}
consteval const_reference operator[](size_type n) const noexcept
{
return storage[n];
}
consteval reference front() noexcept
{
return operator[](0);
}
consteval const_reference front() const noexcept
{
return operator[](0);
}
consteval reference back() noexcept
{
return operator[](size() - 1);
}
consteval const_reference back() const noexcept
{
return operator[](size() - 1);
}
consteval iterator begin() noexcept
{
return storage;
}
consteval const_iterator begin() const noexcept
{
return storage;
}
consteval const_iterator cbegin() const noexcept
{
return storage;
}
consteval iterator end() noexcept
{
return storage + size();
}
consteval const_iterator end() const noexcept
{
return storage + size();
}
consteval const_iterator cend() const noexcept
{
return storage + size();
}
consteval bool starts_with(char c) const noexcept
{
return front() == c;
}
template<std::size_t M>
consteval bool starts_with(string<M> s) const noexcept
{
if constexpr (M > N) {
return false;
}
for (std::size_t i = 0; i < M; ++i) {
if (storage[i] != s[i]) {
return false;
}
}
return true;
}
consteval bool ends_with(char c) const noexcept
{
return back() == c;
}
template<std::size_t M>
consteval bool ends_with(string<M> s) const noexcept
{
if constexpr (M > N) {
return false;
}
constexpr auto K = N - M;
for (std::size_t i = 0; i < M; ++i) {
if (storage[i + K] != s[i]) {
return false;
}
}
return true;
}
consteval string<N + 1> append(char c) const noexcept
{
string<N + 1> result{};
auto it = result.begin();
for (auto s : *this) {
*it = s;
it++;
}
*it = c;
return result;
}
template<size_t M>
consteval string<N + M> append(string<M> s) const noexcept
{
string<N + M> result{};
auto it = result.begin();
for (auto c : *this) {
*it = c;
it++;
}
for (auto c : s) {
*it = c;
it++;
}
return result;
}
template<size_type M>
consteval auto operator+(string<M> s) const noexcept
{
return append(s);
}
consteval auto operator+(char c) const noexcept
{
return append(c);
}
template<size_t M>
consteval bool operator==(string<M> s) const noexcept
{
if constexpr (N != M) {
return false;
}
for (size_type i = 0; i < N; ++i) {
if (storage[i] != s[i]) {
return false;
}
}
return true;
}
template<size_t M>
consteval bool operator==(const char (&s)[M]) const noexcept
{
if constexpr (M != (N + 1)) {
return false;
}
for (size_type i = 0; i < N + 1; ++i) {
if (storage[i] != s[i]) {
return false;
}
}
return true;
}
value_type storage[N + 1]{};
};
template<size_t N>
using consteval_string = string<N>;
template<size_t N>
string(const char (&)[N]) -> string<N - 1>;
inline constexpr string empty = "";
/// Apply an operation over every character in \p S.
/// \param op A map operation that takes a char.
template<string S, typename F>
requires (std::is_invocable_r_v<char, F, char>)
consteval decltype(S) map(F&& op) noexcept(noexcept(op))
{
auto new_str = S;
for (auto& c : new_str) {
c = op(c);
}
return new_str;
}
/// Apply a left-fold operation to \p S.
/// \param init The initial value.
/// \param op A fold operation that takes an init value and a char.
template<string S, typename T, typename F>
requires (std::is_invocable_r_v<T, F, T, char>)
consteval T fold(T&& init, F&& op) noexcept(noexcept(op))
{
decltype(auto) result = std::forward<T>(init);
for (auto c : S) {
result = op(std::forward<T>(result), c);
}
return result;
}
/// Slice \p S into the range [\p InclusiveBegin, \p ExclusiveEnd).
template<string S, std::size_t InclusiveBegin, std::size_t ExclusiveEnd>
requires (ExclusiveEnd <= S.size() && ExclusiveEnd >= InclusiveBegin)
consteval string<ExclusiveEnd - InclusiveBegin> slice() noexcept
{
if constexpr (ExclusiveEnd == InclusiveBegin) {
return string<0>{ "" };
} else {
string<ExclusiveEnd - InclusiveBegin> result{};
for (size_t i = InclusiveBegin; i < ExclusiveEnd; ++i) {
result[i - InclusiveBegin] = S[i];
}
return result;
}
}
/// Substring \p S with length \p Length starting with \p Begin.
///
/// \note If \p Begin + \p Length is larger than \p S, then \p Length is bounded to \p S.size().
template<string S, std::size_t Begin = 0, std::size_t Length = S.size()>
requires (Begin < S.size())
consteval auto substr() noexcept
{
constexpr auto MaybeEnd = Begin + Length;
constexpr auto End = MaybeEnd > S.size() ? S.size() : MaybeEnd;
return slice<S, Begin, End>();
}
/// Concat all of \p S.
template<string... S>
consteval auto concat() noexcept
{
return (... + S);
}
/// Find the first instance of \p C in \p S.
template<string S, char C>
consteval std::size_t find() noexcept
{
constexpr auto N = S.length();
for (size_t i = 0; i < N; ++i) {
if (S[i] == C) {
return i;
}
}
return string<N>::npos;
}
/// Find the first instance of \p T in \p S.
template<string S, string T>
consteval std::size_t find() noexcept
{
constexpr auto N = S.length();
constexpr auto M = T.length();
for (std::size_t n = 0; n < N; ++n) {
for (std::size_t m = 0; m <= M; ++m) {
if (m == M) {
return n;
}
if (n + m >= N) {
n = N;
break;
}
if (S[n + m] != T[m]) {
n += m;
break;
}
}
}
return string<N>::npos;
}
/// Count the number of occurrences of \p C in \p S.
template<string S, char C>
consteval std::size_t count() noexcept
{
return fold<S>(static_cast<std::size_t>(0), [](auto acc, char s) { return acc + (s == C); });
}
/// Split \p S by the first character \p C.
template<string S, char C>
consteval auto split_once() noexcept
{
constexpr auto pos = find<S, C>();
if constexpr (pos == decltype(S)::npos) {
return std::make_tuple(S);
} else if constexpr (pos == S.size()) {
return std::make_tuple(slice<0, pos>(S), empty);
} else if constexpr (pos == 0) {
return std::make_tuple(empty, slice<S, 1, S.size()>());
} else {
return std::make_tuple(slice<S, 0, pos>(), slice<S, pos + 1, S.size()>());
}
}
/// Split \p S by the first instance of \p T.
template<string S, string T>
consteval auto split_once() noexcept
{
constexpr auto pos = find<S, T>();
if constexpr (pos == decltype(S)::npos) {
return std::make_tuple(S);
} else if constexpr (pos == (S.size() - T.size())) {
return std::make_tuple(slice<S, 0, pos>(), empty);
} else if constexpr (pos == 0) {
return std::make_tuple(empty, slice<S, T.size(), S.size()>());
} else {
return std::make_tuple(slice<S, 0, pos>(), slice<S, pos + T.size(), S.size()>());
}
}
/// Split \p S by all instances of \p C.
template<string S, char C>
consteval auto split() noexcept
{
constexpr auto s = split_once<S, C>();
if constexpr (std::tuple_size_v<decltype(s)> == 1) {
return s;
} else {
constexpr auto left = std::make_tuple(std::get<0>(s));
constexpr auto right = split<std::get<1>(s), C>();
return std::tuple_cat(left, right);
}
}
/// Split \p S by all instances of \p T.
template<string S, string T>
consteval auto split() noexcept
{
constexpr auto s = split_once<S, T>();
if constexpr (std::tuple_size_v<decltype(s)> == 1) {
return s;
} else {
constexpr auto left = std::make_tuple(std::get<0>(s));
constexpr auto right = split<std::get<1>(s), T>();
return std::tuple_cat(left, right);
}
}
/// Replace all instances of \p From with \p To in \p S.
template<string S, char From, char To>
consteval decltype(S) replace() noexcept
{
return map<S>([](char s) { return s == From ? To : s; });
}
/// Replace all instances of \p From with \p To in \p S.
template<string S, string From, string To>
consteval auto replace() noexcept
{
constexpr auto s = split_once<S, From>();
if constexpr (std::tuple_size_v<decltype(s)> == 1) {
return S;
} else {
constexpr auto left = std::get<0>(s).append(To);
constexpr auto right = replace<std::get<1>(s), From, To>();
return concat<left, right>();
}
}
/// Remove all instances of \p C from \p S.
template<string S, char C>
consteval auto remove() noexcept
{
constexpr auto N = S.size();
constexpr auto M = count<S, C>();
if constexpr (M == 0) {
return S;
}
string<N - M> result{};
auto* out = result.data();
for (auto it = S.begin(); it != S.end() && out != result.end(); ++it) {
if (*it == C) {
continue;
}
*out = *it;
++out;
}
return result;
}
/// Remove all instances of \p T from \p S.
template<string S, string T>
consteval auto remove() noexcept
{
if constexpr (T == empty) {
return S;
} else {
return replace<S, T, "">();
}
}
/// Reverse the characters of \p S.
template<string S>
consteval decltype(S) reverse() noexcept
{
constexpr auto N = S.size();
string<N> r{};
for (std::size_t i = 0; i < N; ++i) {
r[i] = S[N - (i + 1)];
}
return r;
}
constexpr char toupper(char c) noexcept
{
return (c < 'a' || c > 'z') ? c : c - 32;
}
constexpr char tolower(char c) noexcept
{
return (c < 'A' || c > 'Z' ? c : c + 32);
}
} // namespace jsm