-
Notifications
You must be signed in to change notification settings - Fork 3
/
strcore.hh
1130 lines (920 loc) · 34.3 KB
/
strcore.hh
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) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # String (`str[buf]`) and `concat(...)` function
// Owns a contiguous sequence of `char` objects.
// Two strings are currently available (different buffers):
// `str` which is an alias for `strcore<detail::strcore::sso>`:
// * Small string optimization (SSO).
// * Null-terminated.
// `strbuf` which is an alias for `strcore<detail::strcore::buf>`:
// * Without SSO.
// * Not null-terminated.
// `strcore` owns a buffer and is the interface for all strings.
#pragma once
#include "snn-core/array.hh"
#include "snn-core/array_view.hh"
#include "snn-core/contiguous_interface.hh"
#include "snn-core/null_term.hh"
#include "snn-core/strcore.fwd.hh"
#include "snn-core/detail/strcore/common.hh"
#include "snn-core/fn/common.hh"
#include "snn-core/hex/table/common.hh"
#include "snn-core/math/common.hh"
#include "snn-core/mem/raw/copy.hh"
#include "snn-core/mem/raw/move.hh"
#include "snn-core/string/size.hh"
#include "snn-core/range/contiguous.hh"
namespace snn
{
// ## Classes
// ### strcore
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
template <typename Buffer>
class strcore final : public contiguous_interface<strcore<Buffer>>
{
public:
// #### Types
using value_type = char;
using iterator = char*;
using const_iterator = const char*;
using pointer = char*;
using const_pointer = const char*;
using reference = char&;
using const_reference = const char&;
using buffer_type = Buffer;
using trivially_relocatable_type = trivially_relocatable_if_t<strcore, Buffer>;
// #### Default constructor
constexpr strcore() noexcept = default;
// #### Explicit constructors
template <character Char>
constexpr explicit strcore(range::contiguous<Char*> rng)
: buf_{rng.data(), rng.count()}
{
}
constexpr explicit strcore(container::reserve_t, const usize capacity)
: buf_{capacity}
{
}
template <character Char>
constexpr explicit strcore(container::fill_t, const usize count, const Char c)
: buf_{count, c}
{
}
template <character CharA, character CharB>
explicit strcore(container::fill_t, CharA, CharB) = delete; // Error-prone
constexpr explicit strcore(const not_null<const_pointer> data, const usize size)
: buf_{data, size}
{
}
constexpr explicit strcore(const const_pointer s, promise::null_terminated_t)
: buf_{s, s + string::size(s, promise::null_terminated)}
{
}
constexpr explicit strcore(const not_null<const_pointer> s, promise::null_terminated_t)
: buf_{s, string::size(s, promise::null_terminated)}
{
}
constexpr explicit strcore(meta::iterators_t, const const_pointer first,
const const_pointer last)
: buf_{first, last}
{
}
// #### Converting constructors
template <usize N>
constexpr strcore(const char (&s)[N])
: buf_{array_view{s}}
{
}
constexpr strcore(const init_list<char> l)
: buf_{l.begin(), l.end()}
{
}
strcore(init_list<int>) = delete; // Error-prone
template <character Char, usize Count>
constexpr strcore(const array<Char, Count>& a)
: buf_{a.template view<>()}
{
}
template <character Char, usize Count>
constexpr strcore(const array_view<Char, Count> s)
: buf_{s}
{
}
template <typename Buf>
constexpr strcore(const strcore<Buf>& s)
: buf_{s.view()}
{
}
// #### Copy constructor/copy assignment operator
constexpr strcore(const strcore& other)
: buf_{other.view()}
{
}
constexpr strcore& operator=(const strcore& other)
{
if (this != &other)
{
const cstrview s = other.view();
mem::raw::copy(s.data(), buf_.resize_uninitialized(s.size()).writable(),
s.byte_size(), promise::no_overlap);
}
return *this;
}
// #### Move constructor/move assignment operator
constexpr strcore(strcore&& other) noexcept
: buf_{std::move(other.buf_)}
{
}
constexpr strcore& operator=(strcore&& other) noexcept
{
swap(other);
return *this;
}
// #### Destructor
~strcore() = default; // "Rule of five".
// #### Assignment operator
constexpr strcore& operator=(const same_as<char> auto c)
{
buf_.resize_uninitialized(1).begin()[0] = c;
return *this;
}
template <usize N>
constexpr strcore& operator=(const char (&s)[N])
{
constexpr usize Size = N - 1;
mem::raw::copy<Size>(not_null{s}, buf_.resize_uninitialized(Size).writable(),
promise::no_overlap);
return *this;
}
constexpr strcore& operator=(const init_list<char> l)
{
if (l.size() == 0)
{
buf_.clear();
}
else
{
mem::raw::copy(not_null{l.begin()}, buf_.resize_uninitialized(l.size()).writable(),
snn::byte_size{l.size()}, promise::no_overlap);
}
return *this;
}
void operator=(init_list<int>) = delete; // Error-prone
template <character Char, usize Count>
constexpr strcore& operator=(const array_view<Char, Count> s)
{
if constexpr (Count == 0)
{
buf_.clear();
}
else
{
buf_.assign(not_null{s.data()}, s.size());
}
return *this;
}
template <typename Buf>
constexpr strcore& operator=(const strcore<Buf>& s)
{
return operator=(s.view());
}
// #### Explicit conversion operators
constexpr explicit operator bool() const noexcept
{
return buf_.size() > 0;
}
// #### Iterators
[[nodiscard]] constexpr iterator begin() noexcept
{
return buf_.begin();
}
[[nodiscard]] constexpr iterator end() noexcept
{
return buf_.end();
}
[[nodiscard]] constexpr const_iterator begin() const noexcept
{
return buf_.cbegin();
}
[[nodiscard]] constexpr const_iterator end() const noexcept
{
return buf_.cend();
}
[[nodiscard]] constexpr const_iterator cbegin() const noexcept
{
return buf_.cbegin();
}
[[nodiscard]] constexpr const_iterator cend() const noexcept
{
return buf_.cend();
}
// #### Single element access
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> at(const usize pos) noexcept
{
return view().template at<R>(pos);
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> at(const usize pos) const noexcept
{
return view().template at<R>(pos);
}
[[nodiscard]] constexpr reference at(const usize pos, promise::within_bounds_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(pos < count());
return begin()[pos];
}
[[nodiscard]] constexpr const_reference at(const usize pos,
promise::within_bounds_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(pos < count());
return cbegin()[pos];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> back() noexcept
{
return view().template back<R>();
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> back() const noexcept
{
return view().template back<R>();
}
[[nodiscard]] constexpr reference back(promise::not_empty_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(count() > 0);
return begin()[count() - 1];
}
[[nodiscard]] constexpr const_reference back(promise::not_empty_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(count() > 0);
return cbegin()[count() - 1];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> front() noexcept
{
return view().template front<R>();
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> front() const noexcept
{
return view().template front<R>();
}
[[nodiscard]] constexpr reference front(promise::not_empty_t) noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(count() > 0);
return begin()[0];
}
[[nodiscard]] constexpr const_reference front(promise::not_empty_t) const noexcept
{
// Don't use view(), it makes it harder for the optimizer to remove the assert.
snn_assert(count() > 0);
return cbegin()[0];
}
// #### Append
constexpr void append(const same_as<char> auto c)
{
buf_.append_uninitialized(1).begin()[0] = c;
}
template <usize N>
constexpr void append(const char (&s)[N])
{
constexpr usize Size = N - 1;
mem::raw::copy<Size>(not_null{s}, buf_.append_uninitialized(Size).writable(),
promise::no_overlap);
}
constexpr void append(const init_list<char> l)
{
if (l.size() > 0)
{
mem::raw::copy(not_null{l.begin()}, buf_.append_uninitialized(l.size()).writable(),
snn::byte_size{l.size()}, promise::no_overlap);
}
}
void append(init_list<int>) = delete; // Error-prone
template <character Char, usize Count>
constexpr void append(const array<Char, Count>& a)
{
mem::raw::copy<Count>(a.data(), buf_.append_uninitialized(Count).writable(),
promise::no_overlap);
}
template <character Char, usize Count>
constexpr void append(const array_view<Char, Count> s)
{
if constexpr (Count > 0)
{
buf_.append(not_null{s.data()}, s.size());
}
}
template <typename Buf>
constexpr void append(const strcore<Buf>& s)
{
append(s.view());
}
constexpr void append(const not_null<const_pointer> data, const usize size)
{
buf_.append(data, size);
}
// #### Append integral
template <math::base Base = math::base::decimal, strict_unsigned_integral UInt>
constexpr void append_integral(const UInt num, const usize min_digits = 0)
{
const usize size = math::max(math::count_digits<Base>(num).get(), min_digits);
strview dest = buf_.append_uninitialized(size);
append_unsigned_<Base>(dest.end(), dest.begin(), num);
}
template <math::base Base = math::base::decimal, strict_signed_integral SInt>
constexpr void append_integral(const SInt num, const usize min_digits = 0)
{
auto n = force_unsigned(num);
usize size = 0;
if (num < 0)
{
n = math::negate_with_overflow(n);
size += sizeof('-');
}
size += math::max(math::count_digits<Base>(n).get(), min_digits);
strview dest = buf_.append_uninitialized(size);
char* first = dest.begin();
char* last = dest.end();
if (num < 0)
{
*first = '-';
++first;
}
append_unsigned_<Base>(last, first, n);
}
// #### Append uninitialized
template <usize SizeIncrease>
requires(SizeIncrease != constant::dynamic_count)
[[nodiscard]] constexpr auto append_uninitialized()
{
return array_view<char, SizeIncrease>{buf_.append_uninitialized(SizeIncrease).begin(),
promise::has_capacity};
}
[[nodiscard]] constexpr strview append_uninitialized(const usize size_increase)
{
return buf_.append_uninitialized(size_increase);
}
// #### Count/Size
[[nodiscard]] constexpr snn::byte_size byte_size() const noexcept
{
return snn::byte_size{buf_.size()};
}
[[nodiscard]] constexpr usize count() const noexcept
{
return buf_.size();
}
[[nodiscard]] constexpr bool is_empty() const noexcept
{
return buf_.size() == 0;
}
[[nodiscard]] constexpr usize size() const noexcept
{
return buf_.size();
}
// #### Capacity
[[nodiscard]] constexpr usize capacity() const noexcept
{
return buf_.capacity();
}
[[nodiscard]] static constexpr usize default_capacity() noexcept
{
return Buffer::default_capacity();
}
constexpr void reserve(const usize capacity)
{
buf_.reserve(capacity);
}
constexpr void reserve_append(const usize size_increase)
{
buf_.reserve_append(size_increase);
}
constexpr void shrink_to_fit() noexcept
{
buf_.shrink_to_fit();
}
// #### Null terminated
[[nodiscard]] static constexpr bool is_null_terminated() noexcept
{
return detail::strcore::is_null_terminated_v<Buffer>;
}
// Will throw if the buffer contains any null bytes (excluding the trailing null byte).
[[nodiscard]] constexpr null_term<const char*> null_terminated() const
requires(detail::strcore::is_null_terminated_v<Buffer>)
{
const auto s = view();
if (string::size(s.data(), promise::null_terminated) == s.size())
{
return null_term<const char*>{s.data(), promise::null_terminated};
}
throw_or_abort(generic::error::unexpected_null_character);
}
// Non-throwing with promise.
[[nodiscard]] constexpr null_term<const char*> null_terminated(
promise::is_valid_t) const noexcept
requires(detail::strcore::is_null_terminated_v<Buffer>)
{
const auto s = view();
snn_should(string::size(s.data(), promise::null_terminated) == s.size());
return null_term<const char*>{s.data(), promise::null_terminated};
}
// #### Raw data access
[[nodiscard]] constexpr not_null<const_pointer> data() const noexcept
{
return not_null<const_pointer>{buf_.cbegin()};
}
// constexpr not_null<pointer> data() noexcept
// {
// // Not implemented.
// // Use writable(), which is more readable and easier to audit.
// }
[[nodiscard]] constexpr not_null<pointer> writable() noexcept
{
return not_null<pointer>{buf_.begin()};
}
// #### Range
[[nodiscard]] constexpr strrng range() noexcept
{
return buf_.view().range();
}
[[nodiscard]] constexpr cstrrng range() const noexcept
{
return buf_.view().range();
}
// #### View
[[nodiscard]] constexpr strview view() noexcept
{
return buf_.view();
}
[[nodiscard]] constexpr cstrview view() const noexcept
{
return buf_.view();
}
[[nodiscard]] constexpr strview view(const usize pos,
const usize size = constant::npos) noexcept
{
return buf_.view().view(pos, size);
}
[[nodiscard]] constexpr cstrview view(const usize pos,
const usize size = constant::npos) const noexcept
{
return buf_.view().view(pos, size);
}
// #### To integral
template <strict_integral Int, math::base Base = math::base::decimal>
[[nodiscard]] constexpr optional<Int> to() const noexcept
{
return buf_.view().template to<Int, Base>();
}
template <strict_integral Int, math::base Base = math::base::decimal, usize MaxDigits = 0>
[[nodiscard]] constexpr pair::value_count<Int, usize> to_prefix(
ascii::leading_zeros policy = ascii::leading_zeros::disallow) const noexcept
{
return buf_.view().template to_prefix<Int, Base, MaxDigits>(policy);
}
// #### Modifiers
constexpr void clear() noexcept
{
buf_.clear();
}
constexpr void insert_at(const usize pos, const transient<cstrview> s)
{
const cstrview sv = s.get();
if (!std::is_constant_evaluated() && !buf_.overlaps(sv)) [[likely]]
{
mem::raw::copy(sv.data(), buf_.replace_uninitialized(pos, 0, sv.size()).writable(),
sv.byte_size(), promise::no_overlap);
}
else
{
// This could be optimized, but it's currently not worth the complexity.
Buffer tmp{buf_.view()};
mem::raw::copy(sv.data(), tmp.replace_uninitialized(pos, 0, sv.size()).writable(),
sv.byte_size(), promise::no_overlap);
buf_.swap(tmp);
}
}
constexpr void insert_at(const usize pos, const transient<cstrview> s,
promise::no_overlap_t)
{
const cstrview sv = s.get();
snn_should(std::is_constant_evaluated() || !buf_.overlaps(sv));
mem::raw::copy(sv.data(), buf_.replace_uninitialized(pos, 0, sv.size()).writable(),
sv.byte_size(), promise::no_overlap);
}
template <same_as<const char> ConstChar, usize N>
constexpr void insert_at(const usize pos, ConstChar (&s)[N])
{
constexpr usize Size = N - 1;
mem::raw::copy<Size>(not_null{s}, buf_.replace_uninitialized(pos, 0, Size).writable(),
promise::no_overlap);
}
[[nodiscard]] constexpr strview insert_uninitialized(const usize pos, const usize size)
{
return buf_.replace_uninitialized(pos, 0, size);
}
constexpr void drop_at(const usize pos, const usize count) noexcept
{
buf_.drop_at(pos, count);
}
constexpr void drop_back_n(const usize size) noexcept
{
buf_.drop_back_n(size);
}
constexpr usize remove(const same_as<char> auto needle) noexcept
{
using base_type = contiguous_interface<strcore<Buffer>>;
return base_type::remove_if(fn::is{fn::equal_to{}, char{needle}});
}
constexpr usize remove(const transient<cstrview> needle) noexcept
{
return replace(needle, "");
}
// Replace all occurrences of `needle` with `replacement`.
constexpr usize replace(const same_as<char> auto needle,
const same_as<char> auto replacement,
const usize start_pos = 0) noexcept
{
strview s = buf_.view();
char* const data = s.writable().get();
const usize size = s.size();
usize replace_count = 0;
for (usize i = start_pos; i < size; ++i)
{
if (data[i] == needle)
{
data[i] = replacement;
++replace_count;
}
}
return replace_count;
}
// Replace all occurrences of `needle` with `replacement`.
constexpr usize replace(const transient<cstrview> needle,
const transient<cstrview> replacement, const usize start_pos = 0)
{
return replace_(needle.get(), replacement.get(), start_pos);
}
constexpr void replace_at(const usize pos, const usize size, const transient<cstrview> s)
{
const cstrview replacement = s.get();
if (!std::is_constant_evaluated() && !buf_.overlaps(replacement)) [[likely]]
{
mem::raw::copy(replacement.data(),
buf_.replace_uninitialized(pos, size, replacement.size()).writable(),
replacement.byte_size(), promise::no_overlap);
}
else
{
// This could be optimized, but it's currently not worth the complexity.
Buffer tmp{buf_.view()};
mem::raw::copy(replacement.data(),
tmp.replace_uninitialized(pos, size, replacement.size()).writable(),
replacement.byte_size(), promise::no_overlap);
buf_.swap(tmp);
}
}
constexpr void replace_at(const usize pos, const usize size, const transient<cstrview> s,
promise::no_overlap_t)
{
const cstrview replacement = s.get();
snn_should(std::is_constant_evaluated() || !buf_.overlaps(replacement));
mem::raw::copy(replacement.data(),
buf_.replace_uninitialized(pos, size, replacement.size()).writable(),
replacement.byte_size(), promise::no_overlap);
}
template <same_as<const char> ConstChar, usize N>
constexpr void replace_at(const usize pos, const usize size, ConstChar (&replacement)[N])
{
constexpr usize Size = N - 1;
mem::raw::copy<Size>(not_null{replacement},
buf_.replace_uninitialized(pos, size, Size).writable(),
promise::no_overlap);
}
[[nodiscard]] constexpr strview replace_uninitialized(const usize pos, const usize size,
const usize replacement_size)
{
return buf_.replace_uninitialized(pos, size, replacement_size);
}
constexpr void reset() noexcept
{
buf_.reset();
}
constexpr void resize(const usize size, const same_as<char> auto fill)
{
buf_.resize(size, fill);
}
[[nodiscard]] constexpr strview resize_uninitialized(const usize size)
{
return buf_.resize_uninitialized(size);
}
constexpr void truncate(const usize size) noexcept
{
buf_.truncate(size);
}
// #### Search
template <typename V>
[[nodiscard]] constexpr usize count(V&& value) const noexcept
{
return view().count(std::forward<V>(value));
}
// #### Swap
constexpr void swap(strcore& other) noexcept
{
// This works even if this == &other.
buf_.swap(other.buf_);
}
// #### Stream append (insertion operator)
constexpr strcore& operator<<(const same_as<char> auto c)
{
append(c);
return *this;
}
template <usize N>
constexpr strcore& operator<<(const char (&s)[N])
{
append(s);
return *this;
}
template <character Char, usize Count>
constexpr strcore& operator<<(const array<Char, Count>& a)
{
append(a);
return *this;
}
template <character Char, usize Count>
constexpr strcore& operator<<(const array_view<Char, Count> s)
{
append(s);
return *this;
}
template <typename Buf>
constexpr strcore& operator<<(const strcore<Buf>& s)
{
append(s);
return *this;
}
template <strict_integral Int>
constexpr strcore& operator<<(const numeric<Int> n)
{
append_integral(n.get());
return *this;
}
// #### Hash
[[nodiscard]] constexpr usize hash() const noexcept
{
return view().hash();
}
// #### Comparison
template <usize N>
constexpr bool operator==(const char (&s)[N]) const noexcept
{
return view() == s;
}
template <character Char, usize Count>
constexpr bool operator==(const array_view<Char, Count> s) const noexcept
{
return view() == s;
}
template <typename Buf>
constexpr bool operator==(const strcore<Buf>& s) const noexcept
{
return view() == s.view();
}
template <usize N>
constexpr std::strong_ordering operator<=>(const char (&s)[N]) const noexcept
{
return view() <=> cstrview{s};
}
template <character Char, usize Count>
constexpr std::strong_ordering operator<=>(const array_view<Char, Count> s) const noexcept
{
return view() <=> s;
}
template <typename Buf>
constexpr std::strong_ordering operator<=>(const strcore<Buf>& s) const noexcept
{
return view() <=> s.view();
}
// #### Overlaps
[[nodiscard]] constexpr bool overlaps(const cstrview s) const noexcept
{
return buf_.overlaps(s);
}
// #### Contiguous interface
// This class inherits from `contiguous_interface`, see
// [contiguous\_interface.hh](contiguous_interface.hh) for additional functionality.
private:
Buffer buf_;
template <math::base Base, typename UInt>
constexpr void append_unsigned_(iterator cur, const const_iterator first, UInt num) noexcept
{
snn_should(cur > first);
if constexpr (Base == math::base::decimal)
{
// This code is based on the talk:
// "Three Optimization Tips for C++" by Andrei Alexandrescu.
while (num >= 100)
{
const usize digit_index = (num % 100) * 2;
num /= 100;
*(--cur) = detail::strcore::digit_lookup[digit_index + 1];
*(--cur) = detail::strcore::digit_lookup[digit_index];
}
if (num < 10)
{
*(--cur) = static_cast<char>(num + '0');
}
else // Less than 100.
{
const usize digit_index = static_cast<usize>(num) * 2;
*(--cur) = detail::strcore::digit_lookup[digit_index + 1];
*(--cur) = detail::strcore::digit_lookup[digit_index];
}
}
else if constexpr (Base == math::base::hex)
{
do
{
*(--cur) = hex::table::lower.at(num, bounds::mask);
num >>= 4;
} while (num);
}
else if constexpr (Base == math::base::binary)
{
do
{
*(--cur) = static_cast<char>((num & 0b1) + '0');
num >>= 1;
} while (num);
}
else if constexpr (Base == math::base::octal)
{
do
{
*(--cur) = static_cast<char>((num & 0b111) + '0');
num >>= 3;
} while (num);
}
else
{
static_assert(meta::always_false<decltype(Base)>, "Unsupported base.");
}
// Pad left?
while (cur > first)
{
*(--cur) = '0';
}
snn_should(cur == first);
}
constexpr usize replace_(const cstrview needle, const cstrview replacement,
const usize start_pos)
{
if (needle.is_empty())
{
return 0;
}
strview subject = view();
usize pos = subject.find(needle, start_pos).value_or_npos();
if (pos == constant::npos)
{
return 0;
}
usize replace_count = 0;
if (std::is_constant_evaluated() || replacement.size() > needle.size() ||
buf_.overlaps(needle) || buf_.overlaps(replacement))
{
const usize diff =
replacement.size() - math::min(needle.size(), replacement.size());
// Reserve for one replacement and an overflow here can't do any harm.
strcore tmp{container::reserve, subject.size() + diff};
usize last_pos = 0;
do
{
if (pos > last_pos)
{
const usize size = pos - last_pos;
tmp.append(cstrview{not_null{subject.data().get() + last_pos}, size});
}
tmp.append(replacement);
pos += needle.size();
last_pos = pos;
pos = subject.find(needle, pos).value_or_npos();
++replace_count;
} while (pos != constant::npos);
if (subject.size() > last_pos)
{