-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathxbasic_fixed_string.hpp
More file actions
2437 lines (2073 loc) · 98.6 KB
/
xbasic_fixed_string.hpp
File metadata and controls
2437 lines (2073 loc) · 98.6 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) Sylvain Corlay and Johan Mabille and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTL_BASIC_FIXED_STRING_HPP
#define XTL_BASIC_FIXED_STRING_HPP
#include <cstddef>
#include <exception>
#include <functional>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <cassert>
#include <algorithm>
#include <type_traits>
#ifdef __CLING__
#include <nlohmann/json.hpp>
#endif
#include "xhash.hpp"
#include "xtl_config.hpp"
namespace xtl
{
namespace string_policy
{
template <std::size_t>
struct silent_error;
template <std::size_t>
struct throwing_error;
}
/***********************
* xbasic_fixed_string *
***********************/
enum storage_options
{
buffer = 1 << 0,
pointer = 1 << 1,
store_size = 1 << 2,
is_const = 1 << 3
};
template <class CT, std::size_t N = 55, int ST = buffer | store_size, template <std::size_t> class EP = string_policy::silent_error, class TR = std::char_traits<CT>>
class xbasic_fixed_string;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_ostream<CT, TR>& operator<<(std::basic_ostream<CT, TR>& os,
const xbasic_fixed_string<CT, N, ST, EP, TR>& str);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_istream<CT, TR>& operator>>(std::basic_istream<CT, TR>& is,
xbasic_fixed_string<CT, N, ST, EP, TR>& str);
template <class CT>
using xbasic_string_view = xbasic_fixed_string<const CT, 0, pointer | store_size | is_const>;
namespace detail
{
template <int selector>
struct select_storage;
template <typename T>
struct fixed_small_string_storage_impl;
template <class T, std::size_t N>
struct fixed_small_string_storage_impl<T[N]>
{
static_assert(N <= (1u << (8 * sizeof(T))), "small string");
fixed_small_string_storage_impl()
{
set_size(0);
}
fixed_small_string_storage_impl(T ptr[N], std::size_t size)
: m_buffer(ptr)
{
m_buffer[N - 1] = N - size;
}
T* buffer()
{
return m_buffer;
}
const T* buffer() const
{
return m_buffer;
}
std::size_t size() const
{
// Don't use std::make_unsinged_t here, this should remain C++11 compatible
using unsigned_type = typename std::make_unsigned<T>::type;
return N - reinterpret_cast<unsigned_type const*>(m_buffer)[N - 1];
}
void set_size(std::size_t sz)
{
assert(sz < N && "setting a small size");
// Don't use std::make_unsinged_t here, this should remain C++11 compatible
using unsigned_type = typename std::make_unsigned<T>::type;
reinterpret_cast<unsigned_type*>(m_buffer)[N - 1] = static_cast<unsigned_type>(N - sz);
m_buffer[sz] = '\0';
}
void adjust_size(std::ptrdiff_t val)
{
assert(size() + val >= 0 && "adjusting to positive size");
set_size(static_cast<std::size_t>(static_cast<std::ptrdiff_t>(size()) + val));
}
T m_buffer[N];
};
template <class T>
struct fixed_string_storage_impl
{
fixed_string_storage_impl() = default;
fixed_string_storage_impl(T ptr, std::size_t size)
: m_buffer(ptr), m_size(size)
{
}
T& buffer()
{
return m_buffer;
}
const T& buffer() const
{
return m_buffer;
}
std::size_t size() const
{
return m_size;
}
void set_size(std::size_t sz)
{
m_size = sz;
m_buffer[sz] = '\0';
}
void adjust_size(std::ptrdiff_t val)
{
m_size += std::size_t(val);
m_buffer[m_size] = '\0';
}
T m_buffer;
std::size_t m_size;
};
template <class T>
struct fixed_string_external_storage_impl
{
fixed_string_external_storage_impl() = default;
fixed_string_external_storage_impl(T ptr, std::ptrdiff_t/*size*/)
{
m_buffer = ptr;
}
T& buffer()
{
return m_buffer;
}
const T& buffer() const
{
return m_buffer;
}
void set_size(std::size_t sz)
{
m_buffer[sz] = '\0';
}
void adjust_size(std::ptrdiff_t val)
{
m_buffer[size() + val] = '\0';
}
std::size_t size() const
{
return std::strlen(m_buffer);
}
T m_buffer;
};
template <class T, bool Small>
struct select_fixed_storage {
using type = fixed_string_storage_impl<T>;
};
template <class T>
struct select_fixed_storage<T, true> {
using type = fixed_small_string_storage_impl<T>;
};
template <>
struct select_storage<buffer | store_size>
{
template <class T, std::size_t N>
using type = typename select_fixed_storage<T[N + 1], N < (1ull << (8 * sizeof(T)))>::type;
};
template <>
struct select_storage<buffer>
{
template <class T, std::size_t N>
using type = fixed_string_external_storage_impl<T[N + 1]>;
};
}
template <class CT,
std::size_t N,
int ST,
template <std::size_t> class EP,
class TR>
class xbasic_fixed_string
{
public:
using traits_type = TR;
using value_type = CT;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using storage_type = typename detail::select_storage<ST>::template type<CT, N>;
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;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static const size_type npos;
using self_type = xbasic_fixed_string;
using initializer_type = std::initializer_list<value_type>;
using string_type = std::basic_string<value_type, traits_type>;
using error_policy = EP<N>;
xbasic_fixed_string();
explicit xbasic_fixed_string(size_type count, value_type ch);
explicit xbasic_fixed_string(const self_type& other,
size_type pos,
size_type count = npos);
explicit xbasic_fixed_string(const string_type& other);
explicit xbasic_fixed_string(const string_type& other,
size_type pos,
size_type count = npos);
xbasic_fixed_string(const_pointer s, size_type count);
xbasic_fixed_string(const_pointer s);
xbasic_fixed_string(initializer_type ilist);
template <class InputIt>
xbasic_fixed_string(InputIt first, InputIt last);
operator string_type() const;
~xbasic_fixed_string() = default;
xbasic_fixed_string(const self_type&) = default;
xbasic_fixed_string(self_type&&) = default;
self_type& operator=(const self_type&) = default;
self_type& operator=(self_type&&) = default;
self_type& operator=(const_pointer s);
self_type& operator=(value_type ch);
self_type& operator=(initializer_type ilist);
self_type& operator=(const string_type& str);
self_type& assign(size_type count, value_type ch);
self_type& assign(const self_type& other,
size_type pos,
size_type count = npos);
self_type& assign(const_pointer s, size_type count);
self_type& assign(const_pointer s);
self_type& assign(initializer_type ilist);
template <class InputIt>
self_type& assign(InputIt first, InputIt last);
self_type& assign(const self_type& rhs);
self_type& assign(self_type&& rhs);
self_type& assign(const string_type& str);
self_type& assign(const string_type& other,
size_type pos,
size_type count = npos);
reference at(size_type pos);
const_reference at(size_type pos) const;
reference operator[](size_type pos);
const_reference operator[](size_type pos) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
pointer data() noexcept;
const_pointer data() const noexcept;
const_pointer c_str() const noexcept;
iterator begin() noexcept;
iterator end() noexcept;
const_iterator begin() const noexcept;
const_iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
reverse_iterator rbegin() noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
bool empty() const noexcept;
size_type size() const noexcept;
size_type length() const noexcept;
size_type max_size() const noexcept;
void clear() noexcept;
void push_back(value_type ch);
void pop_back();
self_type substr(size_type pos = 0, size_type count = npos) const;
size_type copy(pointer dest, size_type count, size_type pos = 0) const;
void resize(size_type count);
void resize(size_type count, value_type ch);
void swap(self_type& rhs) noexcept;
self_type& insert(size_type index, size_type count, value_type ch);
self_type& insert(size_type index, const_pointer s);
self_type& insert(size_type index, const_pointer s, size_type count);
self_type& insert(size_type index, const self_type& str);
self_type& insert(size_type index, const self_type& str,
size_type index_str, size_type count = npos);
self_type& insert(size_type index, const string_type& str);
self_type& insert(size_type index, const string_type& str,
size_type index_str, size_type count = npos);
iterator insert(const_iterator pos, value_type ch);
iterator insert(const_iterator pos, size_type count, value_type ch);
iterator insert(const_iterator pos, initializer_type ilist);
template <class InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last);
self_type& erase(size_type index = 0, size_type count = npos);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
self_type& append(size_type count, value_type ch);
self_type& append(const self_type& str);
self_type& append(const self_type& str,
size_type pos, size_type count = npos);
self_type& append(const string_type& str);
self_type& append(const string_type& str,
size_type pos, size_type count = npos);
self_type& append(const_pointer s, size_type count);
self_type& append(const_pointer s);
self_type& append(initializer_type ilist);
template <class InputIt>
self_type& append(InputIt first, InputIt last);
self_type& operator+=(const self_type& str);
self_type& operator+=(const string_type& str);
self_type& operator+=(value_type ch);
self_type& operator+=(const_pointer s);
self_type& operator+=(initializer_type ilist);
int compare(const self_type& str) const noexcept;
int compare(size_type pos1, size_type count1, const self_type& str) const;
int compare(size_type pos1, size_type count1, const self_type& str,
size_type pos2, size_type count2 = npos) const;
int compare(const string_type& str) const noexcept;
int compare(size_type pos1, size_type count1, const string_type& str) const;
int compare(size_type pos1, size_type count1, const string_type& str,
size_type pos2, size_type count2 = npos) const;
int compare(const_pointer s) const noexcept;
int compare(size_type pos1, size_type count1, const_pointer s) const;
int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const;
self_type& replace(size_type pos, size_type count, const self_type& str);
self_type& replace(const_iterator first, const_iterator last, const self_type& str);
self_type& replace(size_type pos1, size_type count1, const self_type& str,
size_type pos2, size_type count2 = npos);
self_type& replace(size_type pos, size_type count, const string_type& str);
self_type& replace(const_iterator first, const_iterator last, const string_type& str);
self_type& replace(size_type pos1, size_type count1, const string_type& str,
size_type pos2, size_type count2 = npos);
self_type& replace(size_type pos, size_type count, const_pointer cstr, size_type count2);
self_type& replace(const_iterator first, const_iterator last, const_pointer cstr, size_type count2);
self_type& replace(size_type pos, size_type count, const_pointer cstr);
self_type& replace(const_iterator first, const_iterator last, const_pointer cstr);
self_type& replace(size_type pos, size_type count, size_type count2, value_type ch);
self_type& replace(const_iterator first, const_iterator last, size_type count2, value_type ch);
self_type& replace(const_iterator first, const_iterator last, initializer_type ilist);
template <class InputIt>
self_type& replace(const_iterator first, const_iterator last, InputIt first2, InputIt last2);
size_type find(const self_type& str, size_type pos = 0) const noexcept;
size_type find(const string_type& str, size_type pos = 0) const noexcept;
size_type find(const_pointer s, size_type pos, size_type count) const;
size_type find(const_pointer s, size_type pos = 0) const;
size_type find(value_type ch, size_type pos = 0) const;
size_type rfind(const self_type& str, size_type pos = npos) const noexcept;
size_type rfind(const string_type& str, size_type pos = npos) const noexcept;
size_type rfind(const_pointer s, size_type pos, size_type count) const;
size_type rfind(const_pointer s, size_type pos = npos) const;
size_type rfind(value_type ch, size_type pos = npos) const;
size_type find_first_of(const self_type& str, size_type pos = 0) const noexcept;
size_type find_first_of(const string_type& str, size_type pos = 0) const noexcept;
size_type find_first_of(const_pointer s, size_type pos, size_type count) const;
size_type find_first_of(const_pointer s, size_type pos = 0) const;
size_type find_first_of(value_type ch, size_type pos = 0) const;
size_type find_first_not_of(const self_type& str, size_type pos = 0) const noexcept;
size_type find_first_not_of(const string_type& str, size_type pos = 0) const noexcept;
size_type find_first_not_of(const_pointer s, size_type pos, size_type count) const;
size_type find_first_not_of(const_pointer s, size_type pos = 0) const;
size_type find_first_not_of(value_type ch, size_type pos = 0) const;
size_type find_last_of(const self_type& str, size_type pos = 0) const noexcept;
size_type find_last_of(const string_type& str, size_type pos = 0) const noexcept;
size_type find_last_of(const_pointer s, size_type pos, size_type count) const;
size_type find_last_of(const_pointer s, size_type pos = 0) const;
size_type find_last_of(value_type ch, size_type pos = 0) const;
size_type find_last_not_of(const self_type& str, size_type pos = npos) const noexcept;
size_type find_last_not_of(const string_type& str, size_type pos = npos) const noexcept;
size_type find_last_not_of(const_pointer s, size_type pos, size_type count) const;
size_type find_last_not_of(const_pointer s, size_type pos = npos) const;
size_type find_last_not_of(value_type ch, size_type pos = npos) const;
private:
int compare_impl(const_pointer s1, size_type count1, const_pointer s2, size_type count2) const noexcept;
void update_null_termination() noexcept;
void check_index(size_type pos, size_type size, const char* what) const;
void check_index_strict(size_type pos, size_type size, const char* what) const;
storage_type m_storage;
};
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
const typename xbasic_fixed_string<CT, N, ST, EP, TR>::size_type xbasic_fixed_string<CT, N, ST, EP, TR>::npos
= std::basic_string<value_type, traits_type>::npos;
template <std::size_t N>
using xfixed_string = xbasic_fixed_string<char, N>;
template <std::size_t N>
using xwfixed_string = xbasic_fixed_string<wchar_t, N>;
template <std::size_t N>
using xu16fixed_string = xbasic_fixed_string<char16_t, N>;
template <std::size_t N>
using xu32fixed_string = xbasic_fixed_string<char32_t, N>;
/**************************
* Concatenation operator *
**************************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
CT rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(CT lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>&& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>&& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>&& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>&& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>&& lhs,
const CT* rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const xbasic_fixed_string<CT, N, ST, EP, TR>&& lhs,
CT rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>&& rhs);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
xbasic_fixed_string<CT, N, ST, EP, TR>
operator+(CT lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>&& rhs);
/************************
* Comparison operators *
************************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator==(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator==(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator==(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator==(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator==(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator!=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator!=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator!=(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator!=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator!=(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<=(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator<=(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const CT* rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>=(const CT* lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>=(const xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
const std::basic_string<CT, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
bool operator>=(const std::basic_string<CT, TR>& lhs,
const xbasic_fixed_string<CT, N, ST, EP, TR>& rhs) noexcept;
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
void swap(xbasic_fixed_string<CT, N, ST, EP, TR>& lhs,
xbasic_fixed_string<CT, N, ST, EP, TR>& rhs);
/******************************
* Input / output declaration *
******************************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_istream<CT, TR>& getline(std::basic_istream<CT, TR>& input,
xbasic_fixed_string<CT, N, ST, EP, TR>& str,
CT delim);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_istream<CT, TR>& getline(std::basic_istream<CT, TR>&& input,
xbasic_fixed_string<CT, N, ST, EP, TR>& str,
CT delim);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_istream<CT, TR>& getline(std::basic_istream<CT, TR>& input,
xbasic_fixed_string<CT, N, ST, EP, TR>& str);
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
std::basic_istream<CT, TR>& getline(std::basic_istream<CT, TR>&& input,
xbasic_fixed_string<CT, N, ST, EP, TR>& str);
} // namespace xtl
namespace std
{
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
struct hash<::xtl::xbasic_fixed_string<CT, N, ST, EP, TR>>
{
using argument_type = ::xtl::xbasic_fixed_string<CT, N, ST, EP, TR>;
using result_type = std::size_t;
inline result_type operator()(const argument_type& arg) const
{
return ::xtl::hash_bytes(arg.data(), arg.size(), static_cast<std::size_t>(0xc70f6907UL));
}
};
} // namespace std
namespace xtl
{
/********************************
* xbasic_fixed_string policies *
********************************/
namespace string_policy
{
template <std::size_t N>
struct silent_error
{
inline static std::size_t check_size(std::size_t size)
{
return size;
}
inline static std::size_t check_add(std::size_t size1, std::size_t size2)
{
return size1 + size2;
}
};
template <std::size_t N>
struct throwing_error
{
inline static std::size_t check_size(std::size_t size)
{
if (size > N)
{
std::ostringstream oss;
oss << "Invalid size (" << size << ") for xbasic_fixed_string - maximal size: " << N;
#if defined(XTL_NO_EXCEPTIONS)
std::fprintf(stderr, "%s\n", oss.str().c_str());
std::terminate();
#else
throw std::length_error(oss.str());
#endif
}
return size;
}
inline static std::size_t check_add(std::size_t size1, std::size_t size2)
{
return check_size(size1 + size2);
}
};
} // string_policy
/**************************************
* xbasic_fixed_string implementation *
**************************************/
/****************
* Constructors *
****************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string()
: m_storage()
{
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(size_type count, value_type ch)
: m_storage()
{
assign(count, ch);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(const self_type& other,
size_type pos,
size_type count)
: m_storage()
{
assign(other, pos, count);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(const string_type& other)
: m_storage()
{
assign(other);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(const string_type& other,
size_type pos,
size_type count)
: m_storage()
{
assign(other, pos, count);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(const_pointer s, size_type count)
: m_storage()
{
assign(s, count);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(const_pointer s)
: m_storage()
{
assign(s);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(initializer_type ilist)
: m_storage()
{
assign(ilist);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
template <class InputIt>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::xbasic_fixed_string(InputIt first, InputIt last)
: m_storage()
{
assign(first, last);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline xbasic_fixed_string<CT, N, ST, EP, TR>::operator string_type() const
{
return string_type(data());
}
/**************
* Assignment *
**************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator=(const_pointer s) -> self_type&
{
return assign(s);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator=(value_type ch) -> self_type&
{
return assign(size_type(1), ch);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator=(initializer_type ilist) -> self_type&
{
return assign(ilist);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator=(const string_type& str) -> self_type&
{
return assign(str);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(size_type count, value_type ch) -> self_type&
{
m_storage.set_size(error_policy::check_size(count));
traits_type::assign(data(), count, ch);
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const self_type& other,
size_type pos,
size_type count) -> self_type&
{
check_index_strict(pos, other.size(), "xbasic_fixed_string::assign");
size_type copy_count = std::min(other.size() - pos, count);
m_storage.set_size(error_policy::check_size(copy_count));
traits_type::copy(data(), other.data() + pos, copy_count);
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const_pointer s, size_type count) -> self_type&
{
m_storage.set_size(error_policy::check_size(count));
traits_type::copy(data(), s, count);
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const_pointer s) -> self_type&
{
std::size_t ssize = traits_type::length(s);
return assign(s, ssize);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(initializer_type ilist) -> self_type&
{
return assign(ilist.begin(), ilist.end());
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
template <class InputIt>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(InputIt first, InputIt last) -> self_type&
{
auto size = error_policy::check_size(static_cast<size_type>(std::distance(first, last)));
m_storage.set_size(size);
std::copy_n(first, size, data());
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const self_type& rhs) -> self_type&
{
if (this != &rhs)
{
m_storage.set_size(rhs.size());
traits_type::copy(data(), rhs.data(), rhs.size());
}
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(self_type&& rhs) -> self_type&
{
if (this != &rhs)
{
m_storage.set_size(rhs.size());
traits_type::copy(data(), rhs.data(), rhs.size());
}
return *this;
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const string_type& other) -> self_type&
{
return assign(other.c_str());
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::assign(const string_type& other,
size_type pos,
size_type count) -> self_type&
{
return assign(other.c_str() + pos, std::min(count, other.size() - pos));
}
/******************
* Element access *
******************/
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::at(size_type pos) -> reference
{
check_index(pos, size(), "basic_fixed_string::at");
return this->operator[](pos);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::at(size_type pos) const -> const_reference
{
check_index(pos, size(), "basic_fixed_string::at");
return this->operator[](pos);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator[](size_type pos) -> reference
{
return data()[pos];
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::operator[](size_type pos) const -> const_reference
{
return data()[pos];
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::front() -> reference
{
return this->operator[](0);
}
template <class CT, std::size_t N, int ST, template <std::size_t> class EP, class TR>
inline auto xbasic_fixed_string<CT, N, ST, EP, TR>::front() const -> const_reference
{
return this->operator[](0);