summaryrefslogtreecommitdiffstats
path: root/src/third-party/scnlib/include/scn/scan/ignore.h
blob: 415a87711c0459c176003276459e5e94e3958660 (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
// 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_SCAN_IGNORE_H
#define SCN_SCAN_IGNORE_H

#include "common.h"

namespace scn {
    SCN_BEGIN_NAMESPACE

    namespace detail {
        template <typename CharT>
        struct ignore_iterator {
            using value_type = CharT;
            using pointer = value_type*;
            using reference = value_type&;
            using difference_type = std::ptrdiff_t;
            using iterator_category = std::output_iterator_tag;

            constexpr ignore_iterator() = default;

            SCN_CONSTEXPR14 ignore_iterator& operator=(CharT) noexcept
            {
                return *this;
            }
            constexpr const ignore_iterator& operator=(CharT) const noexcept
            {
                return *this;
            }

            SCN_CONSTEXPR14 ignore_iterator& operator*() noexcept
            {
                return *this;
            }
            constexpr const ignore_iterator& operator*() const noexcept
            {
                return *this;
            }

            SCN_CONSTEXPR14 ignore_iterator& operator++() noexcept
            {
                return *this;
            }
            constexpr const ignore_iterator& operator++() const noexcept
            {
                return *this;
            }
        };

        template <typename CharT>
        struct ignore_iterator_n {
            using value_type = CharT;
            using pointer = value_type*;
            using reference = value_type&;
            using difference_type = std::ptrdiff_t;
            using iterator_category = std::output_iterator_tag;

            ignore_iterator_n() = default;
            ignore_iterator_n(difference_type n) : i(n) {}

            constexpr const ignore_iterator_n& operator=(CharT) const noexcept
            {
                return *this;
            }

            constexpr const ignore_iterator_n& operator*() const noexcept
            {
                return *this;
            }

            SCN_CONSTEXPR14 ignore_iterator_n& operator++() noexcept
            {
                ++i;
                return *this;
            }

            constexpr bool operator==(const ignore_iterator_n& o) const noexcept
            {
                return i == o.i;
            }
            constexpr bool operator!=(const ignore_iterator_n& o) const noexcept
            {
                return !(*this == o);
            }

            difference_type i{0};
        };

        template <typename WrappedRange,
                  typename Until,
                  typename CharT = typename WrappedRange::char_type>
        error ignore_until_impl(WrappedRange& r, Until until)
        {
            ignore_iterator<CharT> it{};
            return read_until_space(r, it, until_pred<CharT>{until}, false);
        }

        template <typename WrappedRange,
                  typename Until,
                  typename CharT = typename WrappedRange::char_type>
        error ignore_until_n_impl(WrappedRange& r,
                                  ranges::range_difference_t<WrappedRange> n,
                                  Until until)
        {
            ignore_iterator_n<CharT> begin{}, end{n};
            return read_until_space_ranged(r, begin, end,
                                           until_pred<CharT>{until}, false);
        }
    }  // namespace detail

    /**
     * Advances the beginning of \c r until \c until is found.
     */
#if SCN_DOXYGEN
    template <typename Range, typename Until>
    auto ignore_until(Range&& r, Until until)
        -> detail::scan_result_for_range<Range>;
#else
    template <typename Range, typename Until>
    SCN_NODISCARD auto ignore_until(Range&& r, Until until)
        -> detail::scan_result_for_range<Range>
    {
        auto wrapped = wrap(SCN_FWD(r));
        auto err = detail::ignore_until_impl(wrapped, until);
        if (!err) {
            auto e = wrapped.reset_to_rollback_point();
            if (!e) {
                err = e;
            }
        }
        else {
            wrapped.set_rollback_point();
        }
        return detail::wrap_result(
            wrapped_error{err}, detail::range_tag<Range>{}, SCN_MOVE(wrapped));
    }
#endif

    /**
     * Advances the beginning of \c r until \c until is found, or the
     * beginning has been advanced \c n times.
     *
     * `Until` can be the `r` character type (`char` or `wchar_t`), or
     * `code_point`.
     */
#if SCN_DOXYGEN
    template <typename Range, typename Until>
    auto ignore_until_n(Range&& r,
                        ranges::range_difference_t<Range> n,
                        Until until) -> detail::scan_result_for_range<Range>;
#else
    template <typename Range, typename Until>
    SCN_NODISCARD auto ignore_until_n(Range&& r,
                                      ranges::range_difference_t<Range> n,
                                      Until until)
        -> detail::scan_result_for_range<Range>
    {
        auto wrapped = wrap(SCN_FWD(r));
        auto err = detail::ignore_until_n_impl(wrapped, n, until);
        if (!err) {
            auto e = wrapped.reset_to_rollback_point();
            if (!e) {
                err = e;
            }
        }
        return detail::wrap_result(
            wrapped_error{err}, detail::range_tag<Range>{}, SCN_MOVE(wrapped));
    }
#endif

    SCN_END_NAMESPACE
}  // namespace scn

#endif