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) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 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  

11  

12  
#include <boost/url/detail/config.hpp>
12  
#include <boost/url/detail/config.hpp>
13  
#include <boost/url/parse_path.hpp>
13  
#include <boost/url/parse_path.hpp>
14  
#include <boost/url/error.hpp>
14  
#include <boost/url/error.hpp>
15  
#include "detail/path.hpp"
15  
#include "detail/path.hpp"
16  
#include <boost/url/grammar/parse.hpp>
16  
#include <boost/url/grammar/parse.hpp>
17  
#include "boost/url/rfc/detail/path_rules.hpp"
17  
#include "boost/url/rfc/detail/path_rules.hpp"
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  

21  

22  
system::result<segments_encoded_view>
22  
system::result<segments_encoded_view>
23  
parse_path(core::string_view s) noexcept
23  
parse_path(core::string_view s) noexcept
24  
{
24  
{
25  
    auto it = s.data();
25  
    auto it = s.data();
26  
    auto const end = it + s.size();
26  
    auto const end = it + s.size();
27  
    std::size_t dn = 0;
27  
    std::size_t dn = 0;
28  
    std::size_t nseg = 0;
28  
    std::size_t nseg = 0;
29  
    if( it != end &&
29  
    if( it != end &&
30  
            *it != '/')
30  
            *it != '/')
31  
        ++nseg;
31  
        ++nseg;
32  
    while(it != end)
32  
    while(it != end)
33  
    {
33  
    {
34  
        if(*it == '/')
34  
        if(*it == '/')
35  
        {
35  
        {
36  
            ++it;
36  
            ++it;
37  
            ++dn;
37  
            ++dn;
38  
            ++nseg;
38  
            ++nseg;
39  
            continue;
39  
            continue;
40  
        }
40  
        }
41  
        auto rv = grammar::parse(
41  
        auto rv = grammar::parse(
42  
            it, end, detail::segment_rule);
42  
            it, end, detail::segment_rule);
43  
        if(! rv)
43  
        if(! rv)
44  
            return rv.error();
44  
            return rv.error();
45  
        if(rv->empty())
45  
        if(rv->empty())
46  
        {
46  
        {
47  
            BOOST_URL_RETURN_EC(
47  
            BOOST_URL_RETURN_EC(
48  
                grammar::error::mismatch);
48  
                grammar::error::mismatch);
49  
        }
49  
        }
50  
        dn += rv->decoded_size();
50  
        dn += rv->decoded_size();
51  
    }
51  
    }
52  
    // adjust nseg
52  
    // adjust nseg
53  
    nseg = detail::path_segments(s, nseg);
53  
    nseg = detail::path_segments(s, nseg);
54  
    return segments_encoded_view(
54  
    return segments_encoded_view(
55  
        detail::path_ref(s, dn, nseg));
55  
        detail::path_ref(s, dn, nseg));
56  
}
56  
}
57  

57  

58  
} // urls
58  
} // urls
59  
} // boost
59  
} // boost
60  

60