-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.hpp
590 lines (503 loc) · 17.5 KB
/
string.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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* ************************************************************************ */
/* */
/* Copyright (C) 2015 Jiří Fatka <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser 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 Lesser General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* ************************************************************************ */
#pragma once
/* ************************************************************************ */
// C++
#include <type_traits>
#include <utility>
#include <iostream>
/* ************************************************************************ */
namespace template_regex {
/* ************************************************************************ */
/**
* @brief Returns character from string at given position.
*
* @tparam CharT Character type.
* @tparam N String literal size.
*
* @param str String literal.
* @param i Required string position. If value is greater than
* string length, zero is returned.
*
* @return Character at i-th position.
*/
template<typename CharT, unsigned N>
constexpr CharT char_at(const CharT (&str)[N], unsigned i) noexcept
{
return i < N ? str[i] : CharT{};
}
/* ************************************************************************ */
/**
* @brief Returns character from string at given position.
*
* @tparam I Character position.
* @tparam CharT Character type.
* @tparam N String literal size.
*
* @param str String literal.
*
* @return Character at i-th position.
*/
template<unsigned I, typename CharT, unsigned N>
constexpr CharT char_at(const CharT (&str)[N]) noexcept
{
return I < N ? str[I] : CharT{};
}
/* ************************************************************************ */
/**
* @brief Returns N-th character from variadic pack.
*
* @tparam N Required character position.
* @tparam CharT Character type.
* @tparam Values A list of characters.
*/
template<unsigned N, typename CharT, CharT... Values>
struct character_at
{
/// N-th character.
static constexpr CharT value = std::conditional<
(sizeof...(Values) > 0),
std::conditional<true, character_at<N - 1, CharT, Values...>, void>,
std::integral_constant<CharT, CharT{}>
>::type::value;
};
/* ************************************************************************ */
/**
* @brief Returns N-th character from variadic pack (specialization).
*
* @tparam N Required character position.
* @tparam CharT Character type.
* @tparam Value The first character.
* @tparam Values A list of characters.
*/
template<unsigned N, typename CharT, CharT Value, CharT... Values>
struct character_at<N, CharT, Value, Values...>
{
static constexpr CharT value = std::conditional<
(N > 0),
std::integral_constant<CharT, character_at<N - 1, CharT, Values...>::value>,
std::integral_constant<CharT, Value>
>::type::value;
};
/* ************************************************************************ */
/**
* @brief Container for character sequence.
*
* This structure is able to store variadic sequence of characters and can
* be used in templates specialization for separation of parameter packs.
*
* @tparam CharT Character type.
* @tparam Chars Sequence of characters.
*/
template<typename CharT, CharT... Chars>
struct basic_string
{
using value_type = CharT;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
/**
* @brief Checks if the string has no characters.
*
* @return true if the string is empty, false otherwise.
*/
static constexpr bool empty() noexcept
{
return size() == 0;
}
/**
* @brief Returns the number of CharT elements in the string.
*
* @return The number of CharT elements in the string.
*/
static constexpr size_type size() noexcept
{
return sizeof...(Chars);
}
/**
* @brief Returns the number of CharT elements in the string.
*
* @return The number of CharT elements in the string.
*/
static constexpr size_type length() noexcept
{
return size();
}
/**
* @brief Returns character at given position.
*
* @tparam I Character position.
*
* @return
*/
template<size_t I>
static constexpr CharT at() noexcept
{
return character_at<I, CharT, Chars...>::value;
}
};
/* ************************************************************************ */
/**
* @brief Returns character at given position.
*
* @tparam I Character position.
* @tparam Str Source string.
*/
template<unsigned I, class Str>
struct character_at_str;
/* ************************************************************************ */
/**
* @brief Returns character at given position.
*
* @tparam I Character position.
*/
template<unsigned I, typename CharT, CharT... Chars>
struct character_at_str<I, basic_string<CharT, Chars...>>
: character_at<I, CharT, Chars...>
{
// Nothing
};
/* ************************************************************************ */
/**
* @brief Appends character to the string.
*
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam Char Character to append.
*/
template<typename CharT, typename Str, CharT Char>
struct basic_string_append;
/* ************************************************************************ */
/**
* @brief Appends character to the string.
*
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam Char Character to append.
*/
template<typename CharT, CharT... Chars, CharT Char>
struct basic_string_append<CharT, basic_string<CharT, Chars...>, Char>
{
using type = basic_string<CharT, Chars..., Char>;
};
/* ************************************************************************ */
/**
* @brief Template string builder.
*
* @tparam N Number of valid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str, CharT... Chars>
struct basic_string_builder;
/* ************************************************************************ */
/**
* @brief Template string builder.
*
* @tparam N Number of valid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam CharX Transfered character.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str, CharT CharX, CharT... Chars>
struct basic_string_builder<N, CharT, Str, CharX, Chars...>
{
static_assert((sizeof...(Chars) + 1 > N), "No more characters");
/// Trimmed string type.
/// This is only one solution that I found.
/// Recursive solution with stop specialization doesn't work due
/// ambiguity.
/// Inner string builder cannot have "type" directly because that
/// causes template instantiation that is not needed (and causes
/// an error).
using type = typename std::conditional<
(N > 0),
basic_string_builder<N - 1, CharT, typename basic_string_append<CharT, Str, CharX>::type, Chars...>,
// Yeah ugly, but I needed something that have a member type named 'type'.
std::conditional<true, Str, void>
>::type::type;
};
/* ************************************************************************ */
/**
* @brief Template string builder.
*
* @tparam N Number of valid characters.
* @tparam CharT Character type.
* @tparam Str1 String type.
* @tparam Str2 String type.
*/
template<unsigned N, typename CharT, typename Str1, typename Str2>
struct basic_string_builder_str;
/* ************************************************************************ */
/**
* @brief Template string builder.
*
* @tparam N Number of valid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam CharX Transfered character.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str1, CharT... Chars>
struct basic_string_builder_str<N, CharT, Str1, basic_string<CharT, Chars...>>
: basic_string_builder<N, CharT, Str1, Chars...> {};
/* ************************************************************************ */
/**
* @brief Template string builder (skip N characters).
*
* @tparam N Number of invalid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str, CharT... Chars>
struct basic_string_builder_rest;
/* ************************************************************************ */
/**
* @brief Template string builder (skip N characters).
*
* @tparam N Number of invalid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam CharX Transfered character.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str, CharT CharX, CharT... Chars>
struct basic_string_builder_rest<N, CharT, Str, CharX, Chars...>
{
static_assert((sizeof...(Chars) + 1 >= N), "No more characters");
/// String type without N character at the begin.
using type = typename std::conditional<
(N > 0),
basic_string_builder_rest<N - 1, CharT, typename basic_string_append<CharT, Str, CharX>::type, Chars...>,
// Yeah ugly, but I needed something that have a member type named 'type'.
std::conditional<true, basic_string<CharT, CharX, Chars...>, void>
>::type::type;
};
/* ************************************************************************ */
/**
* @brief Template string builder (skip N characters).
*
* @tparam N Number of invalid characters.
* @tparam CharT Character type.
* @tparam Str String type.
*/
template<unsigned N, typename CharT, typename Str>
struct basic_string_builder_rest<N, CharT, Str>
{
/// No more characters.
using type = basic_string<CharT>;
};
/* ************************************************************************ */
/**
* @brief Template string builder (skip N characters).
*
* @tparam N Number of invalid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str1, typename Str2>
struct basic_string_builder_rest_str;
/* ************************************************************************ */
/**
* @brief Template string builder (skip N characters).
*
* @tparam N Number of invalid characters.
* @tparam CharT Character type.
* @tparam Str String type.
* @tparam CharX Transfered character.
* @tparam Chars Characters.
*/
template<unsigned N, typename CharT, typename Str1, CharT... Chars>
struct basic_string_builder_rest_str<N, CharT, Str1, basic_string<CharT, Chars...>>
: basic_string_builder_rest<N, CharT, Str1, Chars...> {};
/* ************************************************************************ */
/**
* @brief Separates string into two parts.
*
* @tparam N Number of characters from string start.
* @tparam CharT Character type.
* @tparam Str Source string.
*/
template<unsigned N, typename CharT, typename Str>
struct basic_string_split;
/* ************************************************************************ */
/**
* @brief Separates string into two parts.
*
* @tparam N Number of characters from string start.
* @tparam CharT Character type.
* @tparam Str Source string.
*/
template<unsigned N, typename CharT, CharT... Chars>
struct basic_string_split<N, CharT, basic_string<CharT, Chars...>>
{
/// The first part (N characters) of the string
using first = typename basic_string_builder<N, CharT, basic_string<CharT>, Chars...>::type;
// TODO second part
};
/* ************************************************************************ */
/**
* @brief Finds character in the string and returns it's position.
*/
template<typename CharT, typename Str, CharT Char>
struct basic_string_find
{
static constexpr unsigned value = 0;
};
/* ************************************************************************ */
/**
* @brief Alias for char string.
*
* @tparam Chars String characters.
*/
template<char... Chars>
using string = basic_string<char, Chars...>;
/* ************************************************************************ */
/**
* @brief Alias for wchar_t string.
*
* @tparam Chars String characters.
*/
template<wchar_t... Chars>
using wstring = basic_string<wchar_t, Chars...>;
/* ************************************************************************ */
/**
* @brief Alias for UTF-8 char string.
*
* @tparam Chars String characters.
*/
template<char... Chars>
using u8string = basic_string<char, Chars...>;
/* ************************************************************************ */
/**
* @brief Alias for UTF-16 char string.
*
* @tparam Chars String characters.
*/
template<char16_t... Chars>
using u16string = basic_string<char16_t, Chars...>;
/* ************************************************************************ */
/**
* @brief Alias for UTF-32 char string.
*
* @tparam Chars String characters.
*/
template<char32_t... Chars>
using u32string = basic_string<char32_t, Chars...>;
/* ************************************************************************ */
/* Helper macros to separate string into characters */
#define TEMPLATE_STRING_GET_C1(str, i) \
char_at(str, i)
#define TEMPLATE_STRING_GET_C2(str, i) \
TEMPLATE_STRING_GET_C1(str, i), TEMPLATE_STRING_GET_C1(str, i + 1)
#define TEMPLATE_STRING_GET_C4(str, i) \
TEMPLATE_STRING_GET_C2(str, i), TEMPLATE_STRING_GET_C2(str, i + 2)
#define TEMPLATE_STRING_GET_C8(str, i) \
TEMPLATE_STRING_GET_C4(str, i), TEMPLATE_STRING_GET_C4(str, i + 4)
#define TEMPLATE_STRING_GET_C16(str, i) \
TEMPLATE_STRING_GET_C8(str, i), TEMPLATE_STRING_GET_C8(str, i + 8)
#define TEMPLATE_STRING_GET_C32(str, i) \
TEMPLATE_STRING_GET_C16(str, i), TEMPLATE_STRING_GET_C16(str, i + 16)
#define TEMPLATE_STRING_GET_C64(str, i) \
TEMPLATE_STRING_GET_C32(str, i), TEMPLATE_STRING_GET_C32(str, i + 32)
/* ************************************************************************ */
/**
* @brief Default string literal expansion macro.
*/
#ifndef TEMPLATE_STRING_EXPAND
#define TEMPLATE_STRING_EXPAND(str) TEMPLATE_STRING_GET_C64(str, 0)
#endif
/* ************************************************************************ */
/**
* @brief Detect string character type.
*
* @param str String literal.
*
* @return Character type.
*/
#define TEMPLATE_STRING_CHAR_TYPE(str) decltype(char_at(str, 0))
/* ************************************************************************ */
/**
* @brief Creates template string object type.
*
* This is just a macro that will expand into very ugly code.
*
* @param str String literal.
*
* @return String type.
*
* @note Currently this is based on macro expansion and allows only
* up to 64 characters strings. If you need create string with more
* characters define own TEMPLATE_STRING_EXPAND macro.
*/
#define make_string_t(str) \
basic_string_builder< \
sizeof(str) / sizeof(TEMPLATE_STRING_CHAR_TYPE(str)) - 1, \
TEMPLATE_STRING_CHAR_TYPE(str), \
basic_string<TEMPLATE_STRING_CHAR_TYPE(str)>, \
TEMPLATE_STRING_EXPAND(str) \
>::type
/* ************************************************************************ */
/**
* @brief Creates template string object.
*
* @param str String literal.
*
* @return String object.
*
* @see `make_template_string_type`
*/
#define make_string(str) make_string_t(str){}
/* ************************************************************************ */
// This doesn't work because string literal cannot be forwared :(
#if 0
template<typename CharT, unsigned N, unsigned... Ints>
constexpr auto make_string_apply(const CharT (&str)[N], std::integer_sequence<unsigned, Ints...>)
{
return basic_string<CharT, str[Ints]...>{};
}
/* ************************************************************************ */
template<typename CharT, unsigned N>
constexpr auto make_string(const CharT (&str)[N])
{
return make_string_apply(str, std::make_integer_sequence<unsigned, N>{});
}
#endif
/* ************************************************************************ */
/**
* @brief Prints a template string into stream.
*
* @param os Output stream.
* @param str Printed string.
*
* @return Output stream.
*/
template<typename CharT, CharT... Chars>
std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os, const basic_string<CharT, Chars...>& str) noexcept
{
constexpr CharT buf[] = {Chars..., CharT()};
os << buf;
return os;
}
/* ************************************************************************ */
}
/* ************************************************************************ */