GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 74 / 0 / 74
Functions: 100.0% 14 / 0 / 14
Branches: 100.0% 10 / 0 / 10

libs/url/src/ipv4_address.cpp
Line Branch Exec Source
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/url
8 //
9
10
11 #include <boost/url/detail/config.hpp>
12 #include <boost/url/ipv4_address.hpp>
13 #include <boost/url/detail/except.hpp>
14 #include <boost/url/grammar/parse.hpp>
15 #include <boost/url/rfc/ipv4_address_rule.hpp>
16 #include <cstring>
17
18 namespace boost {
19 namespace urls {
20
21 19 ipv4_address::
22 ipv4_address(
23 19 uint_type addr) noexcept
24 19 : addr_(addr)
25 {
26 19 }
27
28 145 ipv4_address::
29 ipv4_address(
30 145 bytes_type const& bytes) noexcept
31 {
32 145 addr_ =
33 145 (static_cast<unsigned long>(bytes[0]) << 24) |
34 145 (static_cast<unsigned long>(bytes[1]) << 16) |
35 145 (static_cast<unsigned long>(bytes[2]) << 8) |
36 145 (static_cast<unsigned long>(bytes[3]));
37 145 }
38
39 29 ipv4_address::
40 ipv4_address(
41 29 core::string_view s)
42 : ipv4_address(
43 29 parse_ipv4_address(s
44
1/1
✓ Branch 2 taken 28 times.
29 ).value(BOOST_URL_POS))
45 {
46 28 }
47
48 auto
49 79 ipv4_address::
50 to_bytes() const noexcept ->
51 bytes_type
52 {
53 bytes_type bytes;
54 79 bytes[0] = (addr_ >> 24) & 0xff;
55 79 bytes[1] = (addr_ >> 16) & 0xff;
56 79 bytes[2] = (addr_ >> 8) & 0xff;
57 79 bytes[3] = addr_ & 0xff;
58 79 return bytes;
59 }
60
61 auto
62 56 ipv4_address::
63 to_uint() const noexcept ->
64 uint_type
65 {
66 56 return addr_;
67 }
68
69 core::string_view
70 21 ipv4_address::
71 to_buffer(
72 char* dest,
73 std::size_t dest_size) const
74 {
75
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 20 times.
21 if(dest_size < max_str_len)
76 1 detail::throw_length_error();
77 20 auto n = print_impl(dest);
78 20 return core::string_view(dest, n);
79 }
80
81 bool
82 4 ipv4_address::
83 is_loopback() const noexcept
84 {
85 4 return (to_uint() & 0xFF000000) ==
86 4 0x7F000000;
87 }
88
89 bool
90 7 ipv4_address::
91 is_unspecified() const noexcept
92 {
93 7 return to_uint() == 0;
94 }
95
96 bool
97 4 ipv4_address::
98 is_multicast() const noexcept
99 {
100 4 return (to_uint() & 0xF0000000) ==
101 4 0xE0000000;
102 }
103
104 void
105 1 ipv4_address::
106 write_ostream(std::ostream& os) const
107 {
108 char buf[ipv4_address::max_str_len];
109
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 os << to_buffer(buf, sizeof(buf));
110 1 }
111
112 std::size_t
113 31 ipv4_address::
114 print_impl(
115 char* dest) const noexcept
116 {
117 31 auto const start = dest;
118 auto const write =
119 124 []( char*& dest,
120 unsigned char v)
121 {
122
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 87 times.
124 if(v >= 100)
123 {
124 37 *dest++ = '0' +
125 37 v / 100;
126 37 v %= 100;
127 37 *dest++ = '0' +
128 37 v / 10;
129 37 v %= 10;
130 }
131
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 84 times.
87 else if(v >= 10)
132 {
133 3 *dest++ = '0' +
134 3 v / 10;
135 3 v %= 10;
136 }
137 124 *dest++ = '0' + v;
138 124 };
139 31 auto const v = to_uint();
140 31 write(dest, (v >> 24) & 0xff);
141 31 *dest++ = '.';
142 31 write(dest, (v >> 16) & 0xff);
143 31 *dest++ = '.';
144 31 write(dest, (v >> 8) & 0xff);
145 31 *dest++ = '.';
146 31 write(dest, (v ) & 0xff);
147 31 return dest - start;
148 }
149
150 void
151 2 ipv4_address::
152 to_string_impl(
153 string_token::arg& t) const
154 {
155 char buf[max_str_len];
156 2 auto const n = print_impl(buf);
157
1/1
✓ Branch 1 taken 2 times.
2 char* dest = t.prepare(n);
158 2 std::memcpy(dest, buf, n);
159 2 }
160
161 //------------------------------------------------
162
163 auto
164 134 parse_ipv4_address(
165 core::string_view s) noexcept ->
166 system::result<ipv4_address>
167 {
168 134 return grammar::parse(
169 134 s, ipv4_address_rule);
170 }
171
172 } // urls
173 } // boost
174
175