Skip to content

Commit f0c6a34

Browse files
IP: Fix formatting issue with IPSpace iteration and BWF.
1 parent 6def36a commit f0c6a34

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

code/include/swoc/IPRange.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ inline IPRange::IPRange(string_view const &text) {
18011801
inline IPRange::IPRange(IPRangeView const& view) {
18021802
if (AF_INET == view._family) {
18031803
*this = *view._raw._4;
1804-
} else if (AF_INET6 == _family) {
1804+
} else if (AF_INET6 == view._family) {
18051805
*this = *view._raw._6;
18061806
}
18071807
}

code/include/swoc/bwf_ip.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IP6Range const &r
5454

5555
BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPRange const &range);
5656

57+
BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPRangeView const &range);
58+
5759
BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPNet const &net);
5860

5961
BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IP4Net const &net);
@@ -108,4 +110,5 @@ operator<<(ostream &s, swoc::IPRange const &Range) {
108110
swoc::LocalBufferWriter<swoc::IP_STREAM_SIZE> w;
109111
return s << bwformat(w, swoc::bwf::Spec::DEFAULT, Range);
110112
}
113+
111114
} // namespace std

code/src/bw_ip_format.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ bwformat(BufferWriter &w, Spec const &spec, IPRange const &range) {
345345
range.is(AF_INET6) ? bwformat(w, spec, range.ip6()) : w.write("*-*"_tv);
346346
}
347347

348+
BufferWriter &
349+
bwformat(BufferWriter &w, Spec const &spec, IPRangeView const &rv) {
350+
return rv.is(AF_INET) ? bwformat(w, spec, rv.ip4()) :
351+
rv.is(AF_INET6) ? bwformat(w, spec, rv.ip6()) : w.write("*-*"_tv);
352+
}
353+
348354
BufferWriter &
349355
bwformat(BufferWriter &w, Spec const &spec, IP4Net const &net) {
350356
bwformat(w, spec, net.min());

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (CMAKE_COMPILER_IS_GNUCXX)
3030
target_compile_options(ex_flat_space PRIVATE -Wall -Wextra -Werror)
3131
endif()
3232

33-
add_executable(ex_diskstats ex_diskstats.cc ../image/include/swoc/IntrusiveDList.h)
33+
add_executable(ex_diskstats ex_diskstats.cc ../code/include/swoc/IntrusiveDList.h)
3434
target_link_libraries(ex_diskstats PUBLIC libswoc-static)
3535
if (CMAKE_COMPILER_IS_GNUCXX)
3636
target_compile_options(ex_diskstats PRIVATE -Wall -Wextra -Werror)

unit_tests/test_ip.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ TEST_CASE("IP ranges and networks", "[libswoc][ip][net][range]") {
875875
CHECK(*r5_net == swoc::IPNet{a, m});
876876
++r5_net;
877877
}
878+
878879
}
879880

880881
TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") {
@@ -1083,6 +1084,31 @@ TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") {
10831084
auto [ r, p ] = *space.find(IPAddr{"2001:4997:58:400::1E"});
10841085
REQUIRE(r.empty());
10851086
}
1087+
1088+
space.clear();
1089+
// Test a mix
1090+
unsigned idx = 0;
1091+
std::array<TextView, 6> mix_r { "1.1.1.1-1.1.1.111", "2.2.2.2-2.2.2.222", "3.3.3.3-3.255.255.255",
1092+
"1:2:3:4:5:6:7:8-1:2:3:4:5:6:7:ffff",
1093+
"11:2:3:4:5:6:7:8-11:2:3:4:5:6:7:ffff",
1094+
"111:2:3:4:5:6:7:8-111:2:3:4:5:6:7:ffff"
1095+
};
1096+
for ( auto && r : mix_r) {
1097+
space.mark(IPRange(r), idx);
1098+
++idx;
1099+
}
1100+
1101+
idx = 0;
1102+
std::string s;
1103+
for (auto [r,p] : space) {
1104+
REQUIRE(!r.empty());
1105+
REQUIRE(p == idx);
1106+
swoc::LocalBufferWriter<64> dbg;
1107+
bwformat(dbg, swoc::bwf::Spec::DEFAULT, r);
1108+
bwprint(s, "{}", r);
1109+
REQUIRE(s == mix_r[idx]);
1110+
++idx;
1111+
}
10861112
}
10871113

10881114
TEST_CASE("IPSpace bitset", "[libswoc][ipspace][bitset]") {

0 commit comments

Comments
 (0)