-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathctstring.hpp
335 lines (268 loc) · 7.85 KB
/
ctstring.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
327
328
329
330
331
332
333
334
/**
@file
Compile-time string
@copyright Denis Priyomov 2017
Distributed under the MIT License
(See accompanying file LICENSE or visit https://github.com/cppden/ctstring)
*/
#include <cstdint>
#include <utility>
#include <type_traits>
#include "string.hpp"
namespace cts {
constexpr char to_upper(char const c)
{
return (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c;
}
constexpr char to_lower(char const c)
{
return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
}
constexpr bool is_equal(char const c1, char const c2)
{
return c1 == c2;
}
constexpr bool is_iequal(char const c1, char const c2)
{
return c1 == c2 || to_upper(c1) == c2;
}
namespace {
template <char... CHARS>
using chars = std::integer_sequence<char, CHARS...>;
template <std::size_t... INDEXES>
using indexes = std::index_sequence<INDEXES...>;
template <class... ICs>
struct sequence {};
template <std::size_t I, std::size_t C>
struct ic //index + char
{
static constexpr std::size_t my_index = I;
static constexpr std::size_t my_char = C;
};
template <class Map, std::size_t ...Is>
struct zip
{
template <char ...Cs>
struct with
{
using type = sequence<ic<Is, Map::apply(Cs, Is)>...>;
};
};
struct plain
{
static constexpr std::size_t apply(std::size_t c, std::size_t)
{
return c;
}
};
template <class Map, std::size_t... Is, char... Cs>
constexpr auto map_index(indexes<Is...>, chars<Cs...>) -> typename zip<Map, Is...>::template with<Cs...>::type;
template <char... Cs>
using indexed_chars = decltype( map_index<plain>(std::make_index_sequence<sizeof...(Cs)>{}, chars<Cs...>{}) );
//TODO: replace with fold expression in c++17
template <class FUNC>
constexpr bool apply_impl(FUNC&& func, char const* p) { return true; }
template <class FUNC, class IC, class... ICs>
constexpr bool apply_impl(FUNC&& func, char const* p)
{
return func(p[IC::my_index], IC::my_char) && apply_impl<FUNC, ICs...>(std::forward<FUNC>(func), p);
}
template <class FUNC, class... ICs>
constexpr bool apply(FUNC&& func, char const* p, sequence<ICs...>)
{
return apply_impl<FUNC, ICs...>(std::forward<FUNC>(func), p);
}
template <class FUNC>
constexpr bool apply_impl(FUNC&& func, char const* p, std::size_t offset) { return true; }
template <class FUNC, class IC, class... ICs>
constexpr bool apply_impl(FUNC&& func, char const* p, std::size_t offset)
{
if (offset)
{
return apply_impl<FUNC, ICs...>(std::forward<FUNC>(func), p, offset - 1);
}
else
{
return func(p[IC::my_index], IC::my_char) && apply_impl<FUNC, ICs...>(std::forward<FUNC>(func), p);
}
}
template <class FUNC, class... ICs>
constexpr bool apply(FUNC&& func, char const* p, sequence<ICs...>, std::size_t offset)
{
return apply_impl<FUNC, ICs...>(std::forward<FUNC>(func), p, offset);
}
} //end: namespace
template <char... CHARS>
class Chars
{
public:
static constexpr std::size_t length() { return sizeof...(CHARS); }
using type = indexed_chars<CHARS...>;
constexpr bool match(char const* begin, char const* end) const
{
return (begin + length() <= end)
? apply(is_equal, begin, type{})
: false;
}
constexpr bool match(char const* begin, char const* end, std::size_t offset) const
{
return (begin + length() <= end && offset < length())
? apply(is_equal, begin, type{}, offset)
: false;
}
static constexpr char const* c_str()
{
return m_string;
}
private:
static constexpr char m_string[] = {CHARS..., 0};
};
template <char... CHARS>
constexpr char Chars<CHARS...>::m_string[];
template <char... CHARS>
class CaseChars
{
public:
static constexpr std::size_t length() { return sizeof...(CHARS); }
using type = indexed_chars<CHARS...>;
constexpr bool match(char const* begin, char const* end) const
{
return (begin + length() <= end)
? apply(is_iequal, begin, type{})
: false;
}
constexpr bool match(char const* begin, char const* end, std::size_t offset) const
{
return (begin + length() <= end && offset < length())
? apply(is_iequal, begin, type{}, offset)
: false;
}
static constexpr char const* c_str()
{
return m_string;
}
private:
static constexpr char m_string[] = {to_upper(CHARS)..., 0};
};
template <char... CHARS>
constexpr char CaseChars<CHARS...>::m_string[];
namespace {
#ifndef CTS_RND_SEED
#define CTS_TIME(index) ((__TIME__[index]-'0')*10 + (__TIME__[index+1]-'0'))
#define CTS_RND_SEED uint32_t(CTS_TIME(0)*3600 + CTS_TIME(3)*60 + CTS_TIME(6) + 13709*(31+ __COUNTER__))
#endif
constexpr std::size_t STATE_OFFSET = 137;
constexpr std::size_t CHAR_SHIFT_MOD = 7;
//https://en.wikipedia.org/wiki/Linear_congruential_generator
//LCG: X(n + 1) = (A * X(n) + C) % m
constexpr std::size_t rand(std::size_t n)
{
//NOTE: cast makes the mod 2^32
return static_cast<uint32_t>(1664525u * (n ? rand(n - 1) : CTS_RND_SEED) + 1013904223u);
}
constexpr std::size_t rand_next(std::size_t rand_curr)
{
//NOTE: cast makes the mod 2^32
return static_cast<uint32_t>(1664525u * rand_curr + 1013904223u);
}
/*
a+b = (a ^ b) + 2*(a | b)
a-b = (a ^ b) - 2*(~a | b)
a*a(a+1)^2 % 4 = 0
(a*a*a -3) % 3 = 0
a + b >= a ^ b
7*a*a - 1 != b*b
*/
struct xmap
{
static constexpr std::size_t apply(std::size_t c, std::size_t index)
{
return ((c + index) << (index % CHAR_SHIFT_MOD)) ^ rand(index + STATE_OFFSET);
}
};
//__attribute__((noinline))
#ifdef __clang__
__attribute__((optnone))
#else
__attribute__((optimize(0)))
#endif
inline std::size_t decode(std::size_t c, std::size_t index, std::size_t state)
{
return ((c ^ state) >> (index % CHAR_SHIFT_MOD)) - index;
}
template <char... Cs>
using xchars = decltype( map_index<xmap>(std::make_index_sequence<sizeof...(Cs)>{}, chars<Cs...>{}) );
//[[gnu::visibility("hidden")]]
template <std::size_t STATE>
constexpr void ximpl(char* p) { }
template <std::size_t STATE, class IC, class... ICs>
__attribute__((always_inline, visibility("internal")))
inline void ximpl(char* p)
{
p[IC::my_index] = static_cast<char>(decode(IC::my_char, IC::my_index, STATE));
ximpl<rand_next(STATE), ICs...>(p);
}
template <class... ICs>
__attribute__((always_inline, visibility("internal")))
inline void xapply(char* p, sequence<ICs...>)
{
return ximpl<rand(STATE_OFFSET), ICs...>(p);
}
} //end: namespace
//obfuscated string via XOR
template <char... CHARS>
class XChars
{
using type = xchars<CHARS...>;
public:
static constexpr std::size_t size() { return sizeof...(CHARS); }
static constexpr std::size_t length() { return size(); }
//de-obfuscate at run-time into string on heap which will clean up its data in dtor
__attribute__((always_inline, visibility("internal")))
string str() const
{
string result{ this->size() };
xapply(result.data(), type{});
return result;
}
//de-obfuscate at run-time avoiding intermediate copy
//but you should clean-up buffer once it's used and no longer needed to erase protected data from memory
__attribute__((always_inline, visibility("internal")))
char* str(void* buffer, std::size_t buf_size) const
{
if (buf_size >= size())
{
auto* p = static_cast<char*>(buffer);
xapply(p, type{});
if (buf_size > size()) //NULL terminate if space allows
{
p[size()] = '\0';
}
return p;
}
return nullptr;
}
template <typename T, std::size_t QTY>
__attribute__((always_inline, visibility("internal")))
char* str(T (&buffer)[QTY]) const
{
static_assert(sizeof(buffer) >= size(), "buffer is too small to fit the string");
return str(buffer, sizeof(buffer));
}
};
namespace literals {
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
#endif
template <typename T, T... CHARS>
constexpr cts::Chars<CHARS...> operator""_chars() { return { }; }
template <typename T, T... CHARS>
constexpr cts::CaseChars<CHARS...> operator""_ichars() { return { }; }
template <typename T, T... CHARS>
constexpr cts::XChars<CHARS...> operator""_xchars() { return { }; }
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} //end: namespace literals
} //end: namespace cts