1 +
//
 
2 +
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
 
3 +
// Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
 
4 +
//
 
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)
 
7 +
//
 
8 +
// Official repository: https://github.com/boostorg/url
 
9 +
//
 
10 +

 
11 +
#ifndef BOOST_URL_DETAIL_IMPL_ANY_PARAMS_ITER_HPP
 
12 +
#define BOOST_URL_DETAIL_IMPL_ANY_PARAMS_ITER_HPP
 
13 +

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

 
18 +
namespace boost {
 
19 +
namespace urls {
 
20 +
namespace detail {
 
21 +

 
22 +
//------------------------------------------------
 
23 +
//
 
24 +
// query_string_iter
 
25 +
//
 
26 +
//------------------------------------------------
 
27 +

 
28 +
inline
 
29 +
query_string_iter::
 
30 +
query_string_iter(
 
31 +
    core::string_view s,
 
32 +
    bool ne) noexcept
 
33 +
    : any_params_iter(
 
34 +
        s.empty() && ! ne, s)
 
35 +
{
 
36 +
    rewind();
 
37 +
}
 
38 +

 
39 +
inline
 
40 +
void
 
41 +
query_string_iter::
 
42 +
rewind() noexcept
 
43 +
{
 
44 +
    if(empty)
 
45 +
    {
 
46 +
        at_end_ = true;
 
47 +
        return;
 
48 +
    }
 
49 +
    p_ = s0.begin();
 
50 +
    if(! s0.empty())
 
51 +
    {
 
52 +
        auto pos =
 
53 +
            s0.find_first_of('&');
 
54 +
        if(pos != core::string_view::npos)
 
55 +
            n_ = pos;
 
56 +
        else
 
57 +
            n_ = s0.size();
 
58 +
    }
 
59 +
    else
 
60 +
    {
 
61 +
        n_ = 0;
 
62 +
    }
 
63 +
    at_end_ = false;
 
64 +
}
 
65 +

 
66 +
inline
 
67 +
bool
 
68 +
query_string_iter::
 
69 +
measure(
 
70 +
    std::size_t& n) noexcept
 
71 +
{
 
72 +
    if(at_end_)
 
73 +
        return false;
 
74 +
    // When interacting with the query as
 
75 +
    // an intact string, we do not treat
 
76 +
    // the plus sign as an encoded space.
 
77 +
    encoding_opts opt;
 
78 +
    opt.space_as_plus = false;
 
79 +
    n += encoded_size(
 
80 +
        core::string_view(p_, n_),
 
81 +
        query_chars,
 
82 +
        opt);
 
83 +
    increment();
 
84 +
    return true;
 
85 +
}
 
86 +

 
87 +
inline
 
88 +
void
 
89 +
query_string_iter::
 
90 +
copy(
 
91 +
    char*& dest,
 
92 +
    char const* end) noexcept
 
93 +
{
 
94 +
    BOOST_ASSERT(! at_end_);
 
95 +
    // When interacting with the query as
 
96 +
    // an intact string, we do not treat
 
97 +
    // the plus sign as an encoded space.
 
98 +
    encoding_opts opt;
 
99 +
    opt.space_as_plus = false;
 
100 +
    dest += encode_unsafe(
 
101 +
        dest,
 
102 +
        end - dest,
 
103 +
        core::string_view(p_, n_),
 
104 +
        query_chars,
 
105 +
        opt);
 
106 +
    increment();
 
107 +
}
 
108 +

 
109 +
inline
 
110 +
void
 
111 +
query_string_iter::
 
112 +
increment() noexcept
 
113 +
{
 
114 +
    p_ += n_;
 
115 +
    if(p_ == s0.end())
 
116 +
    {
 
117 +
        at_end_ = true;
 
118 +
        return;
 
119 +
    }
 
120 +
    ++p_;
 
121 +
    core::string_view s(p_, s0.end() - p_);
 
122 +
    auto pos = s.find_first_of('&');
 
123 +
    if(pos != core::string_view::npos)
 
124 +
        n_ = pos;
 
125 +
    else
 
126 +
        n_ = s.size();
 
127 +
}
 
128 +

 
129 +
} // detail
 
130 +
} // urls
 
131 +
} // boost
 
132 +

 
133 +
#endif