include/boost/url/grammar/impl/literal_rule.hpp

100.0% Lines (16/16) 100.0% Functions (1/1)
include/boost/url/grammar/impl/literal_rule.hpp
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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_GRAMMAR_IMPL_LITERAL_RULE_HPP
12 #define BOOST_URL_GRAMMAR_IMPL_LITERAL_RULE_HPP
13
14 #include <boost/url/detail/config.hpp>
15 #include <boost/url/grammar/error.hpp>
16 #include <boost/assert.hpp>
17 #include <cstring>
18
19 namespace boost {
20 namespace urls {
21 namespace grammar {
22
23 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
24 auto
25 10 literal_rule::
26 parse(
27 char const*& it,
28 char const* end) const noexcept ->
29 system::result<value_type>
30 {
31 // Can't have a literal
32 // with an empty string!
33 10 BOOST_ASSERT(n_ > 0);
34
35 10 std::size_t n = end - it;
36 10 if(n >= n_)
37 {
38 4 if(std::memcmp(
39 4 it, s_, n_) != 0)
40 {
41 // non-match
42 1 BOOST_URL_CONSTEXPR_RETURN_EC(
43 error::mismatch);
44 }
45 3 it += n_;
46 3 return core::string_view(
47 3 it - n_, it);
48 }
49 6 if(n > 0)
50 {
51 // short input
52 5 if(std::memcmp(
53 5 it, s_, n) != 0)
54 {
55 // non-match
56 2 BOOST_URL_CONSTEXPR_RETURN_EC(
57 error::mismatch);
58 }
59 // prefix matches
60 3 BOOST_URL_CONSTEXPR_RETURN_EC(
61 error::need_more);
62 }
63 // end
64 1 BOOST_URL_CONSTEXPR_RETURN_EC(
65 error::need_more);
66 }
67
68 } // grammar
69 } // urls
70 } // boost
71
72 #endif
73