1  

1  

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

9  

10  

10  

11  
#include <boost/url/detail/config.hpp>
11  
#include <boost/url/detail/config.hpp>
12  
#include <boost/url/grammar/error.hpp>
12  
#include <boost/url/grammar/error.hpp>
13  
#include <boost/url/grammar/literal_rule.hpp>
13  
#include <boost/url/grammar/literal_rule.hpp>
14  
#include <boost/assert.hpp>
14  
#include <boost/assert.hpp>
15  
#include <cstring>
15  
#include <cstring>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace urls {
18  
namespace urls {
19  
namespace grammar {
19  
namespace grammar {
20  

20  

21  
auto
21  
auto
22  
literal_rule::
22  
literal_rule::
23  
parse(
23  
parse(
24  
    char const*& it,
24  
    char const*& it,
25  
    char const* end) const noexcept ->
25  
    char const* end) const noexcept ->
26  
        system::result<value_type>
26  
        system::result<value_type>
27  
{
27  
{
28  
    // Can't have a literal
28  
    // Can't have a literal
29  
    // with an empty string!
29  
    // with an empty string!
30  
    BOOST_ASSERT(n_ > 0);
30  
    BOOST_ASSERT(n_ > 0);
31  

31  

32  
    std::size_t n = end - it;
32  
    std::size_t n = end - it;
33  
    if(n >= n_)
33  
    if(n >= n_)
34  
    {
34  
    {
35  
        if(std::memcmp(
35  
        if(std::memcmp(
36  
            it, s_, n_) != 0)
36  
            it, s_, n_) != 0)
37  
        {
37  
        {
38  
            // non-match
38  
            // non-match
39  
            BOOST_URL_RETURN_EC(
39  
            BOOST_URL_RETURN_EC(
40  
                error::mismatch);
40  
                error::mismatch);
41  
        }
41  
        }
42  
        it += n_;
42  
        it += n_;
43  
        return core::string_view(
43  
        return core::string_view(
44  
            it - n_, it);
44  
            it - n_, it);
45  
    }
45  
    }
46  
    if(n > 0)
46  
    if(n > 0)
47  
    {
47  
    {
48  
        // short input
48  
        // short input
49  
        if(std::memcmp(
49  
        if(std::memcmp(
50  
            it, s_, n) != 0)
50  
            it, s_, n) != 0)
51  
        {
51  
        {
52  
            // non-match
52  
            // non-match
53  
            BOOST_URL_RETURN_EC(
53  
            BOOST_URL_RETURN_EC(
54  
                error::mismatch);
54  
                error::mismatch);
55  
        }
55  
        }
56  
        // prefix matches
56  
        // prefix matches
57  
        BOOST_URL_RETURN_EC(
57  
        BOOST_URL_RETURN_EC(
58  
            error::need_more);
58  
            error::need_more);
59  
    }
59  
    }
60  
    // end
60  
    // end
61  
    BOOST_URL_RETURN_EC(
61  
    BOOST_URL_RETURN_EC(
62  
        error::need_more);
62  
        error::need_more);
63  
}
63  
}
64  

64  

65  
} // grammar
65  
} // grammar
66  
} // urls
66  
} // urls
67  
} // boost
67  
} // boost
68  

68