-
Notifications
You must be signed in to change notification settings - Fork 1
/
StaticStr.hpp
248 lines (204 loc) · 7.08 KB
/
StaticStr.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
// Copyright (c) 2023 Francesco Cavaliere
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef CAV_INCLUDE_STATICSTR_HPP
#define CAV_INCLUDE_STATICSTR_HPP
#include <array>
#include <cassert>
#include <string_view>
#include "../comptime/mp_base.hpp"
#include "../comptime/test.hpp"
#include "../mish/util_functions.hpp"
#if __has_include(<fmt/core.h>)
#define CAV_FOUND_FMT
#include <fmt/core.h>
#endif
namespace cav {
/// @brief String literal made to be easily converted to other compile-time sized char containers
template <std::size_t N>
struct StaticStr : std::array<char, N> {
using base = std::array<char, N>;
using self = StaticStr;
private:
static constexpr void _copy_init(auto& src, auto& dest) noexcept {
// assert(src[N - 1] == '\0');
for (std::size_t i = 0; i < N; ++i)
dest[i] = src[i];
}
public:
constexpr StaticStr() = default;
constexpr StaticStr(base const& str)
: base{str} {
}
constexpr StaticStr(std::array<char const, N> const& str) {
_copy_init(str, *this);
}
constexpr StaticStr(char const (&str)[N]) {
_copy_init(str, *this);
}
constexpr StaticStr(char (&str)[N]) {
_copy_init(str, *this);
}
constexpr StaticStr(std::string_view str) {
size_t str_size = std::size(str);
size_t sz = str_size < N ? str_size : N - 1;
for (size_t i = 0; i < sz; ++i)
(*this)[i] = str[i];
for (size_t i = sz; i < N; ++i)
(*this)[i] = '\0';
}
constexpr operator std::array<char const, N>() const {
std::array<char const, N> result{};
_copy_init(*this, result);
return result;
}
constexpr operator base const&() const {
return *this;
}
constexpr operator base&() {
return *this;
}
constexpr operator auto *() const {
return base::data();
}
constexpr operator std::string_view() const {
return {base::begin(), base::end() - 1}; // remove \0
}
#ifdef CAV_FOUND_FMT
constexpr operator fmt::string_view() const {
return {base::data(), base::size() - 1}; // remove \0
} // namespace
#endif
[[nodiscard]] static constexpr std::size_t size() {
return N;
}
[[nodiscard]] constexpr bool starts_with(auto const& prefix) const {
return static_cast<std::string_view>(*this).starts_with(
static_cast<std::string_view>(prefix));
}
};
template <size_t M, size_t N>
constexpr auto operator<=>(StaticStr<M> const& s1, StaticStr<N> const& s2) noexcept {
return std::string_view{s1} <=> std::string_view{s2};
}
template <size_t M, size_t N>
constexpr bool operator==(StaticStr<M> const& s1, StaticStr<N> const& s2) noexcept {
return std::string_view{s1} == std::string_view{s2};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// COMPILE TIME FACILITIES /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//// Number of elements of a c-string or array-string
template <typename>
struct size_of;
template <size_t N>
struct size_of<StaticStr<N>> {
static constexpr size_t value = N;
};
template <typename T, size_t N>
struct size_of<std::array<T, N>> {
static constexpr size_t value = N;
};
template <typename T, size_t N>
struct size_of<T[N]> {
static constexpr size_t value = N;
};
/// @brief Concatenate strings at compile time. Only c-array, std::array, and StaticStr can be
/// used (since the size must be known at compile time).
template <typename... Ts>
[[nodiscard]] constexpr auto str_concat(Ts&&... args) {
// Compute new size removing zero-terminations
constexpr size_t res_size = (size_of<no_cvr<Ts>>::value + ...) - sizeof...(Ts) + 1;
auto result = StaticStr<res_size>{};
auto it = result.begin();
auto copy_into = [&](auto constr_str) {
for (char c : constr_str) {
assert(it < result.end());
*it++ = c;
}
--it;
};
(copy_into(StaticStr(args)), ...);
*it = '\0';
assert(it + 1 == result.end());
return result;
}
template <typename ST1, typename ST2>
requires requires(ST1 s1, ST2 s2) {
{ StaticStr(s1) };
{ StaticStr(s2) };
}
[[nodiscard]] constexpr decl_auto operator+(ST1&& s1, ST2&& s2) {
return str_concat(StaticStr(FWD(s1)), StaticStr(FWD(s2)));
}
#ifdef CAV_COMP_TESTS
namespace {
static constexpr auto stra = StaticStr{"a"};
static constexpr char strb[] = "b";
static constexpr auto strc = std::array{'c', '\0'};
CAV_PASS(str_concat(stra) == StaticStr{"a"});
CAV_PASS(str_concat(strb) == StaticStr{"b"});
CAV_PASS(str_concat(strc) == StaticStr{"c"});
CAV_PASS(str_concat(stra, stra) == StaticStr{"aa"});
CAV_PASS(str_concat(strb, strb) == StaticStr{"bb"});
CAV_PASS(str_concat(strc, strc) == StaticStr{"cc"});
CAV_PASS(str_concat(stra, strc) == StaticStr{"ac"});
CAV_PASS(str_concat(strb, stra) == StaticStr{"ba"});
CAV_PASS(str_concat(strc, strb) == StaticStr{"cb"});
CAV_PASS(str_concat(stra, strb, strc) == StaticStr{"abc"});
} // namespace
#endif
template <std::integral auto Val>
[[nodiscard]] constexpr auto int_to_const_str() {
constexpr auto size = ilog10(Val) + 2;
StaticStr<size> result;
auto n = Val;
result.back() = '\0';
for (int i = size - 2; i >= 0; --i) {
result[i] = '0' + (n % 10);
n /= 10;
}
return result;
}
#ifdef CAV_COMP_TESTS
namespace {
CAV_PASS(int_to_const_str<12345>()[0] == '1');
CAV_PASS(int_to_const_str<12345>()[1] == '2');
CAV_PASS(int_to_const_str<12345>()[2] == '3');
CAV_PASS(int_to_const_str<12345>()[3] == '4');
CAV_PASS(int_to_const_str<12345>()[4] == '5');
CAV_PASS(int_to_const_str<12345>()[5] == '\0');
} // namespace
#endif
} // namespace cav
template <cav::StaticStr Str>
constexpr auto operator""_s() noexcept {
return Str;
}
template <cav::StaticStr Str>
constexpr cav::ct<Str> operator""_cs() noexcept {
return {};
}
#ifdef CAV_COMP_TESTS
namespace {
CAV_PASS(cav::StaticStr("test") == "test"_s);
CAV_PASS(cav::eq<cav::StaticStr<5>, TYPEOF("test"_s)>);
CAV_PASS("test"_s.starts_with(""_s));
CAV_PASS("test"_s.starts_with("t"_s));
CAV_PASS("test"_s.starts_with("test"_s));
CAV_FAIL("test"_s.starts_with("est"_s));
CAV_FAIL("test"_s.starts_with("test "_s));
} // namespace
#endif
#endif /* CAV_INCLUDE_STATICSTR_HPP */