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_RFC_DETAIL_IMPL_PORT_RULE_HPP
 
12 +
#define BOOST_URL_RFC_DETAIL_IMPL_PORT_RULE_HPP
 
13 +

 
14 +
#include <boost/url/detail/config.hpp>
 
15 +
#include <boost/url/grammar/digit_chars.hpp>
 
16 +
#include <boost/url/grammar/parse.hpp>
 
17 +
#include <boost/url/grammar/unsigned_rule.hpp>
 
18 +

 
19 +
namespace boost {
 
20 +
namespace urls {
 
21 +
namespace detail {
 
22 +

 
23 +
inline BOOST_URL_CXX20_CONSTEXPR
 
24 +
auto
 
25 +
port_rule::
 
26 +
parse(
 
27 +
    char const*& it,
 
28 +
    char const* end) const noexcept ->
 
29 +
        system::result<value_type>
 
30 +
{
 
31 +
    value_type t;
 
32 +
    auto const start = it;
 
33 +
    while(
 
34 +
        it != end &&
 
35 +
        *it == '0')
 
36 +
    {
 
37 +
        ++it;
 
38 +
    }
 
39 +

 
40 +
    if (it != end)
 
41 +
    {
 
42 +
        grammar::unsigned_rule<std::uint16_t> r;
 
43 +
        auto it0 = it;
 
44 +
        auto rv = r.parse(it, end);
 
45 +
        if (rv)
 
46 +
        {
 
47 +
            t.str = core::string_view(start, it);
 
48 +
            t.has_number = true;
 
49 +
            t.number = *rv;
 
50 +
            return t;
 
51 +
        }
 
52 +
        it = it0;
 
53 +
        if (grammar::digit_chars(*it))
 
54 +
        {
 
55 +
            while (
 
56 +
                it != end &&
 
57 +
                grammar::digit_chars(*it))
 
58 +
            {
 
59 +
                ++it;
 
60 +
            }
 
61 +
            t.str = core::string_view(start, it);
 
62 +
            t.has_number = true;
 
63 +
            t.number = 0;
 
64 +
            return t;
 
65 +
        }
 
66 +
    }
 
67 +
    t.str = core::string_view(start, it);
 
68 +
    t.has_number = it != end;
 
69 +
    t.number = 0;
 
70 +
    return t;
 
71 +
}
 
72 +

 
73 +
inline BOOST_URL_CXX20_CONSTEXPR
 
74 +
auto
 
75 +
port_part_rule_t::
 
76 +
parse(
 
77 +
    char const*& it,
 
78 +
    char const* end) const noexcept ->
 
79 +
        system::result<value_type>
 
80 +
{
 
81 +
    value_type t;
 
82 +
    if( it == end ||
 
83 +
        *it != ':')
 
84 +
    {
 
85 +
        t.has_port = false;
 
86 +
        return t;
 
87 +
    }
 
88 +
    ++it;
 
89 +
    auto rv = grammar::parse(
 
90 +
        it, end, port_rule{});
 
91 +
    if(! rv)
 
92 +
        return rv.error();
 
93 +
    t.has_port = true;
 
94 +
    t.port = rv->str;
 
95 +
    t.has_number = rv->has_number;
 
96 +
    t.port_number = rv->number;
 
97 +
    return t;
 
98 +
}
 
99 +

 
100 +
} // detail
 
101 +
} // urls
 
102 +
} // boost
 
103 +

 
104 +

 
105 +
#endif