1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_DETAIL_IMPL_ANY_SEGMENTS_ITER_HPP
11  
#ifndef BOOST_URL_DETAIL_IMPL_ANY_SEGMENTS_ITER_HPP
12  
#define BOOST_URL_DETAIL_IMPL_ANY_SEGMENTS_ITER_HPP
12  
#define BOOST_URL_DETAIL_IMPL_ANY_SEGMENTS_ITER_HPP
13  

13  

14  
#include <boost/url/encode.hpp>
14  
#include <boost/url/encode.hpp>
15  
#include <boost/url/rfc/pchars.hpp>
15  
#include <boost/url/rfc/pchars.hpp>
16  
#include <boost/url/rfc/detail/charsets.hpp>
16  
#include <boost/url/rfc/detail/charsets.hpp>
17  
#include <boost/url/detail/encode.hpp>
17  
#include <boost/url/detail/encode.hpp>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  
namespace detail {
21  
namespace detail {
22  

22  

23  
//------------------------------------------------
23  
//------------------------------------------------
24  
//
24  
//
25  
// segment_iter
25  
// segment_iter
26  
//
26  
//
27  
//------------------------------------------------
27  
//------------------------------------------------
28  

28  

29  
inline
29  
inline
30  
segment_iter::
30  
segment_iter::
31  
segment_iter(
31  
segment_iter(
32  
    core::string_view s_) noexcept
32  
    core::string_view s_) noexcept
33  
    : any_segments_iter(s_)
33  
    : any_segments_iter(s_)
34  
{
34  
{
35  
    front = s;
35  
    front = s;
36  
    fast_nseg = 1;
36  
    fast_nseg = 1;
37  
}
37  
}
38  

38  

39  
inline
39  
inline
40  
void
40  
void
41  
segment_iter::
41  
segment_iter::
42  
rewind() noexcept
42  
rewind() noexcept
43  
{
43  
{
44  
    at_end_ = false;
44  
    at_end_ = false;
45  
}
45  
}
46  

46  

47  
inline
47  
inline
48  
bool
48  
bool
49  
segment_iter::
49  
segment_iter::
50  
measure(
50  
measure(
51  
    std::size_t& n) noexcept
51  
    std::size_t& n) noexcept
52  
{
52  
{
53  
    if(at_end_)
53  
    if(at_end_)
54  
        return false;
54  
        return false;
55  
    encoding_opts opt;
55  
    encoding_opts opt;
56  
    opt.space_as_plus = false;
56  
    opt.space_as_plus = false;
57  
    n += encoded_size(
57  
    n += encoded_size(
58  
        s,
58  
        s,
59  
        encode_colons ?
59  
        encode_colons ?
60  
            nocolon_pchars :
60  
            nocolon_pchars :
61  
            pchars,
61  
            pchars,
62  
        opt);
62  
        opt);
63  
    at_end_ = true;
63  
    at_end_ = true;
64  
    return true;
64  
    return true;
65  
}
65  
}
66  

66  

67  
inline
67  
inline
68  
void
68  
void
69  
segment_iter::
69  
segment_iter::
70  
copy(
70  
copy(
71  
    char*& dest,
71  
    char*& dest,
72  
    char const* end) noexcept
72  
    char const* end) noexcept
73  
{
73  
{
74  
    encoding_opts opt;
74  
    encoding_opts opt;
75  
    opt.space_as_plus = false;
75  
    opt.space_as_plus = false;
76  
    dest += encode(
76  
    dest += encode(
77  
        dest,
77  
        dest,
78  
        end - dest,
78  
        end - dest,
79  
        s,
79  
        s,
80  
        encode_colons ?
80  
        encode_colons ?
81  
            nocolon_pchars :
81  
            nocolon_pchars :
82  
            pchars,
82  
            pchars,
83  
        opt);
83  
        opt);
84  
}
84  
}
85  

85  

86  
//------------------------------------------------
86  
//------------------------------------------------
87  
//
87  
//
88  
// segments_iter_base
88  
// segments_iter_base
89  
//
89  
//
90  
//------------------------------------------------
90  
//------------------------------------------------
91  

91  

92  
inline
92  
inline
93  
void
93  
void
94  
segments_iter_base::
94  
segments_iter_base::
95  
measure_impl(
95  
measure_impl(
96  
    std::size_t& n,
96  
    std::size_t& n,
97  
    core::string_view s,
97  
    core::string_view s,
98  
    bool encode_colons) noexcept
98  
    bool encode_colons) noexcept
99  
{
99  
{
100  
    encoding_opts opt;
100  
    encoding_opts opt;
101  
    opt.space_as_plus = false;
101  
    opt.space_as_plus = false;
102  
    n += encoded_size(
102  
    n += encoded_size(
103  
        s,
103  
        s,
104  
        encode_colons ?
104  
        encode_colons ?
105  
            nocolon_pchars :
105  
            nocolon_pchars :
106  
            pchars,
106  
            pchars,
107  
        opt);
107  
        opt);
108  
}
108  
}
109  

109  

110  
inline
110  
inline
111  
void
111  
void
112  
segments_iter_base::
112  
segments_iter_base::
113  
copy_impl(
113  
copy_impl(
114  
    char*& dest,
114  
    char*& dest,
115  
    char const* end,
115  
    char const* end,
116  
    core::string_view s,
116  
    core::string_view s,
117  
    bool encode_colons) noexcept
117  
    bool encode_colons) noexcept
118  
{
118  
{
119  
    encoding_opts opt;
119  
    encoding_opts opt;
120  
    opt.space_as_plus = false;
120  
    opt.space_as_plus = false;
121  
    dest += encode(
121  
    dest += encode(
122  
        dest,
122  
        dest,
123  
        end - dest,
123  
        end - dest,
124  
        s,
124  
        s,
125  
        encode_colons ?
125  
        encode_colons ?
126  
            nocolon_pchars :
126  
            nocolon_pchars :
127  
            pchars,
127  
            pchars,
128  
        opt);
128  
        opt);
129  
}
129  
}
130  

130  

131  
//------------------------------------------------
131  
//------------------------------------------------
132  
//
132  
//
133  
// segment_encoded_iter
133  
// segment_encoded_iter
134  
//
134  
//
135  
//------------------------------------------------
135  
//------------------------------------------------
136  

136  

137  
inline
137  
inline
138  
segment_encoded_iter::
138  
segment_encoded_iter::
139  
segment_encoded_iter(
139  
segment_encoded_iter(
140  
    pct_string_view const& s_) noexcept
140  
    pct_string_view const& s_) noexcept
141  
    : any_segments_iter(s_)
141  
    : any_segments_iter(s_)
142  
{
142  
{
143  
    front = s;
143  
    front = s;
144  
    fast_nseg = 1;
144  
    fast_nseg = 1;
145  
}
145  
}
146  

146  

147  
inline
147  
inline
148  
void
148  
void
149  
segment_encoded_iter::
149  
segment_encoded_iter::
150  
rewind() noexcept
150  
rewind() noexcept
151  
{
151  
{
152  
    at_end_ = false;
152  
    at_end_ = false;
153  
}
153  
}
154  

154  

155  
inline
155  
inline
156  
bool
156  
bool
157  
segment_encoded_iter::
157  
segment_encoded_iter::
158  
measure(
158  
measure(
159  
    std::size_t& n) noexcept
159  
    std::size_t& n) noexcept
160  
{
160  
{
161  
    if(at_end_)
161  
    if(at_end_)
162  
        return false;
162  
        return false;
163  
    n += detail::re_encoded_size_unsafe(
163  
    n += detail::re_encoded_size_unsafe(
164  
        s,
164  
        s,
165  
        encode_colons ?
165  
        encode_colons ?
166  
            nocolon_pchars :
166  
            nocolon_pchars :
167  
            pchars);
167  
            pchars);
168  
    at_end_ = true;
168  
    at_end_ = true;
169  
    return true;
169  
    return true;
170  
}
170  
}
171  

171  

172  
inline
172  
inline
173  
void
173  
void
174  
segment_encoded_iter::
174  
segment_encoded_iter::
175  
copy(
175  
copy(
176  
    char*& dest,
176  
    char*& dest,
177  
    char const* end) noexcept
177  
    char const* end) noexcept
178  
{
178  
{
179  
    detail::re_encode_unsafe(
179  
    detail::re_encode_unsafe(
180  
        dest,
180  
        dest,
181  
        end,
181  
        end,
182  
        s,
182  
        s,
183  
        encode_colons ?
183  
        encode_colons ?
184  
            nocolon_pchars :
184  
            nocolon_pchars :
185  
            pchars);
185  
            pchars);
186  
}
186  
}
187  

187  

188  
//------------------------------------------------
188  
//------------------------------------------------
189  
//
189  
//
190  
// segments_encoded_iter_base
190  
// segments_encoded_iter_base
191  
//
191  
//
192  
//------------------------------------------------
192  
//------------------------------------------------
193  

193  

194  
inline
194  
inline
195  
void
195  
void
196  
segments_encoded_iter_base::
196  
segments_encoded_iter_base::
197  
measure_impl(
197  
measure_impl(
198  
    std::size_t& n,
198  
    std::size_t& n,
199  
    core::string_view s,
199  
    core::string_view s,
200  
    bool encode_colons) noexcept
200  
    bool encode_colons) noexcept
201  
{
201  
{
202  
    n += detail::re_encoded_size_unsafe(
202  
    n += detail::re_encoded_size_unsafe(
203  
        s,
203  
        s,
204  
        encode_colons ?
204  
        encode_colons ?
205  
            nocolon_pchars :
205  
            nocolon_pchars :
206  
            pchars);
206  
            pchars);
207  
}
207  
}
208  

208  

209  
inline
209  
inline
210  
void
210  
void
211  
segments_encoded_iter_base::
211  
segments_encoded_iter_base::
212  
copy_impl(
212  
copy_impl(
213  
    char*& dest,
213  
    char*& dest,
214  
    char const* end,
214  
    char const* end,
215  
    core::string_view s,
215  
    core::string_view s,
216  
    bool encode_colons) noexcept
216  
    bool encode_colons) noexcept
217  
{
217  
{
218  
    detail::re_encode_unsafe(
218  
    detail::re_encode_unsafe(
219  
        dest,
219  
        dest,
220  
        end,
220  
        end,
221  
        s,
221  
        s,
222  
        encode_colons ?
222  
        encode_colons ?
223  
            nocolon_pchars :
223  
            nocolon_pchars :
224  
            pchars);
224  
            pchars);
225  
}
225  
}
226  

226  

227  
} // detail
227  
} // detail
228  
} // urls
228  
} // urls
229  
} // boost
229  
} // boost
230  

230  

231  
#endif
231  
#endif