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 +
inline BOOST_URL_CXX20_CONSTEXPR
 
25 +
auto
 
26 +
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 +
    if(it == end)
 
34 +
    {
 
35 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
36 +
            error::mismatch);
 
37 +
    }
 
38 +
    if(! digit_chars(*it))
 
39 +
    {
 
40 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
41 +
            error::mismatch);
 
42 +
    }
 
43 +
    unsigned v = *it - '0';
 
44 +
    ++it;
 
45 +
    if( it == end ||
 
46 +
        ! digit_chars(*it))
 
47 +
    {
 
48 +
        return static_cast<
 
49 +
            value_type>(v);
 
50 +
    }
 
51 +
    if(v == 0)
 
52 +
    {
 
53 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
54 +
            error::invalid);
 
55 +
    }
 
56 +
    v = (10 * v) + *it - '0';
 
57 +
    ++it;
 
58 +
    if( it == end ||
 
59 +
        ! digit_chars(*it))
 
60 +
    {
 
61 +
        return static_cast<
 
62 +
            value_type>(v);
 
63 +
    }
 
64 +
    if(v > 25)
 
65 +
    {
 
66 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
67 +
            error::invalid);
 
68 +
    }
 
69 +
    v = (10 * v) + *it - '0';
 
70 +
    if(v > 255)
 
71 +
    {
 
72 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
73 +
            error::invalid);
 
74 +
    }
 
75 +
    ++it;
 
76 +
    if( it != end &&
 
77 +
        digit_chars(*it))
 
78 +
    {
 
79 +
        BOOST_URL_CONSTEXPR_RETURN_EC(
 
80 +
            error::invalid);
 
81 +
    }
 
82 +
    return static_cast<
 
83 +
        value_type>(v);
 
84 +
}
 
85 +

 
86 +
} // implementation_defined
 
87 +
} // grammar
 
88 +
} // urls
 
89 +
} // boost
 
90 +

 
91 +
#endif