include/boost/url/rfc/detail/impl/port_rule.hpp
97.8% Lines (44/45)
100.0% Functions (2/2)
| 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_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 | BOOST_URL_CXX20_CONSTEXPR_OR_INLINE | ||
| 24 | auto | ||
| 25 | 395 | port_rule:: | |
| 26 | parse( | ||
| 27 | char const*& it, | ||
| 28 | char const* end) const noexcept -> | ||
| 29 | system::result<value_type> | ||
| 30 | { | ||
| 31 | 395 | value_type t; | |
| 32 | 395 | auto const start = it; | |
| 33 | 395 | while( | |
| 34 | 523 | it != end && | |
| 35 | 461 | *it == '0') | |
| 36 | { | ||
| 37 | 128 | ++it; | |
| 38 | } | ||
| 39 | |||
| 40 | 395 | if (it != end) | |
| 41 | { | ||
| 42 | grammar::unsigned_rule<std::uint16_t> r; | ||
| 43 | 333 | auto it0 = it; | |
| 44 | 333 | auto rv = r.parse(it, end); | |
| 45 | 333 | if (rv) | |
| 46 | { | ||
| 47 | // number < max uint16_t | ||
| 48 | 279 | t.str = core::string_view(start, it); | |
| 49 | 279 | t.has_number = true; | |
| 50 | 279 | t.number = *rv; | |
| 51 | 300 | return t; | |
| 52 | } | ||
| 53 | 54 | it = it0; | |
| 54 | 54 | if (grammar::digit_chars(*it)) | |
| 55 | { | ||
| 56 | // number > max uint16_t | ||
| 57 | 21 | while ( | |
| 58 | 366 | it != end && | |
| 59 | 177 | grammar::digit_chars(*it)) | |
| 60 | { | ||
| 61 | 168 | ++it; | |
| 62 | } | ||
| 63 | 21 | t.str = core::string_view(start, it); | |
| 64 | 21 | t.has_number = true; | |
| 65 | 21 | t.number = 0; | |
| 66 | 21 | return t; | |
| 67 | } | ||
| 68 | } | ||
| 69 | // no digits | ||
| 70 | 95 | t.str = core::string_view(start, it); | |
| 71 | 95 | t.has_number = it != end; | |
| 72 | 95 | t.number = 0; | |
| 73 | 95 | return t; | |
| 74 | } | ||
| 75 | |||
| 76 | BOOST_URL_CXX20_CONSTEXPR_OR_INLINE | ||
| 77 | auto | ||
| 78 | 2043 | port_part_rule_t:: | |
| 79 | parse( | ||
| 80 | char const*& it, | ||
| 81 | char const* end) const noexcept -> | ||
| 82 | system::result<value_type> | ||
| 83 | { | ||
| 84 | 2043 | value_type t; | |
| 85 | 2043 | if( it == end || | |
| 86 | 1414 | *it != ':') | |
| 87 | { | ||
| 88 | 1738 | t.has_port = false; | |
| 89 | 1738 | return t; | |
| 90 | } | ||
| 91 | 305 | ++it; | |
| 92 | 305 | auto rv = grammar::parse( | |
| 93 | 305 | it, end, port_rule{}); | |
| 94 | 305 | if(! rv) | |
| 95 | ✗ | return rv.error(); | |
| 96 | 305 | t.has_port = true; | |
| 97 | 305 | t.port = rv->str; | |
| 98 | 305 | t.has_number = rv->has_number; | |
| 99 | 305 | t.port_number = rv->number; | |
| 100 | 305 | return t; | |
| 101 | } | ||
| 102 | |||
| 103 | } // detail | ||
| 104 | } // urls | ||
| 105 | } // boost | ||
| 106 | |||
| 107 | |||
| 108 | #endif | ||
| 109 |