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_HIER_PART_RULE_HPP
 
12 +
#define BOOST_URL_RFC_DETAIL_IMPL_HIER_PART_RULE_HPP
 
13 +

 
14 +
#include <boost/url/detail/config.hpp>
 
15 +
#include <boost/url/rfc/detail/path_rules.hpp>
 
16 +
#include <boost/url/grammar/parse.hpp>
 
17 +

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

 
22 +
inline BOOST_URL_CXX20_CONSTEXPR
 
23 +
auto
 
24 +
hier_part_rule_t::
 
25 +
parse(
 
26 +
    char const*& it,
 
27 +
    char const* const end
 
28 +
        ) const noexcept ->
 
29 +
    system::result<value_type>
 
30 +
{
 
31 +
    value_type t;
 
32 +
    if(it == end)
 
33 +
    {
 
34 +
        return t;
 
35 +
    }
 
36 +
    if(end - it == 1)
 
37 +
    {
 
38 +
        if(*it == '/')
 
39 +
        {
 
40 +
            t.path = make_pct_string_view_unsafe(
 
41 +
                it, 1, 1);
 
42 +
            t.segment_count = 1;
 
43 +
            ++it;
 
44 +
            return t;
 
45 +
        }
 
46 +
        auto rv = grammar::parse(
 
47 +
            it, end, segment_rule);
 
48 +
        if(! rv)
 
49 +
            return rv.error();
 
50 +
        t.path = *rv;
 
51 +
        t.segment_count = !t.path.empty();
 
52 +
        return t;
 
53 +
    }
 
54 +
    if( it[0] == '/' &&
 
55 +
        it[1] == '/')
 
56 +
    {
 
57 +
        it += 2;
 
58 +
        auto rv = grammar::parse(
 
59 +
            it, end, authority_rule);
 
60 +
        if(! rv)
 
61 +
            return rv.error();
 
62 +
        t.authority = *rv;
 
63 +
        t.has_authority = true;
 
64 +
    }
 
65 +
    if(it == end || (
 
66 +
        t.has_authority && (
 
67 +
            *it != '/' &&
 
68 +
            *it != '?' &&
 
69 +
            *it != '#')))
 
70 +
    {
 
71 +
        return t;
 
72 +
    }
 
73 +
    auto const it0 = it;
 
74 +
    std::size_t dn = 0;
 
75 +
    if(*it != '/')
 
76 +
    {
 
77 +
        auto rv = grammar::parse(
 
78 +
            it, end, segment_rule);
 
79 +
        if(! rv)
 
80 +
            return rv.error();
 
81 +
        if(rv->empty())
 
82 +
            return t;
 
83 +
        dn += rv->decoded_size();
 
84 +
        ++t.segment_count;
 
85 +
    }
 
86 +
    while(it != end)
 
87 +
    {
 
88 +
        if(*it == '/')
 
89 +
        {
 
90 +
            ++dn;
 
91 +
            ++it;
 
92 +
            ++t.segment_count;
 
93 +
            continue;
 
94 +
        }
 
95 +
        auto rv = grammar::parse(
 
96 +
            it, end, segment_rule);
 
97 +
        if(! rv)
 
98 +
            return rv.error();
 
99 +
        if(rv->empty())
 
100 +
            break;
 
101 +
        dn += rv->decoded_size();
 
102 +
    }
 
103 +
    t.path = make_pct_string_view_unsafe(
 
104 +
        it0, it - it0, dn);
 
105 +
    return t;
 
106 +
}
 
107 +

 
108 +
} // detail
 
109 +
} // urls
 
110 +
} // boost
 
111 +

 
112 +

 
113 +
#endif