summaryrefslogtreecommitdiffstats
path: root/src/third-party/scnlib/include/scn/detail/range.h
blob: 5b8802f89682c498b4fa9b79c90c1310c9b57d9d (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
// 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

#ifndef SCN_DETAIL_RANGE_H
#define SCN_DETAIL_RANGE_H

#include "../ranges/ranges.h"
#include "../util/algorithm.h"
#include "../util/memory.h"
#include "error.h"
#include "vectored.h"

namespace scn {
    SCN_BEGIN_NAMESPACE

    namespace detail {
        namespace _reset_begin_iterator {
            struct fn {
            private:
                template <typename Iterator>
                static auto impl(Iterator& it, priority_tag<1>) noexcept(
                    noexcept(it.reset_begin_iterator()))
                    -> decltype(it.reset_begin_iterator())
                {
                    return it.reset_begin_iterator();
                }

                template <typename Iterator>
                static void impl(Iterator&, size_t, priority_tag<0>) noexcept
                {
                }

            public:
                template <typename Iterator>
                auto operator()(Iterator& it) const
                    noexcept(noexcept(fn::impl(it, priority_tag<1>{})))
                        -> decltype(fn::impl(it, priority_tag<1>{}))
                {
                    return fn::impl(it, priority_tag<1>{});
                }
            };
        }  // namespace _reset_begin_iterator
        namespace {
            static constexpr auto& reset_begin_iterator =
                static_const<detail::_reset_begin_iterator::fn>::value;
        }

        template <typename Iterator, typename = void>
        struct extract_char_type;
        template <typename Iterator>
        struct extract_char_type<
            Iterator,
            typename std::enable_if<std::is_integral<
                polyfill_2a::iter_value_t<Iterator>>::value>::type> {
            using type = polyfill_2a::iter_value_t<Iterator>;
        };
        template <typename Iterator>
        struct extract_char_type<
            Iterator,
            void_t<
                typename std::enable_if<!std::is_integral<
                    polyfill_2a::iter_value_t<Iterator>>::value>::type,
                typename polyfill_2a::iter_value_t<Iterator>::success_type>> {
            using type =
                typename polyfill_2a::iter_value_t<Iterator>::success_type;
        };

        template <typename Range, typename = void>
        struct is_direct_impl
            : std::is_integral<ranges::range_value_t<const Range>> {
        };

        template <typename Range>
        struct reconstruct_tag {
        };

        template <
            typename Range,
            typename Iterator,
            typename Sentinel,
            typename = typename std::enable_if<
                std::is_constructible<Range, Iterator, Sentinel>::value>::type>
        Range reconstruct(reconstruct_tag<Range>, Iterator begin, Sentinel end)
        {
            return {begin, end};
        }
#if SCN_HAS_STRING_VIEW
        // std::string_view is not reconstructible pre-C++20
        template <typename CharT,
                  typename Traits,
                  typename Iterator,
                  typename Sentinel>
        std::basic_string_view<CharT, Traits> reconstruct(
            reconstruct_tag<std::basic_string_view<CharT, Traits>>,
            Iterator begin,
            Sentinel end)
        {
            // On MSVC, string_view can't even be constructed from its
            // iterators!
            return {::scn::detail::to_address(begin),
                    static_cast<size_t>(ranges::distance(begin, end))};
        }
#endif  // SCN_HAS_STRING_VIEW

        template <typename T, bool>
        struct range_wrapper_storage;
        template <typename T>
        struct range_wrapper_storage<T, true> {
            using type = remove_cvref_t<T>;
            using range_type = const type&;

            const type* value{nullptr};

            range_wrapper_storage() = default;
            range_wrapper_storage(const type& v, dummy_type)
                : value(std::addressof(v))
            {
            }

            const type& get() const& noexcept
            {
                return *value;
            }
            type&& get() && noexcept
            {
                return *value;
            }
        };
        template <typename T>
        struct range_wrapper_storage<T, false> {
            using range_type = T;

            T value{};

            range_wrapper_storage() = default;
            template <typename U>
            range_wrapper_storage(U&& v, dummy_type) : value(SCN_FWD(v))
            {
            }

            const T& get() const& noexcept
            {
                return value;
            }
            T&& get() && noexcept
            {
                return value;
            }
        };

        template <typename T>
        using _range_wrapper_marker = typename T::range_wrapper_marker;

        template <typename T>
        struct _has_range_wrapper_marker
            : custom_ranges::detail::exists<_range_wrapper_marker, T> {
        };

        /**
         * Wraps a source range for more consistent behavior
         */
        template <typename Range>
        class range_wrapper {
        public:
            using range_type = Range;
            using range_nocvref_type = remove_cvref_t<Range>;
            using iterator = ranges::iterator_t<const range_nocvref_type>;
            using sentinel = ranges::sentinel_t<const range_nocvref_type>;
            using char_type = typename extract_char_type<iterator>::type;
            using difference_type =
                ranges::range_difference_t<const range_nocvref_type>;
            using storage_type =
                range_wrapper_storage<Range, std::is_reference<Range>::value>;
            using storage_range_type = typename storage_type::range_type;

            using range_wrapper_marker = void;

            template <
                typename R,
                typename = typename std::enable_if<
                    !_has_range_wrapper_marker<remove_cvref_t<R>>::value>::type>
            range_wrapper(R&& r)
                : m_range(SCN_FWD(r), dummy_type{}),
                  m_begin(ranges::cbegin(m_range.get()))
            {
            }

            range_wrapper(const range_wrapper& o) : m_range(o.m_range)
            {
                const auto n =
                    ranges::distance(o.begin_underlying(), o.m_begin);
                m_begin = ranges::cbegin(m_range.get());
                ranges::advance(m_begin, n);
                m_read = o.m_read;
            }
            range_wrapper& operator=(const range_wrapper& o)
            {
                const auto n =
                    ranges::distance(o.begin_underlying(), o.m_begin);
                m_range = o.m_range;
                m_begin = ranges::cbegin(m_range.get());
                ranges::advance(m_begin, n);
                m_read = o.m_read;
                return *this;
            }

            range_wrapper(range_wrapper&& o) noexcept
            {
                const auto n =
                    ranges::distance(o.begin_underlying(), o.m_begin);
                m_range = SCN_MOVE(o.m_range);
                m_begin = ranges::cbegin(m_range.get());
                ranges::advance(m_begin, n);
                m_read = exchange(o.m_read, 0);
            }
            range_wrapper& operator=(range_wrapper&& o) noexcept
            {
                reset_to_rollback_point();

                const auto n =
                    ranges::distance(o.begin_underlying(), o.m_begin);
                m_range = SCN_MOVE(o.m_range);
                m_begin = ranges::cbegin(m_range.get());
                ranges::advance(m_begin, n);
                m_read = exchange(o.m_read, 0);
                return *this;
            }

            ~range_wrapper() = default;

            iterator begin() const noexcept
            {
                return m_begin;
            }
            SCN_GCC_PUSH
            SCN_GCC_IGNORE("-Wnoexcept")
            sentinel end() const noexcept(
                noexcept(ranges::end(SCN_DECLVAL(const storage_type&).get())))
            {
                return ranges::end(m_range.get());
            }
            SCN_GCC_POP

            struct dummy {
            };

            /**
             * Returns `true` if `begin() == end()`.
             */
            bool empty() const
            {
                return begin() == end();
            }

            /**
             * Advance the begin iterator by `n` characters.
             */
            iterator advance(difference_type n = 1) noexcept
            {
                SCN_EXPECT(_advance_check(
                    n, std::integral_constant<bool, is_contiguous>{}));
                m_read += n;
                ranges::advance(m_begin, n);
                return m_begin;
            }

            /// @{
            /**
             * Advance the begin iterator, until it's equal to `it`.
             * Assumes that `it` is reachable by repeatedly incrementing begin,
             * will hang otherwise.
             */
            template <typename R = range_nocvref_type,
                      typename std::enable_if<SCN_CHECK_CONCEPT(
                          ranges::sized_range<R>)>::type* = nullptr>
            void advance_to(iterator it) noexcept
            {
                const auto diff = ranges::distance(m_begin, it);
                m_read += diff;
                m_begin = it;
            }
            template <typename R = range_nocvref_type,
                      typename std::enable_if<SCN_CHECK_CONCEPT(
                          !ranges::sized_range<R>)>::type* = nullptr>
            void advance_to(iterator it) noexcept
            {
                while (m_begin != it) {
                    ++m_read;
                    ++m_begin;
                }
            }
            /// @}

            /**
             * Returns the begin iterator of the underlying source range, is not
             * necessarily equal to `begin()`.
             */
            iterator begin_underlying() const noexcept(noexcept(
                ranges::cbegin(SCN_DECLVAL(const range_nocvref_type&))))
            {
                return ranges::cbegin(m_range.get());
            }

            /**
             * Returns the underlying source range.
             * Note that `range_underlying().begin()` may not be equal to
             * `begin()`.
             */
            const range_type& range_underlying() const noexcept
            {
                return m_range.get();
            }

            /**
             * Returns a pointer to the beginning of the range.
             * `*this` must be contiguous.
             */
            template <typename R = range_nocvref_type,
                      typename std::enable_if<SCN_CHECK_CONCEPT(
                          ranges::contiguous_range<R>)>::type* = nullptr>
            auto data() const
                noexcept(noexcept(*SCN_DECLVAL(ranges::iterator_t<const R>)))
                    -> decltype(std::addressof(
                        *SCN_DECLVAL(ranges::iterator_t<const R>)))
            {
                return std::addressof(*m_begin);
            }
            SCN_GCC_PUSH
            SCN_GCC_IGNORE("-Wnoexcept")
            /**
             * Returns `end() - begin()`.
             * `*this` must be sized.
             */
            template <typename R = range_nocvref_type,
                      typename std::enable_if<SCN_CHECK_CONCEPT(
                          ranges::sized_range<R>)>::type* = nullptr>
            auto size() const noexcept(noexcept(
                ranges::distance(SCN_DECLVAL(ranges::iterator_t<const R>),
                                 SCN_DECLVAL(ranges::sentinel_t<const R>))))
                -> decltype(ranges::distance(
                    SCN_DECLVAL(ranges::iterator_t<const R>),
                    SCN_DECLVAL(ranges::sentinel_t<const R>)))
            {
                return ranges::distance(m_begin, end());
            }
            SCN_GCC_POP
            struct dummy2 {
            };

            template <typename R = range_nocvref_type,
                      typename std::enable_if<provides_buffer_access_impl<
                          R>::value>::type* = nullptr>
            span<const char_type> get_buffer_and_advance(
                size_t max_size = std::numeric_limits<size_t>::max())
            {
                auto buf = get_buffer(m_range.get(), begin(), max_size);
                if (buf.size() == 0) {
                    return buf;
                }
                advance(buf.ssize());
                return buf;
            }

            /**
             * Reset `begin()` to the rollback point, as if by repeatedly
             * calling `operator--()` on the begin iterator.
             *
             * Returns `error::unrecoverable_source_error` on failure.
             *
             * \see set_rollback_point()
             */
            error reset_to_rollback_point()
            {
                for (; m_read != 0; --m_read) {
                    --m_begin;
                    if (m_begin == end()) {
                        return {error::unrecoverable_source_error,
                                "Putback failed"};
                    }
                }
                return {};
            }
            /**
             * Sets the rollback point equal to the current `begin()` iterator.
             *
             * \see reset_to_rollback_point()
             */
            void set_rollback_point()
            {
                m_read = 0;
            }

            void reset_begin_iterator()
            {
                detail::reset_begin_iterator(m_begin);
            }

            /**
             * Construct a new source range from `begin()` and `end()`, and wrap
             * it in a new `range_wrapper`.
             */
            template <typename R>
            auto reconstruct_and_rewrap() && -> range_wrapper<R>
            {
                auto reconstructed =
                    reconstruct(reconstruct_tag<R>{}, begin(), end());
                return {SCN_MOVE(reconstructed)};
            }

            /**
             * `true` if `value_type` is a character type (`char` or `wchar_t`)
             * `false` if it's an `expected` containing a character
             */
            static constexpr bool is_direct =
                is_direct_impl<range_nocvref_type>::value;
            // can call .data() and memcpy
            /**
             * `true` if `this->data()` can be called, and `memcpy` can be
             * performed on it.
             */
            static constexpr bool is_contiguous =
                SCN_CHECK_CONCEPT(ranges::contiguous_range<range_nocvref_type>);
            /**
             * `true` if the range provides a way to access a contiguous buffer
             * on it (`detail::get_buffer()`), which may not provide the entire
             * source data, e.g. a `span` of `span`s (vectored I/O).
             */
            static constexpr bool provides_buffer_access =
                provides_buffer_access_impl<range_nocvref_type>::value;

        private:
            template <typename R = Range>
            bool _advance_check(std::ptrdiff_t n, std::true_type)
            {
                SCN_CLANG_PUSH
                SCN_CLANG_IGNORE("-Wzero-as-null-pointer-constant")
                return m_begin + n <= end();
                SCN_CLANG_POP
            }
            template <typename R = Range>
            bool _advance_check(std::ptrdiff_t, std::false_type)
            {
                return true;
            }

            storage_type m_range;
            iterator m_begin;
            mutable difference_type m_read{0};
        };

        namespace _wrap {
            struct fn {
            private:
                template <typename Range>
                static range_wrapper<Range> impl(const range_wrapper<Range>& r,
                                                 priority_tag<4>) noexcept
                {
                    return r;
                }
                template <typename Range>
                static range_wrapper<Range> impl(range_wrapper<Range>&& r,
                                                 priority_tag<4>) noexcept
                {
                    return SCN_MOVE(r);
                }

                template <typename Range>
                static auto impl(Range&& r, priority_tag<3>) noexcept(
                    noexcept(SCN_FWD(r).wrap())) -> decltype(SCN_FWD(r).wrap())
                {
                    return SCN_FWD(r).wrap();
                }

                template <typename CharT, std::size_t N>
                static auto impl(CharT (&str)[N], priority_tag<2>) noexcept
                    -> range_wrapper<
                        basic_string_view<typename std::remove_cv<CharT>::type>>
                {
                    return {
                        basic_string_view<typename std::remove_cv<CharT>::type>(
                            str, str + N - 1)};
                }

                template <typename CharT, typename Allocator>
                static auto impl(
                    const std::basic_string<CharT,
                                            std::char_traits<CharT>,
                                            Allocator>& str,
                    priority_tag<2>) noexcept
                    -> range_wrapper<basic_string_view<CharT>>
                {
                    return {basic_string_view<CharT>{str.data(), str.size()}};
                }
                template <typename CharT, typename Allocator>
                static auto impl(
                    std::basic_string<CharT,
                                      std::char_traits<CharT>,
                                      Allocator>&& str,
                    priority_tag<2>) noexcept(std::
                                                  is_nothrow_move_constructible<
                                                      decltype(str)>::value)
                    -> range_wrapper<std::basic_string<CharT,
                                                       std::char_traits<CharT>,
                                                       Allocator>>
                {
                    return {SCN_MOVE(str)};
                }

#if SCN_HAS_STRING_VIEW
                template <typename CharT>
                static auto impl(const std::basic_string_view<CharT>& str,
                                 priority_tag<1>) noexcept
                    -> range_wrapper<basic_string_view<CharT>>
                {
                    return {basic_string_view<CharT>{str.data(), str.size()}};
                }
#endif
                template <typename T,
                          typename CharT = typename std::remove_const<T>::type>
                static auto impl(span<T> s, priority_tag<2>) noexcept
                    -> range_wrapper<basic_string_view<CharT>>
                {
                    return {basic_string_view<CharT>{s.data(), s.size()}};
                }

                template <typename Range,
                          typename = typename std::enable_if<
                              SCN_CHECK_CONCEPT(ranges::view<Range>)>::type>
                static auto impl(Range r, priority_tag<1>) noexcept
                    -> range_wrapper<Range>
                {
                    return {r};
                }

                template <typename Range>
                static auto impl(const Range& r, priority_tag<0>) noexcept
                    -> range_wrapper<Range&>
                {
                    static_assert(SCN_CHECK_CONCEPT(ranges::range<Range>),
                                  "Input needs to be a Range");
                    return {r};
                }
                template <typename Range,
                          typename = typename std::enable_if<
                              !std::is_reference<Range>::value>::type>
                static auto impl(Range&& r, priority_tag<0>) noexcept
                    -> range_wrapper<Range>
                {
                    static_assert(SCN_CHECK_CONCEPT(ranges::range<Range>),
                                  "Input needs to be a Range");
                    return {SCN_MOVE(r)};
                }

            public:
                template <typename Range>
                auto operator()(Range&& r) const
                    noexcept(noexcept(fn::impl(SCN_FWD(r), priority_tag<4>{})))
                        -> decltype(fn::impl(SCN_FWD(r), priority_tag<4>{}))
                {
                    return fn::impl(SCN_FWD(r), priority_tag<4>{});
                }
            };
        }  // namespace _wrap
    }      // namespace detail

    namespace {
        /**
         * Create a `range_wrapper` for any supported source range.
         */
        static constexpr auto& wrap =
            detail::static_const<detail::_wrap::fn>::value;
    }  // namespace

    template <typename Range>
    struct range_wrapper_for {
        using type = decltype(wrap(SCN_DECLVAL(Range)));
    };
    template <typename Range>
    using range_wrapper_for_t = typename range_wrapper_for<Range>::type;

    SCN_END_NAMESPACE
}  // namespace scn

#endif  // SCN_DETAIL_RANGE_H