summaryrefslogtreecommitdiffstats
path: root/src/third-party/scnlib/include/scn/ranges/custom_impl.h
blob: 86d73d528f7f682a3a5e9caa1f04ff9460d26bac (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
// Copyright 2017 Elias Kosunen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is a part of scnlib:
//     https://github.com/eliaskosunen/scnlib
//
// The contents of this file are adapted from NanoRange.
//     https://github.com/tcbrindle/NanoRange
//     Copyright (c) 2018 Tristan Brindle
//     Distributed under the Boost Software License, Version 1.0

#ifndef SCN_RANGES_CUSTOM_IMPL_H
#define SCN_RANGES_CUSTOM_IMPL_H

#include "util.h"

#include "../util/string_view.h"

#include <iterator>
#include <utility>

namespace scn {
    SCN_BEGIN_NAMESPACE

    namespace custom_ranges {
        // iterator_category is span.h

        template <typename T>
        struct iterator_category;

        namespace detail {
            template <typename T, typename = void>
            struct iterator_category {
            };
            template <typename T>
            struct iterator_category<T*>
                : std::enable_if<std::is_object<T>::value,
                                 contiguous_iterator_tag> {
            };
            template <typename T>
            struct iterator_category<const T> : iterator_category<T> {
            };
            template <typename T>
            struct iterator_category<
                T,
                detail::void_t<typename T::iterator_category>> {
                using type = typename T::iterator_category;
            };
        }  // namespace detail

        template <typename T>
        struct iterator_category : detail::iterator_category<T> {
        };
        template <typename T>
        using iterator_category_t = typename iterator_category<T>::type;

        template <typename T>
        using iter_reference_t = decltype(*SCN_DECLVAL(T&));

        // iter_difference_t
        template <typename>
        struct incrementable_traits;

        namespace detail {
            struct empty {
            };

            template <typename T>
            struct with_difference_type {
                using difference_type = T;
            };
            template <typename, typename = void>
            struct incrementable_traits_helper {
            };

            template <>
            struct incrementable_traits_helper<void*> {
            };
            template <typename T>
            struct incrementable_traits_helper<T*>
                : std::conditional<std::is_object<T>::value,
                                   with_difference_type<std::ptrdiff_t>,
                                   empty>::type {
            };
            template <typename I>
            struct incrementable_traits_helper<const I>
                : incrementable_traits<typename std::decay<I>::type> {
            };

            template <typename, typename = void>
            struct has_member_difference_type : std::false_type {
            };
            template <typename T>
            struct has_member_difference_type<
                T,
                detail::void_t<typename T::difference_type>> : std::true_type {
            };

            template <typename T>
            struct incrementable_traits_helper<
                T,
                typename std::enable_if<
                    has_member_difference_type<T>::value>::type> {
                using difference_type = typename T::difference_type;
            };
            template <typename T>
            struct incrementable_traits_helper<
                T,
                typename std::enable_if<
                    !std::is_pointer<T>::value &&
                    !has_member_difference_type<T>::value &&
                    std::is_integral<decltype(SCN_DECLVAL(const T&) -
                                              SCN_DECLVAL(const T&))>::value>::
                    type>
                : with_difference_type<typename std::make_signed<
                      decltype(SCN_DECLVAL(T) - SCN_DECLVAL(T))>::type> {
            };
        }  // namespace detail

        template <typename T>
        struct incrementable_traits : detail::incrementable_traits_helper<T> {
        };

        template <typename T>
        using iter_difference_t =
            typename incrementable_traits<T>::difference_type;

        // iter_value_t
        template <typename>
        struct readable_traits;

        namespace detail {
            template <typename T>
            struct with_value_type {
                using value_type = T;
            };
            template <typename, typename = void>
            struct readable_traits_helper {
            };

            template <typename T>
            struct readable_traits_helper<T*>
                : std::conditional<
                      std::is_object<T>::value,
                      with_value_type<typename std::remove_cv<T>::type>,
                      empty>::type {
            };

            template <typename I>
            struct readable_traits_helper<
                I,
                typename std::enable_if<std::is_array<I>::value>::type>
                : readable_traits<typename std::decay<I>::type> {
            };

            template <typename I>
            struct readable_traits_helper<
                const I,
                typename std::enable_if<!std::is_array<I>::value>::type>
                : readable_traits<typename std::decay<I>::type> {
            };

            template <typename T, typename V = typename T::value_type>
            struct member_value_type
                : std::conditional<std::is_object<V>::value,
                                   with_value_type<V>,
                                   empty>::type {
            };

            template <typename T, typename E = typename T::element_type>
            struct _member_element_type
                : std::conditional<
                      std::is_object<E>::value,
                      with_value_type<typename std::remove_cv<E>::type>,
                      empty>::type {
            };

            template <typename T>
            using member_value_type_t = typename T::value_type;

            template <typename T>
            struct has_member_value_type : exists<member_value_type_t, T> {
            };

            template <typename T>
            using member_element_type_t = typename T::element_type;

            template <typename T>
            struct has_member_element_type : exists<member_element_type_t, T> {
            };

            template <typename T>
            struct readable_traits_helper<
                T,
                typename std::enable_if<
                    has_member_value_type<T>::value &&
                    !has_member_element_type<T>::value>::type>
                : member_value_type<T> {
            };

            template <typename T>
            struct readable_traits_helper<
                T,
                typename std::enable_if<has_member_element_type<T>::value &&
                                        !has_member_value_type<T>::value>::type>
                : _member_element_type<T> {
            };

            template <typename T>
            struct readable_traits_helper<
                T,
                typename std::enable_if<
                    has_member_element_type<T>::value &&
                    has_member_value_type<T>::value>::type> {
            };
        }  // namespace detail

        template <typename T>
        struct readable_traits : detail::readable_traits_helper<T> {
        };

        template <typename T>
        using iter_value_t = typename readable_traits<T>::value_type;

        // sentinel_for
        namespace detail {
            struct sentinel_for_concept {
                template <typename S, typename I>
                auto _test_requires(S s, I i)
                    -> decltype(scn::detail::valid_expr(*i, i == s, i != s));
            };
        }  // namespace detail
        template <typename S, typename I>
        struct sentinel_for
            : std::integral_constant<
                  bool,
                  std::is_default_constructible<S>::value &&
                      std::is_copy_constructible<S>::value &&
                      detail::_requires<detail::sentinel_for_concept, S, I>::
                          value> {
        };

        // sized_sentinel_for
        namespace detail {
            struct sized_sentinel_for_concept {
                template <typename S, typename I>
                auto _test_requires(const S& s, const I& i) -> decltype(
                    detail::requires_expr<
                        std::is_same<decltype(s - i),
                                     iter_difference_t<I>>::value>{},
                    detail::requires_expr<
                        std::is_same<decltype(i - s),
                                     iter_difference_t<I>>::value>{});
            };
        }  // namespace detail
        template <typename S, typename I>
        struct sized_sentinel_for
            : std::integral_constant<
                  bool,
                  detail::_requires<detail::sized_sentinel_for_concept, S, I>::
                          value &&
                      sentinel_for<S, I>::value> {
        };
        template <typename S>
        struct sized_sentinel_for<S, void*> : std::false_type {
        };
        template <typename I>
        struct sized_sentinel_for<void*, I> : std::false_type {
        };
        template <>
        struct sized_sentinel_for<void*, void*> : std::false_type {
        };

        // begin
        namespace _begin {
            template <typename T>
            void begin(T&&) = delete;
            template <typename T>
            void begin(std::initializer_list<T>&&) = delete;

            struct fn {
            private:
                template <typename T, std::size_t N>
                static SCN_CONSTEXPR14 void impl(T(&&)[N],
                                                 detail::priority_tag<3>) =
                    delete;

                template <typename T, std::size_t N>
                static SCN_CONSTEXPR14 auto impl(
                    T (&t)[N],
                    detail::priority_tag<3>) noexcept -> decltype((t) + 0)
                {
                    return (t) + 0;
                }

                template <typename C>
                static SCN_CONSTEXPR14 auto impl(
                    basic_string_view<C> sv,
                    detail::priority_tag<2>) noexcept -> decltype(sv.begin())
                {
                    return sv.begin();
                }

                template <typename T>
                static SCN_CONSTEXPR14 auto
                impl(T& t, detail::priority_tag<1>) noexcept(noexcept(
                    ::scn::custom_ranges::detail::decay_copy(t.begin())))
                    -> decltype(::scn::custom_ranges::detail::decay_copy(
                        t.begin()))
                {
                    return ::scn::custom_ranges::detail::decay_copy(t.begin());
                }

                template <typename T>
                static SCN_CONSTEXPR14 auto
                impl(T&& t, detail::priority_tag<0>) noexcept(
                    noexcept(::scn::custom_ranges::detail::decay_copy(
                        begin(SCN_FWD(t)))))
                    -> decltype(::scn::custom_ranges::detail::decay_copy(
                        begin(SCN_FWD(t))))
                {
                    return ::scn::custom_ranges::detail::decay_copy(
                        begin(SCN_FWD(t)));
                }

            public:
                template <typename T>
                SCN_CONSTEXPR14 auto operator()(T&& t) const noexcept(
                    noexcept(fn::impl(SCN_FWD(t), detail::priority_tag<3>{})))
                    -> decltype(fn::impl(SCN_FWD(t), detail::priority_tag<3>{}))
                {
                    return fn::impl(SCN_FWD(t), detail::priority_tag<3>{});
                }
            };
        }  // namespace _begin
        namespace {
            constexpr auto& begin = detail::static_const<_begin::fn>::value;
        }

        // end
        namespace _end {
            template <typename T>
            void end(T&&) = delete;
            template <typename T>
            void end(std::initializer_list<T>&&) = delete;

            struct fn {
            private:
                template <typename T, std::size_t N>
                static constexpr void impl(T(&&)[N],
                                           detail::priority_tag<2>) = delete;

                template <typename T, std::size_t N>
                static constexpr auto impl(T (&t)[N],
                                           detail::priority_tag<2>) noexcept
                    -> decltype((t) + N)
                {
                    return (t) + N;
                }

                template <typename C>
                static constexpr auto impl(basic_string_view<C> sv,
                                           detail::priority_tag<2>) noexcept
                    -> decltype(sv.end())
                {
                    return sv.end();
                }

                SCN_GCC_PUSH
                SCN_GCC_IGNORE("-Wnoexcept")
                template <typename T,
                          typename S =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  SCN_DECLVAL(T&).end())),
                          typename I = decltype(::scn::custom_ranges::begin(
                              SCN_DECLVAL(T&)))>
                static constexpr auto
                impl(T& t, detail::priority_tag<1>) noexcept(
                    noexcept(::scn::custom_ranges::detail::decay_copy(t.end())))
                    -> decltype(::scn::custom_ranges::detail::decay_copy(
                        t.end()))
                {
                    return ::scn::custom_ranges::detail::decay_copy(t.end());
                }

                template <typename T,
                          typename S =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  end(SCN_DECLVAL(T)))),
                          typename I = decltype(::scn::custom_ranges::begin(
                              SCN_DECLVAL(T)))>
                static constexpr auto
                impl(T& t, detail::priority_tag<0>) noexcept(noexcept(
                    ::scn::custom_ranges::detail::decay_copy(end(SCN_FWD(t)))))
                    -> S
                {
                    return ::scn::custom_ranges::detail::decay_copy(
                        end(SCN_FWD(t)));
                }

            public:
                template <typename T>
                constexpr auto operator()(T&& t) const noexcept(
                    noexcept(fn::impl(SCN_FWD(t), detail::priority_tag<2>{})))
                    -> decltype(fn::impl(SCN_FWD(t), detail::priority_tag<2>{}))
                {
                    return fn::impl(SCN_FWD(t), detail::priority_tag<2>{});
                }
                SCN_GCC_POP
            };
        }  // namespace _end
        namespace {
            constexpr auto& end = detail::static_const<_end::fn>::value;
        }

        // cbegin
        namespace _cbegin {
            struct fn {
                template <typename T>
                constexpr auto operator()(const T& t) const
                    noexcept(noexcept(::scn::custom_ranges::begin(t)))
                        -> decltype(::scn::custom_ranges::begin(t))
                {
                    return ::scn::custom_ranges::begin(t);
                }

                template <typename T>
                constexpr auto operator()(const T&& t) const noexcept(noexcept(
                    ::scn::custom_ranges::begin(static_cast<const T&&>(t))))
                    -> decltype(::scn::custom_ranges::begin(
                        static_cast<const T&&>(t)))
                {
                    return ::scn::custom_ranges::begin(
                        static_cast<const T&&>(t));
                }
            };
        }  // namespace _cbegin
        namespace {
            constexpr auto& cbegin = detail::static_const<_cbegin::fn>::value;
        }

        // cend
        namespace _cend {
            struct fn {
                template <typename T>
                constexpr auto operator()(const T& t) const
                    noexcept(noexcept(::scn::custom_ranges::end(t)))
                        -> decltype(::scn::custom_ranges::end(t))
                {
                    return ::scn::custom_ranges::end(t);
                }

                template <typename T>
                constexpr auto operator()(const T&& t) const noexcept(noexcept(
                    ::scn::custom_ranges::end(static_cast<const T&&>(t))))
                    -> decltype(::scn::custom_ranges::end(
                        static_cast<const T&&>(t)))
                {
                    return ::scn::custom_ranges::end(static_cast<const T&&>(t));
                }
            };
        }  // namespace _cend
        namespace {
            constexpr auto& cend = detail::static_const<_cend::fn>::value;
        }

        // range
        namespace detail {
            struct range_impl_concept {
                template <typename T>
                auto _test_requires(T&& t)
                    -> decltype(::scn::custom_ranges::begin(SCN_FWD(t)),
                                ::scn::custom_ranges::end(SCN_FWD(t)));
            };
            template <typename T>
            struct range_impl : _requires<range_impl_concept, T> {
            };
            struct range_concept {
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename T>
                static auto test(int) ->
                    typename std::enable_if<range_impl<T&>::value,
                                            std::true_type>::type;
            };
        }  // namespace detail
        template <typename T>
        struct range : decltype(detail::range_concept::test<T>(0)) {
        };

        template <typename T>
        struct forwarding_range
            : std::integral_constant<bool,
                                     range<T>::value &&
                                         detail::range_impl<T>::value> {
        };

        // typedefs
        template <typename R>
        using iterator_t =
            typename std::enable_if<range<R>::value,
                                    decltype(::scn::custom_ranges::begin(
                                        SCN_DECLVAL(R&)))>::type;
        template <typename R>
        using sentinel_t =
            typename std::enable_if<range<R>::value,
                                    decltype(::scn::custom_ranges::end(
                                        SCN_DECLVAL(R&)))>::type;
        template <typename R>
        using range_difference_t =
            typename std::enable_if<range<R>::value,
                                    iter_difference_t<iterator_t<R>>>::type;
        template <typename R>
        using range_value_t =
            typename std::enable_if<range<R>::value,
                                    iter_value_t<iterator_t<R>>>::type;
        template <typename R>
        using range_reference_t =
            typename std::enable_if<range<R>::value,
                                    iter_reference_t<iterator_t<R>>>::type;

        // view
        struct view_base {
        };

        namespace detail {
            template <typename>
            struct is_std_non_view : std::false_type {
            };
            template <typename T>
            struct is_std_non_view<std::initializer_list<T>> : std::true_type {
            };
            template <typename T>
            struct enable_view_helper
                : std::conditional<
                      std::is_base_of<view_base, T>::value,
                      std::true_type,
                      typename std::conditional<
                          is_std_non_view<T>::value,
                          std::false_type,
                          typename std::conditional<
                              range<T>::value && range<const T>::value,
                              std::is_same<range_reference_t<T>,
                                           range_reference_t<const T>>,
                              std::true_type>::type>::type>::type {
            };
            template <typename T>
            struct view_impl
                : std::integral_constant<
                      bool,
                      std::is_copy_constructible<T>::value &&
                          std::is_default_constructible<T>::value &&
                          detail::enable_view_helper<T>::value> {
            };
        }  // namespace detail
        template <typename T>
        struct view : std::conditional<range<T>::value,
                                       detail::view_impl<T>,
                                       std::false_type>::type {
        };

        // data
        template <typename P>
        struct _is_object_pointer
            : std::integral_constant<
                  bool,
                  std::is_pointer<P>::value &&
                      std::is_object<detail::test_t<iter_value_t, P>>::value> {
        };

        namespace _data {
            struct fn {
            private:
                template <typename CharT, typename Traits, typename Allocator>
                static constexpr auto impl(
                    std::basic_string<CharT, Traits, Allocator>& str,
                    detail::priority_tag<2>) noexcept -> typename std::
                    basic_string<CharT, Traits, Allocator>::pointer
                {
                    return std::addressof(*str.begin());
                }
                template <typename CharT, typename Traits, typename Allocator>
                static constexpr auto impl(
                    const std::basic_string<CharT, Traits, Allocator>& str,
                    detail::priority_tag<2>) noexcept -> typename std::
                    basic_string<CharT, Traits, Allocator>::const_pointer
                {
                    return std::addressof(*str.begin());
                }
                template <typename CharT, typename Traits, typename Allocator>
                static constexpr auto impl(
                    std::basic_string<CharT, Traits, Allocator>&& str,
                    detail::priority_tag<2>) noexcept -> typename std::
                    basic_string<CharT, Traits, Allocator>::pointer
                {
                    return std::addressof(*str.begin());
                }

                template <typename T,
                          typename D =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  SCN_DECLVAL(T&).data()))>
                static constexpr auto
                impl(T& t, detail::priority_tag<1>) noexcept(noexcept(
                    ::scn::custom_ranges::detail::decay_copy(t.data()))) ->
                    typename std::enable_if<_is_object_pointer<D>::value,
                                            D>::type
                {
                    return ::scn::custom_ranges::detail::decay_copy(t.data());
                }

                template <typename T>
                static constexpr auto
                impl(T&& t, detail::priority_tag<0>) noexcept(
                    noexcept(::scn::custom_ranges::begin(SCN_FWD(t)))) ->
                    typename std::enable_if<
                        _is_object_pointer<decltype(::scn::custom_ranges::begin(
                            SCN_FWD(t)))>::value,
                        decltype(::scn::custom_ranges::begin(SCN_FWD(t)))>::type
                {
                    return ::scn::custom_ranges::begin(SCN_FWD(t));
                }

            public:
                template <typename T>
                constexpr auto operator()(T&& t) const noexcept(
                    noexcept(fn::impl(SCN_FWD(t), detail::priority_tag<2>{})))
                    -> decltype(fn::impl(SCN_FWD(t), detail::priority_tag<2>{}))
                {
                    return fn::impl(SCN_FWD(t), detail::priority_tag<2>{});
                }
            };
        }  // namespace _data
        namespace {
            constexpr auto& data = detail::static_const<_data::fn>::value;
        }

        // size
        template <typename>
        struct disable_sized_range : std::false_type {
        };

        namespace _size {
            template <typename T>
            void size(T&&) = delete;
            template <typename T>
            void size(T&) = delete;

            struct fn {
            private:
                template <typename T, std::size_t N>
                static constexpr std::size_t impl(
                    const T(&&)[N],
                    detail::priority_tag<3>) noexcept
                {
                    return N;
                }

                template <typename T, std::size_t N>
                static constexpr std::size_t impl(
                    const T (&)[N],
                    detail::priority_tag<3>) noexcept
                {
                    return N;
                }

                template <typename T,
                          typename I =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  SCN_DECLVAL(T).size()))>
                static constexpr auto
                impl(T&& t, detail::priority_tag<2>) noexcept(
                    noexcept(::scn::custom_ranges::detail::decay_copy(
                        SCN_FWD(t).size()))) ->
                    typename std::enable_if<
                        std::is_integral<I>::value &&
                            !disable_sized_range<
                                detail::remove_cvref_t<T>>::value,
                        I>::type
                {
                    return ::scn::custom_ranges::detail::decay_copy(
                        SCN_FWD(t).size());
                }

                template <typename T,
                          typename I =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  size(SCN_DECLVAL(T))))>
                static constexpr auto
                impl(T&& t, detail::priority_tag<1>) noexcept(noexcept(
                    ::scn::custom_ranges::detail::decay_copy(size(SCN_FWD(t)))))
                    -> typename std::enable_if<
                        std::is_integral<I>::value &&
                            !disable_sized_range<
                                detail::remove_cvref_t<T>>::value,
                        I>::type
                {
                    return ::scn::custom_ranges::detail::decay_copy(
                        size(SCN_FWD(t)));
                }

                template <typename T,
                          typename I = decltype(::scn::custom_ranges::begin(
                              SCN_DECLVAL(T))),
                          typename S = decltype(::scn::custom_ranges::end(
                              SCN_DECLVAL(T))),
                          typename D =
                              decltype(::scn::custom_ranges::detail::decay_copy(
                                  SCN_DECLVAL(S) - SCN_DECLVAL(I)))>
                static constexpr auto
                impl(T&& t, detail::priority_tag<0>) noexcept(
                    noexcept(::scn::custom_ranges::detail::decay_copy(
                        ::scn::custom_ranges::end(t) -
                        ::scn::custom_ranges::begin(t)))) ->
                    typename std::enable_if<
                        !std::is_array<detail::remove_cvref_t<T>>::value,
                        D>::type
                {
                    return ::scn::custom_ranges::detail::decay_copy(
                        ::scn::custom_ranges::end(t) -
                        ::scn::custom_ranges::begin(t));
                }

            public:
                template <typename T>
                constexpr auto operator()(T&& t) const noexcept(
                    noexcept(fn::impl(SCN_FWD(t), detail::priority_tag<3>{})))
                    -> decltype(fn::impl(SCN_FWD(t), detail::priority_tag<3>{}))
                {
                    return fn::impl(SCN_FWD(t), detail::priority_tag<3>{});
                }
            };
        }  // namespace _size
        namespace {
            constexpr auto& size = detail::static_const<_size::fn>::value;
        }

        // empty
        namespace _empty_ns {
            struct fn {
            private:
                template <typename T>
                static constexpr auto
                impl(T&& t, detail::priority_tag<2>) noexcept(
                    noexcept((bool(SCN_FWD(t).empty()))))
                    -> decltype((bool(SCN_FWD(t).empty())))
                {
                    return bool((SCN_FWD(t).empty()));
                }
                template <typename T>
                static constexpr auto
                impl(T&& t, detail::priority_tag<1>) noexcept(
                    noexcept(::scn::custom_ranges::size(SCN_FWD(t)) == 0))
                    -> decltype(::scn::custom_ranges::size(SCN_FWD(t)) == 0)
                {
                    return ::scn::custom_ranges::size(SCN_FWD(t)) == 0;
                }

                template <typename T,
                          typename I = decltype(::scn::custom_ranges::begin(
                              SCN_DECLVAL(T)))>
                static constexpr auto
                impl(T&& t, detail::priority_tag<0>) noexcept(
                    noexcept(::scn::custom_ranges::begin(t) ==
                             ::scn::custom_ranges::end(t)))
                    -> decltype(::scn::custom_ranges::begin(t) ==
                                ::scn::custom_ranges::end(t))
                {
                    return ::scn::custom_ranges::begin(t) ==
                           ::scn::custom_ranges::end(t);
                }

            public:
                template <typename T>
                constexpr auto operator()(T&& t) const noexcept(
                    noexcept(fn::impl(SCN_FWD(t), detail::priority_tag<2>{})))
                    -> decltype(fn::impl(SCN_FWD(t), detail::priority_tag<2>{}))
                {
                    return fn::impl(SCN_FWD(t), detail::priority_tag<2>{});
                }
            };
        }  // namespace _empty_ns
        namespace {
            constexpr auto& empty = detail::static_const<_empty_ns::fn>::value;
        }

        // sized_range
        namespace detail {
            struct sized_range_concept {
                template <typename T>
                auto _test_requires(T& t)
                    -> decltype(::scn::custom_ranges::size(t));
            };
        }  // namespace detail
        template <typename T>
        struct sized_range
            : std::integral_constant<
                  bool,
                  range<T>::value &&
                      !disable_sized_range<detail::remove_cvref_t<T>>::value &&
                      detail::_requires<detail::sized_range_concept,
                                        T>::value> {
        };

        // contiguous_range
        namespace detail {
            struct contiguous_range_concept {
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename T>
                static auto test(int) -> typename std::enable_if<
                    _requires<contiguous_range_concept, T>::value,
                    std::true_type>::type;

                template <typename T>
                auto _test_requires(T& t)
                    -> decltype(requires_expr<std::is_same<
                                    decltype(::scn::custom_ranges::data(t)),
                                    typename std::add_pointer<
                                        range_reference_t<T>>::type>::value>{});
            };
        }  // namespace detail
        template <typename T>
        struct contiguous_range
            : decltype(detail::contiguous_range_concept::test<T>(0)) {
        };

        // subrange
        template <typename D>
        class view_interface : public view_base {
            static_assert(std::is_class<D>::value, "");
            static_assert(
                std::is_same<D, typename std::remove_cv<D>::type>::value,
                "");

        private:
            SCN_CONSTEXPR14 D& derived() noexcept
            {
                return static_cast<D&>(*this);
            }
            constexpr D& derived() const noexcept
            {
                return static_cast<const D&>(*this);
            }

        public:
            SCN_NODISCARD SCN_CONSTEXPR14 bool empty()
            {
                return ::scn::custom_ranges::begin(derived()) ==
                       ::scn::custom_ranges::end(derived());
            }
            SCN_NODISCARD constexpr bool empty() const
            {
                return ::scn::custom_ranges::begin(derived()) ==
                       ::scn::custom_ranges::end(derived());
            }

            template <typename R = D,
                      typename = decltype(::scn::custom_ranges::empty(
                          SCN_DECLVAL(R&)))>
            SCN_CONSTEXPR14 explicit operator bool()
            {
                return !::scn::custom_ranges::empty(derived());
            }
            template <typename R = D,
                      typename = decltype(::scn::custom_ranges::empty(
                          SCN_DECLVAL(const R&)))>
            constexpr explicit operator bool() const
            {
                return !::scn::custom_ranges::empty(derived());
            }

            template <typename R = D,
                      typename std::enable_if<
                          contiguous_range<R>::value>::type* = nullptr>
            auto data() -> decltype(std::addressof(
                *::scn::custom_ranges::begin(static_cast<R&>(*this))))
            {
                return ::scn::custom_ranges::empty(derived())
                           ? nullptr
                           : std::addressof(
                                 *::scn::custom_ranges::begin(derived()));
            }
            template <typename R = D,
                      typename std::enable_if<
                          contiguous_range<const R>::value>::type* = nullptr>
            auto data() const -> decltype(std::addressof(
                *::scn::custom_ranges::begin(static_cast<const R&>(*this))))
            {
                return ::scn::custom_ranges::empty(derived())
                           ? nullptr
                           : std::addressof(
                                 *::scn::custom_ranges::begin(derived()));
            }

            template <typename R = D,
                      typename std::enable_if<
                          range<R>::value &&
                          sized_sentinel_for<sentinel_t<R>, iterator_t<R>>::
                              value>::type* = nullptr>
            SCN_CONSTEXPR14 auto size()
                -> decltype(::scn::custom_ranges::end(static_cast<R&>(*this)) -
                            ::scn::custom_ranges::begin(static_cast<R&>(*this)))
            {
                return ::scn::custom_ranges::end(derived()) -
                       ::scn::custom_ranges::begin(derived());
            }

            template <
                typename R = D,
                typename std::enable_if<
                    range<const R>::value &&
                    sized_sentinel_for<sentinel_t<const R>,
                                       iterator_t<const R>>::value>::type* =
                    nullptr>
            constexpr auto size() const
                -> decltype(::scn::custom_ranges::end(
                                static_cast<const R&>(*this)) -
                            ::scn::custom_ranges::begin(
                                static_cast<const R&>(*this)))
            {
                return ::scn::custom_ranges::end(derived()) -
                       ::scn::custom_ranges::begin(derived());
            }
        };

        enum class subrange_kind : bool { unsized, sized };

        namespace detail {
            template <typename I, typename S>
            struct default_subrange_kind
                : std::integral_constant<subrange_kind,
                                         sized_sentinel_for<S, I>::value
                                             ? subrange_kind::sized
                                             : subrange_kind::unsized> {
            };
        }  // namespace detail

        namespace _subrange {
            template <typename I,
                      typename S = I,
                      subrange_kind = scn::custom_ranges::detail::
                          default_subrange_kind<I, S>::value>
            class subrange;
        }  // namespace _subrange

        using _subrange::subrange;

        namespace detail {
            struct pair_like_concept {
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename T,
                          typename = typename std::tuple_size<T>::type>
                static auto test(int) -> typename std::enable_if<
                    _requires<pair_like_concept, T>::value,
                    std::true_type>::type;

                template <typename T>
                auto _test_requires(T t) -> decltype(
                    requires_expr<
                        std::is_base_of<std::integral_constant<std::size_t, 2>,
                                        std::tuple_size<T>>::value>{},
                    std::declval<std::tuple_element<
                        0,
                        typename std::remove_const<T>::type>>(),
                    std::declval<std::tuple_element<
                        1,
                        typename std::remove_const<T>::type>>(),
                    requires_expr<std::is_convertible<
                        decltype(std::get<0>(t)),
                        const std::tuple_element<0, T>&>::value>{},
                    requires_expr<std::is_convertible<
                        decltype(std::get<1>(t)),
                        const std::tuple_element<1, T>&>::value>{});
            };
            template <typename T>
            struct pair_like
                : std::integral_constant<
                      bool,
                      !std::is_reference<T>::value &&
                          decltype(pair_like_concept::test<T>(0))::value> {
            };

            struct pair_like_convertible_to_concept {
                template <typename T, typename U, typename V>
                auto _test_requires(T&& t) -> decltype(
                    requires_expr<
                        std::is_convertible<decltype(std::get<0>(SCN_FWD(t))),
                                            U>::value>{},
                    requires_expr<
                        std::is_convertible<decltype(std::get<1>(SCN_FWD(t))),
                                            V>::value>{});
            };
            template <typename T, typename U, typename V>
            struct pair_like_convertible_to
                : std::integral_constant<
                      bool,
                      !range<T>::value &&
                          pair_like<
                              typename std::remove_reference<T>::type>::value &&
                          _requires<pair_like_convertible_to_concept, T, U, V>::
                              value> {
            };
            template <typename T, typename U, typename V>
            struct pair_like_convertible_from
                : std::integral_constant<
                      bool,
                      !range<T>::value &&
                          pair_like<
                              typename std::remove_reference<T>::type>::value &&
                          std::is_constructible<T, U, V>::value> {
            };

            struct iterator_sentinel_pair_concept {
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename T>
                static auto test(int) -> typename std::enable_if<
                    !range<T>::value && pair_like<T>::value &&
                        sentinel_for<
                            typename std::tuple_element<1, T>::type,
                            typename std::tuple_element<0, T>::type>::value,
                    std::true_type>::type;
            };
            template <typename T>
            struct iterator_sentinel_pair
                : decltype(iterator_sentinel_pair_concept::test<T>(0)) {
            };

            template <typename I, typename S, bool StoreSize = false>
            struct subrange_data {
                constexpr subrange_data() = default;
                constexpr subrange_data(I&& b, S&& e)
                    : begin(SCN_MOVE(b)), end(SCN_MOVE(e))
                {
                }
                template <bool Dependent = true>
                constexpr subrange_data(
                    I&& b,
                    S&& e,
                    typename std::enable_if<Dependent,
                                            iter_difference_t<I>>::type)
                    : begin(SCN_MOVE(b)), end(SCN_MOVE(e))
                {
                }

                constexpr iter_difference_t<I> get_size() const
                {
                    return distance(begin, end);
                }

                I begin{};
                S end{};
            };

            template <typename I, typename S>
            struct subrange_data<I, S, true> {
                constexpr subrange_data() = default;
                constexpr subrange_data(I&& b, S&& e, iter_difference_t<I> s)
                    : begin(SCN_MOVE(b)), end(SCN_MOVE(e)), size(s)
                {
                }

                constexpr iter_difference_t<I> get_size() const
                {
                    return size;
                }

                I begin{};
                S end{};
                iter_difference_t<I> size{0};
            };

            template <typename R, typename I, typename S, subrange_kind K>
            auto subrange_range_constructor_constraint_helper_fn(long)
                -> std::false_type;

            template <typename R, typename I, typename S, subrange_kind K>
            auto subrange_range_constructor_constraint_helper_fn(int) ->
                typename std::enable_if<
                    forwarding_range<R>::value &&
                        std::is_convertible<iterator_t<R>, I>::value &&
                        std::is_convertible<sentinel_t<R>, S>::value,
                    std::true_type>::type;

            template <typename R, typename I, typename S, subrange_kind K>
            struct subrange_range_constructor_constraint_helper
                : decltype(subrange_range_constructor_constraint_helper_fn<R,
                                                                           I,
                                                                           S,
                                                                           K>(
                      0)) {
            };

            template <typename R>
            constexpr subrange_kind subrange_deduction_guide_helper()
            {
                return (sized_range<R>::value ||
                        sized_sentinel_for<sentinel_t<R>, iterator_t<R>>::value)
                           ? subrange_kind::sized
                           : subrange_kind::unsized;
            }

            template <typename T, typename U>
            struct not_same_as : std::integral_constant<
                                     bool,
                                     !std::is_same<remove_cvref_t<T>,
                                                   remove_cvref_t<U>>::value> {
            };
        }  // namespace detail

        namespace _subrange {
            template <typename I, typename S, subrange_kind K>
            class subrange : public view_interface<subrange<I, S, K>> {
                static_assert(sentinel_for<S, I>::value, "");
                static_assert(K == subrange_kind::sized ||
                                  !sized_sentinel_for<S, I>::value,
                              "");

                static constexpr bool _store_size =
                    K == subrange_kind::sized &&
                    !sized_sentinel_for<S, I>::value;

            public:
                using iterator = I;
                using sentinel = S;

                subrange() = default;

                template <bool SS = _store_size,
                          typename std::enable_if<!SS>::type* = nullptr>
                SCN_CONSTEXPR14 subrange(I i, S s)
                    : m_data{SCN_MOVE(i), SCN_MOVE(s)}
                {
                }
                template <bool Dependent = true,
                          subrange_kind KK = K,
                          typename std::enable_if<
                              KK == subrange_kind::sized>::type* = nullptr>
                SCN_CONSTEXPR14 subrange(
                    I i,
                    S s,
                    typename std::enable_if<Dependent,
                                            iter_difference_t<I>>::type n)
                    : m_data{SCN_MOVE(i), SCN_MOVE(s), n}
                {
                }

                constexpr I begin() const noexcept
                {
                    return m_data.begin;
                }

                constexpr S end() const noexcept
                {
                    return m_data.end;
                }

                SCN_NODISCARD constexpr bool empty() const noexcept
                {
                    return m_data.begin == m_data.end;
                }

                template <subrange_kind KK = K,
                          typename std::enable_if<
                              KK == subrange_kind::sized>::type* = nullptr>
                constexpr iter_difference_t<I> size() const noexcept
                {
                    return m_data.get_size();
                }

            private:
                detail::subrange_data<I, S, _store_size> m_data{};
            };

            template <typename I, typename S, subrange_kind K>
            I begin(subrange<I, S, K>&& r) noexcept
            {
                return r.begin();
            }
            template <typename I, typename S, subrange_kind K>
            S end(subrange<I, S, K>&& r) noexcept
            {
                return r.end();
            }
        }  // namespace _subrange

        namespace detail {
            template <std::size_t N>
            struct subrange_get_impl;
            template <>
            struct subrange_get_impl<0> {
                template <typename I, typename S, subrange_kind K>
                static auto get(const subrange<I, S, K>& s)
                    -> decltype(s.begin())
                {
                    return s.begin();
                }
            };
            template <>
            struct subrange_get_impl<1> {
                template <typename I, typename S, subrange_kind K>
                static auto get(const subrange<I, S, K>& s) -> decltype(s.end())
                {
                    return s.end();
                }
            };
        }  // namespace detail

        template <std::size_t N,
                  typename I,
                  typename S,
                  subrange_kind K,
                  typename std::enable_if<(N < 2)>::type* = nullptr>
        auto get(const subrange<I, S, K>& s)
            -> decltype(detail::subrange_get_impl<N>::get(s))
        {
            return detail::subrange_get_impl<N>::get(s);
        }

        // reconstructible_range
        template <typename R>
        struct pair_reconstructible_range
            : std::integral_constant<
                  bool,
                  range<R>::value &&
                      forwarding_range<
                          typename std::remove_reference<R>::type>::value &&
                      std::is_constructible<R, iterator_t<R>, sentinel_t<R>>::
                          value> {
        };
        template <typename R>
        struct reconstructible_range
            : std::integral_constant<
                  bool,
                  range<R>::value &&
                      forwarding_range<
                          typename std::remove_reference<R>::type>::value &&
                      std::is_constructible<
                          R,
                          subrange<iterator_t<R>, sentinel_t<R>>>::value> {
        };
    }  // namespace custom_ranges

    namespace polyfill_2a {
        // bidir iterator
        namespace detail {
            struct bidirectional_iterator_concept {
                template <typename I>
                auto _test_requires(I i)
                    -> decltype(custom_ranges::detail::requires_expr<
                                std::is_same<decltype(i--), I>::value>{});
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename I>
                static auto test(int) -> typename std::enable_if<
                    std::is_base_of<
                        custom_ranges::bidirectional_iterator_tag,
                        custom_ranges::iterator_category_t<I>>::value &&
                        custom_ranges::detail::
                            _requires<bidirectional_iterator_concept, I>::value,
                    std::true_type>::type;
            };
        }  // namespace detail
        template <typename I>
        struct bidirectional_iterator
            : decltype(detail::bidirectional_iterator_concept::test<I>(0)) {
        };

        // random access iterator
        namespace detail {
            struct random_access_iterator_concept {
                template <typename I>
                auto _test_requires(I i,
                                    const I j,
                                    const custom_ranges::iter_difference_t<I> n)
                    -> decltype(valid_expr(
                        j + n,
                        custom_ranges::detail::requires_expr<
                            std::is_same<decltype(j + n), I>::value>{},
                        n + j,
#ifndef _MSC_VER
                        custom_ranges::detail::requires_expr<
                            std::is_same<decltype(n + j), I>::value>{},
#endif
                        j - n,
                        custom_ranges::detail::requires_expr<
                            std::is_same<decltype(j - n), I>::value>{},
                        j[n],
                        custom_ranges::detail::requires_expr<std::is_same<
                            decltype(j[n]),
                            custom_ranges::iter_reference_t<I>>::value>{},
                        custom_ranges::detail::requires_expr<
                            std::is_convertible<decltype(i < j),
                                                bool>::value>{}));
                template <typename>
                static auto test(long) -> std::false_type;
                template <typename I>
                static auto test(int) -> typename std::enable_if<
                    bidirectional_iterator<I>::value &&
                        std::is_base_of<
                            custom_ranges::random_access_iterator_tag,
                            custom_ranges::iterator_category_t<I>>::value &&
                        custom_ranges::sized_sentinel_for<I, I>::value &&
                        custom_ranges::detail::
                            _requires<random_access_iterator_concept, I>::value,
                    std::true_type>::type;
            };
        }  // namespace detail
        template <typename I>
        struct random_access_iterator
            : decltype(detail::random_access_iterator_concept::test<I>(0)) {
        };
    }  // namespace polyfill_2a

    namespace custom_ranges {
        // advance
        namespace _advance {
            struct fn {
            private:
                template <typename T>
                static constexpr T abs(T t)
                {
                    return t < T{0} ? -t : t;
                }

                template <
                    typename R,
                    typename std::enable_if<polyfill_2a::random_access_iterator<
                        R>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 void impl(R& r, iter_difference_t<R> n)
                {
                    r += n;
                }

                template <
                    typename I,
                    typename std::enable_if<
                        polyfill_2a::bidirectional_iterator<I>::value &&
                        !polyfill_2a::random_access_iterator<I>::value>::type* =
                        nullptr>
                static SCN_CONSTEXPR14 void impl(I& i, iter_difference_t<I> n)
                {
                    constexpr auto zero = iter_difference_t<I>{0};

                    if (n > zero) {
                        while (n-- > zero) {
                            ++i;
                        }
                    }
                    else {
                        while (n++ < zero) {
                            --i;
                        }
                    }
                }

                template <
                    typename I,
                    typename std::enable_if<
                        !polyfill_2a::bidirectional_iterator<I>::value>::type* =
                        nullptr>
                static SCN_CONSTEXPR14 void impl(I& i, iter_difference_t<I> n)
                {
                    while (n-- > iter_difference_t<I>{0}) {
                        ++i;
                    }
                }

                template <
                    typename I,
                    typename S,
                    typename std::enable_if<
                        std::is_assignable<I&, S>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 void impl(I& i,
                                                 S bound,
                                                 detail::priority_tag<2>)
                {
                    i = SCN_MOVE(bound);
                }

                template <typename I,
                          typename S,
                          typename std::enable_if<
                              sized_sentinel_for<S, I>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 void impl(I& i,
                                                 S bound,
                                                 detail::priority_tag<1>)
                {
                    fn::impl(i, bound - i);
                }

                template <typename I, typename S>
                static SCN_CONSTEXPR14 void impl(I& i,
                                                 S bound,
                                                 detail::priority_tag<0>)
                {
                    while (i != bound) {
                        ++i;
                    }
                }

                template <typename I,
                          typename S,
                          typename std::enable_if<
                              sized_sentinel_for<S, I>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 auto impl(I& i,
                                                 iter_difference_t<I> n,
                                                 S bound)
                    -> iter_difference_t<I>
                {
                    if (fn::abs(n) >= fn::abs(bound - i)) {
                        auto dist = bound - i;
                        fn::impl(i, bound, detail::priority_tag<2>{});
                        return dist;
                    }
                    else {
                        fn::impl(i, n);
                        return n;
                    }
                }

                template <
                    typename I,
                    typename S,
                    typename std::enable_if<
                        polyfill_2a::bidirectional_iterator<I>::value &&
                        !sized_sentinel_for<S, I>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 auto impl(I& i,
                                                 iter_difference_t<I> n,
                                                 S bound)
                    -> iter_difference_t<I>
                {
                    constexpr iter_difference_t<I> zero{0};
                    iter_difference_t<I> counter{0};

                    if (n < zero) {
                        do {
                            --i;
                            --counter;
                        } while (++n < zero && i != bound);
                    }
                    else {
                        while (n-- > zero && i != bound) {
                            ++i;
                            ++counter;
                        }
                    }

                    return counter;
                }

                template <
                    typename I,
                    typename S,
                    typename std::enable_if<
                        !polyfill_2a::bidirectional_iterator<I>::value &&
                        !sized_sentinel_for<S, I>::value>::type* = nullptr>
                static SCN_CONSTEXPR14 auto impl(I& i,
                                                 iter_difference_t<I> n,
                                                 S bound)
                    -> iter_difference_t<I>
                {
                    constexpr iter_difference_t<I> zero{0};
                    iter_difference_t<I> counter{0};

                    while (n-- > zero && i != bound) {
                        ++i;
                        ++counter;
                    }

                    return counter;
                }

            public:
                template <typename I>
                SCN_CONSTEXPR14 void operator()(I& i,
                                                iter_difference_t<I> n) const
                {
                    fn::impl(i, n);
                }

                template <typename I,
                          typename S,
                          typename std::enable_if<
                              sentinel_for<S, I>::value>::type* = nullptr>
                SCN_CONSTEXPR14 void operator()(I& i, S bound) const
                {
                    fn::impl(i, bound, detail::priority_tag<2>{});
                }

                template <typename I,
                          typename S,
                          typename std::enable_if<
                              sentinel_for<S, I>::value>::type* = nullptr>
                SCN_CONSTEXPR14 iter_difference_t<I>
                operator()(I& i, iter_difference_t<I> n, S bound) const
                {
                    return n - fn::impl(i, n, bound);
                }
            };
        }  // namespace _advance
        namespace {
            constexpr auto& advance = detail::static_const<_advance::fn>::value;
        }

        // distance
        SCN_GCC_PUSH
        SCN_GCC_IGNORE("-Wnoexcept")
        namespace _distance {
            struct fn {
            private:
                template <typename I, typename S>
                static SCN_CONSTEXPR14 auto impl(I i,
                                                 S s) noexcept(noexcept(s - i))
                    -> typename std::enable_if<sized_sentinel_for<S, I>::value,
                                               iter_difference_t<I>>::type
                {
                    return s - i;
                }

                template <typename I, typename S>
                static SCN_CONSTEXPR14 auto impl(I i, S s) noexcept(
                    noexcept(i != s, ++i, ++SCN_DECLVAL(iter_difference_t<I>&)))
                    -> typename std::enable_if<!sized_sentinel_for<S, I>::value,
                                               iter_difference_t<I>>::type
                {
                    iter_difference_t<I> counter{0};
                    while (i != s) {
                        ++i;
                        ++counter;
                    }
                    return counter;
                }

                template <typename R>
                static SCN_CONSTEXPR14 auto impl(R&& r) noexcept(
                    noexcept(::scn::custom_ranges::size(r))) ->
                    typename std::enable_if<
                        sized_range<R>::value,
                        iter_difference_t<iterator_t<R>>>::type
                {
                    return static_cast<iter_difference_t<iterator_t<R>>>(
                        ::scn::custom_ranges::size(r));
                }

                template <typename R>
                static SCN_CONSTEXPR14 auto impl(R&& r) noexcept(
                    noexcept(fn::impl(::scn::custom_ranges::begin(r),
                                      ::scn::custom_ranges::end(r)))) ->
                    typename std::enable_if<
                        !sized_range<R>::value,
                        iter_difference_t<iterator_t<R>>>::type
                {
                    return fn::impl(::scn::custom_ranges::begin(r),
                                    ::scn::custom_ranges::end(r));
                }

            public:
                template <typename I, typename S>
                SCN_CONSTEXPR14 auto operator()(I first, S last) const
                    noexcept(noexcept(fn::impl(SCN_MOVE(first),
                                               SCN_MOVE(last)))) ->
                    typename std::enable_if<sentinel_for<S, I>::value,
                                            iter_difference_t<I>>::type
                {
                    return fn::impl(SCN_MOVE(first), SCN_MOVE(last));
                }

                template <typename R>
                SCN_CONSTEXPR14 auto operator()(R&& r) const
                    noexcept(noexcept(fn::impl(SCN_FWD(r)))) ->
                    typename std::enable_if<
                        range<R>::value,
                        iter_difference_t<iterator_t<R>>>::type
                {
                    return fn::impl(SCN_FWD(r));
                }
            };
        }  // namespace _distance
        namespace {
            constexpr auto& distance =
                detail::static_const<_distance::fn>::value;
        }
        SCN_GCC_POP  // -Wnoexcept
    }                // namespace custom_ranges

    namespace polyfill_2a {
        template <typename T>
        using iter_value_t = ::scn::custom_ranges::iter_value_t<T>;
        template <typename T>
        using iter_reference_t = ::scn::custom_ranges::iter_reference_t<T>;
        template <typename T>
        using iter_difference_t = ::scn::custom_ranges::iter_difference_t<T>;
    }  // namespace polyfill_2a

    SCN_END_NAMESPACE
}  // namespace scn

namespace std {
    template <typename I, typename S, ::scn::custom_ranges::subrange_kind K>
    struct tuple_size<::scn::custom_ranges::subrange<I, S, K>>
        : public integral_constant<size_t, 2> {
    };

    template <typename I, typename S, ::scn::custom_ranges::subrange_kind K>
    struct tuple_element<0, ::scn::custom_ranges::subrange<I, S, K>> {
        using type = I;
    };
    template <typename I, typename S, ::scn::custom_ranges::subrange_kind K>
    struct tuple_element<1, ::scn::custom_ranges::subrange<I, S, K>> {
        using type = S;
    };

    using ::scn::custom_ranges::get;
}  // namespace std

#define SCN_CHECK_CONCEPT(C) C::value

#endif  // SCN_RANGES_CUSTOM_IMPL_H