TLA Line data 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 HIT 8214 : 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 8214 : if(it == end)
34 : {
35 : // end
36 14 : BOOST_URL_CONSTEXPR_RETURN_EC(
37 : error::mismatch);
38 : }
39 8200 : if(! digit_chars(*it))
40 : {
41 : // expected DIGIT
42 6483 : BOOST_URL_CONSTEXPR_RETURN_EC(
43 : error::mismatch);
44 : }
45 1717 : unsigned v = *it - '0';
46 1717 : ++it;
47 3228 : if( it == end ||
48 1511 : ! digit_chars(*it))
49 : {
50 1006 : return static_cast<
51 1006 : value_type>(v);
52 : }
53 711 : if(v == 0)
54 : {
55 : // leading '0'
56 11 : BOOST_URL_CONSTEXPR_RETURN_EC(
57 : error::invalid);
58 : }
59 700 : v = (10 * v) + *it - '0';
60 700 : ++it;
61 1380 : if( it == end ||
62 680 : ! digit_chars(*it))
63 : {
64 99 : return static_cast<
65 99 : value_type>(v);
66 : }
67 601 : if(v > 25)
68 : {
69 : // integer overflow
70 13 : BOOST_URL_CONSTEXPR_RETURN_EC(
71 : error::invalid);
72 : }
73 588 : v = (10 * v) + *it - '0';
74 588 : if(v > 255)
75 : {
76 : // integer overflow
77 17 : BOOST_URL_CONSTEXPR_RETURN_EC(
78 : error::invalid);
79 : }
80 571 : ++it;
81 1095 : if( it != end &&
82 524 : digit_chars(*it))
83 : {
84 : // integer overflow
85 7 : BOOST_URL_CONSTEXPR_RETURN_EC(
86 : error::invalid);
87 : }
88 564 : return static_cast<
89 564 : value_type>(v);
90 : }
91 :
92 : } // implementation_defined
93 : } // grammar
94 : } // urls
95 : } // boost
96 :
97 : #endif
|