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

100.0% Lines (30/30) 100.0% Functions (1/1)
include/boost/url/grammar/impl/dec_octet_rule.hpp
Line TLA Hits Source Code
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_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
12 #define BOOST_URL_GRAMMAR_IMPL_DEC_OCTET_RULE_HPP
13
14 #include <boost/url/detail/config.hpp>
15 #include <boost/url/grammar/charset.hpp>
16 #include <boost/url/grammar/digit_chars.hpp>
17 #include <boost/url/grammar/error.hpp>
18
19 namespace boost {
20 namespace urls {
21 namespace grammar {
22 namespace implementation_defined {
23
24 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
25 auto
26 2478 dec_octet_rule_t::
27 parse(
28 char const*& it,
29 char const* const end
30 ) const noexcept ->
31 system::result<value_type>
32 {
33 2478 if(it == end)
34 {
35 // end
36 14 BOOST_URL_CONSTEXPR_RETURN_EC(
37 error::mismatch);
38 }
39 2464 if(! digit_chars(*it))
40 {
41 // expected DIGIT
42 1818 BOOST_URL_CONSTEXPR_RETURN_EC(
43 error::mismatch);
44 }
45 646 unsigned v = *it - '0';
46 646 ++it;
47 1223 if( it == end ||
48 577 ! digit_chars(*it))
49 {
50 433 return static_cast<
51 433 value_type>(v);
52 }
53 213 if(v == 0)
54 {
55 // leading '0'
56 11 BOOST_URL_CONSTEXPR_RETURN_EC(
57 error::invalid);
58 }
59 202 v = (10 * v) + *it - '0';
60 202 ++it;
61 400 if( it == end ||
62 198 ! digit_chars(*it))
63 {
64 23 return static_cast<
65 23 value_type>(v);
66 }
67 179 if(v > 25)
68 {
69 // integer overflow
70 13 BOOST_URL_CONSTEXPR_RETURN_EC(
71 error::invalid);
72 }
73 166 v = (10 * v) + *it - '0';
74 166 if(v > 255)
75 {
76 // integer overflow
77 17 BOOST_URL_CONSTEXPR_RETURN_EC(
78 error::invalid);
79 }
80 149 ++it;
81 286 if( it != end &&
82 137 digit_chars(*it))
83 {
84 // integer overflow
85 7 BOOST_URL_CONSTEXPR_RETURN_EC(
86 error::invalid);
87 }
88 142 return static_cast<
89 142 value_type>(v);
90 }
91
92 } // implementation_defined
93 } // grammar
94 } // urls
95 } // boost
96
97 #endif
98