1 +
//
 
2 +
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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_RFC_DETAIL_IMPL_RELATIVE_PART_RULE_HPP
 
12 +
#define BOOST_URL_RFC_DETAIL_IMPL_RELATIVE_PART_RULE_HPP
 
13 +

 
14 +
#include <boost/url/detail/config.hpp>
 
15 +
#include <boost/url/rfc/detail/path_rules.hpp>
 
16 +
#include <boost/url/rfc/pct_encoded_rule.hpp>
 
17 +
#include <boost/url/rfc/pchars.hpp>
 
18 +
#include <boost/url/grammar/error.hpp>
 
19 +
#include <boost/url/grammar/parse.hpp>
 
20 +

 
21 +
namespace boost {
 
22 +
namespace urls {
 
23 +
namespace detail {
 
24 +

 
25 +
inline BOOST_URL_CXX20_CONSTEXPR
 
26 +
auto
 
27 +
relative_part_rule_t::
 
28 +
parse(
 
29 +
    char const*& it,
 
30 +
    char const* const end
 
31 +
        ) const noexcept ->
 
32 +
    system::result<value_type>
 
33 +
{
 
34 +
    constexpr auto pchars_nc = pchars - ':';
 
35 +

 
36 +
    value_type t;
 
37 +
    if(it == end)
 
38 +
    {
 
39 +
        return t;
 
40 +
    }
 
41 +
    if(end - it == 1)
 
42 +
    {
 
43 +
        if(*it == '/')
 
44 +
        {
 
45 +
            t.path = make_pct_string_view_unsafe(
 
46 +
                it, 1, 1);
 
47 +
            t.segment_count = 1;
 
48 +
            ++it;
 
49 +
            return t;
 
50 +
        }
 
51 +
        if(*it != ':')
 
52 +
        {
 
53 +
            auto rv = grammar::parse(
 
54 +
                it, end, segment_rule);
 
55 +
            if(! rv)
 
56 +
                return rv.error();
 
57 +
            if(! rv->empty())
 
58 +
            {
 
59 +
                t.path = *rv;
 
60 +
                t.segment_count = 1;
 
61 +
            }
 
62 +
        }
 
63 +
        return t;
 
64 +
    }
 
65 +
    if( it[0] == '/' &&
 
66 +
        it[1] == '/')
 
67 +
    {
 
68 +
        it += 2;
 
69 +
        auto rv = grammar::parse(
 
70 +
            it, end, authority_rule);
 
71 +
        if(! rv)
 
72 +
            return rv.error();
 
73 +
        t.authority = *rv;
 
74 +
        t.has_authority = true;
 
75 +
    }
 
76 +
    if(it == end)
 
77 +
    {
 
78 +
        return t;
 
79 +
    }
 
80 +
    auto const it0 = it;
 
81 +
    std::size_t dn = 0;
 
82 +
    if(*it != '/')
 
83 +
    {
 
84 +
        auto rv = grammar::parse(it, end,
 
85 +
            pct_encoded_rule(pchars_nc));
 
86 +
        if(! rv)
 
87 +
            return rv.error();
 
88 +
        if(rv->empty())
 
89 +
            return t;
 
90 +
        dn += rv->decoded_size();
 
91 +
        ++t.segment_count;
 
92 +
        if( it != end &&
 
93 +
            *it == ':')
 
94 +
        {
 
95 +
            BOOST_URL_CONSTEXPR_RETURN_EC(
 
96 +
                grammar::error::mismatch);
 
97 +
        }
 
98 +
    }
 
99 +
    while(it != end)
 
100 +
    {
 
101 +
        if(*it == '/')
 
102 +
        {
 
103 +
            ++dn;
 
104 +
            ++it;
 
105 +
            ++t.segment_count;
 
106 +
            continue;
 
107 +
        }
 
108 +
        auto rv = grammar::parse(
 
109 +
            it, end, segment_rule);
 
110 +
        if(! rv)
 
111 +
            return rv.error();
 
112 +
        if(rv->empty())
 
113 +
            break;
 
114 +
        dn += rv->decoded_size();
 
115 +
    }
 
116 +
    t.path = make_pct_string_view_unsafe(
 
117 +
        it0, it - it0, dn);
 
118 +
    return t;
 
119 +
}
 
120 +

 
121 +
} // detail
 
122 +
} // urls
 
123 +
} // boost
 
124 +

 
125 +

 
126 +
#endif