src/corosio/src/ip_address.cpp

93.8% Lines (15/16) 100.0% List of functions (3/3) 91.7% Branches (11/12)
ip_address.cpp
f(x) Functions (3)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Steve Gerbino
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/cppalliance/corosio
9 //
10
11 #include <boost/corosio/ip_address.hpp>
12
13 #include <ostream>
14 #include <stdexcept>
15
16 namespace boost::corosio {
17
18 std::string_view
19 3x ip_address::to_buffer(char* dest, std::size_t dest_size) const
20 {
21 // Uniform capacity requirement: a v4 value in an ip_address
22 // still requires the family-independent max_str_len.
23
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3x if (dest_size < max_str_len)
24
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1x throw std::length_error("buffer too small for IP address");
25
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2x return is_v4() ? v4_.to_buffer(dest, dest_size)
26 1x : v6_.to_buffer(dest, dest_size);
27 ✗ }
28
29 std::ostream&
30 2x operator<<(std::ostream& os, ip_address const& addr)
31 {
32
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2x return addr.is_v4() ? os << addr.v4_ : os << addr.v6_;
33 }
34
35 capy::io_result<ip_address>
36 45x make_ip_address(std::string_view s) noexcept
37 {
38
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 32 times.
45x if (auto [ec, v4] = make_ipv4_address(s); !ec)
39 26x return {std::error_code{}, ip_address(v4)};
40 32x auto [ec, v6] = make_ipv6_address(s);
41
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 21 times.
32x if (ec)
42 11x return {ec, ip_address{}};
43 42x return {std::error_code{}, ip_address(v6)};
44 45x }
45
46 } // namespace boost::corosio
47