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) 2023 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2023 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  
#ifndef BOOST_URL_GRAMMAR_IMPL_LITERAL_RULE_HPP
11  
#ifndef BOOST_URL_GRAMMAR_IMPL_LITERAL_RULE_HPP
12  
#define BOOST_URL_GRAMMAR_IMPL_LITERAL_RULE_HPP
12  
#define BOOST_URL_GRAMMAR_IMPL_LITERAL_RULE_HPP
13  

13  

14  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/detail/config.hpp>
15  
#include <boost/url/grammar/error.hpp>
15  
#include <boost/url/grammar/error.hpp>
16  
#include <boost/assert.hpp>
16  
#include <boost/assert.hpp>
17  
#include <cstring>
17  
#include <cstring>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  
namespace grammar {
21  
namespace grammar {
22  

22  

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

34  

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

67  

68  
} // grammar
68  
} // grammar
69  
} // urls
69  
} // urls
70  
} // boost
70  
} // boost
71  

71  

72  
#endif
72  
#endif