net/addr: Support std::format
Change-Id: Ib3e7e7ede426db31322aa114eb750fe298168d9a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/stdplus/net/addr/ether.hpp b/include/stdplus/net/addr/ether.hpp
index ee72572..2d3851f 100644
--- a/include/stdplus/net/addr/ether.hpp
+++ b/include/stdplus/net/addr/ether.hpp
@@ -117,3 +117,14 @@
struct fmt::formatter<stdplus::EtherAddr, CharT> :
stdplus::Format<stdplus::ToStr<stdplus::EtherAddr>, CharT>
{};
+
+namespace std
+{
+template <typename T, typename CharT>
+struct formatter;
+
+template <typename CharT>
+struct formatter<stdplus::EtherAddr, CharT> :
+ stdplus::Format<stdplus::ToStr<stdplus::EtherAddr>, CharT>
+{};
+} // namespace std
diff --git a/include/stdplus/net/addr/ip.hpp b/include/stdplus/net/addr/ip.hpp
index 1b0b2df..fdef5c3 100644
--- a/include/stdplus/net/addr/ip.hpp
+++ b/include/stdplus/net/addr/ip.hpp
@@ -602,3 +602,13 @@
struct fmt::formatter<Addr, CharT> :
stdplus::Format<stdplus::ToStr<Addr>, CharT>
{};
+
+namespace std
+{
+template <typename T, typename CharT>
+struct formatter;
+
+template <stdplus::InAddr Addr, typename CharT>
+struct formatter<Addr, CharT> : stdplus::Format<stdplus::ToStr<Addr>, CharT>
+{};
+} // namespace std
diff --git a/include/stdplus/net/addr/subnet.hpp b/include/stdplus/net/addr/subnet.hpp
index 2a91ed8..93fc226 100644
--- a/include/stdplus/net/addr/subnet.hpp
+++ b/include/stdplus/net/addr/subnet.hpp
@@ -365,3 +365,13 @@
return stdplus::hashMulti(addr.getAddr(), addr.getPfx());
}
};
+
+namespace std
+{
+template <typename T, typename CharT>
+struct formatter;
+
+template <stdplus::Subnet Sub, typename CharT>
+struct formatter<Sub, CharT> : stdplus::Format<stdplus::ToStr<Sub>, CharT>
+{};
+} // namespace std
diff --git a/test/net/addr/ether.cpp b/test/net/addr/ether.cpp
index bff7248..cf23223 100644
--- a/test/net/addr/ether.cpp
+++ b/test/net/addr/ether.cpp
@@ -2,6 +2,8 @@
#include <stdplus/net/addr/ether.hpp>
+#include <format>
+
#include <gtest/gtest.h>
namespace stdplus
@@ -44,6 +46,9 @@
EXPECT_EQ(
"a 01:02:03:04:05:67 b",
fmt::format("a {} b", EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
+ EXPECT_EQ(
+ "a 01:02:03:04:05:67 b",
+ std::format("a {} b", EtherAddr{0x01, 0x02, 0x03, 0x04, 0x05, 0x67}));
}
TEST(FromStr, EtherAddr)
diff --git a/test/net/addr/ip.cpp b/test/net/addr/ip.cpp
index 2ce2580..5fef26a 100644
--- a/test/net/addr/ip.cpp
+++ b/test/net/addr/ip.cpp
@@ -3,6 +3,7 @@
#include <stdplus/net/addr/ip.hpp>
#include <stdplus/numeric/endian.hpp>
+#include <format>
#include <string_view>
#include <gtest/gtest.h>
@@ -48,6 +49,7 @@
EXPECT_EQ("1.15.3.4", tsh(In4Addr{1, 15, 3, 4}));
EXPECT_EQ("0.0.0.0", tsh(In4Addr{}));
EXPECT_EQ("a 1.15.3.4 b", fmt::format("a {} b", In4Addr{1, 15, 3, 4}));
+ EXPECT_EQ("a 1.15.3.4 b", std::format("a {} b", In4Addr{1, 15, 3, 4}));
}
TEST(EqualOperator, In6Addr)
@@ -157,6 +159,7 @@
0xff, 0xff, 192, 168, 0, 1}));
EXPECT_EQ("a ff00:: b", fmt::format("a {} b", In6Addr{0xff}));
+ EXPECT_EQ("a ff00:: b", std::format("a {} b", In6Addr{0xff}));
}
TEST(EqualOperator, InAnyAddr)
@@ -221,6 +224,7 @@
EXPECT_EQ("ff::", tsh(In6Addr{0, 0xff}));
EXPECT_EQ("a ff00:: b", fmt::format("a {} b", InAnyAddr{In6Addr{0xff}}));
+ EXPECT_EQ("a ff00:: b", std::format("a {} b", InAnyAddr{In6Addr{0xff}}));
EXPECT_NE("0.0.0.0"_ip, std::optional<InAnyAddr>());
EXPECT_EQ("0.0.0.0"_ip, std::optional<InAnyAddr>(In4Addr{}));
}
diff --git a/test/net/addr/subnet.cpp b/test/net/addr/subnet.cpp
index 31bad90..8973bdb 100644
--- a/test/net/addr/subnet.cpp
+++ b/test/net/addr/subnet.cpp
@@ -2,6 +2,7 @@
#include <stdplus/net/addr/subnet.hpp>
+#include <format>
#include <string_view>
#include <gtest/gtest.h>
@@ -74,6 +75,8 @@
EXPECT_EQ("255.0.255.255/28", tsh(Subnet4(In4Addr{255, 0, 255, 255}, 28)));
EXPECT_EQ("a 1.2.3.4/32 b",
fmt::format("a {} b", Subnet4(In4Addr{1, 2, 3, 4}, 32)));
+ EXPECT_EQ("a 1.2.3.4/32 b",
+ std::format("a {} b", Subnet4(In4Addr{1, 2, 3, 4}, 32)));
}
TEST(Subnet6, Basic)
@@ -146,6 +149,8 @@
EXPECT_EQ("ff00::/128", tsh(Subnet6(In6Addr{0xff}, 128)));
EXPECT_EQ("a 102:304::/32 b",
fmt::format("a {} b", Subnet6(In6Addr{1, 2, 3, 4}, 32)));
+ EXPECT_EQ("a 102:304::/32 b",
+ std::format("a {} b", Subnet6(In6Addr{1, 2, 3, 4}, 32)));
}
TEST(SubnetAny, Basic)
@@ -224,8 +229,12 @@
EXPECT_EQ("ff00::/128", tsh(SubnetAny(In6Addr{0xff}, 128)));
EXPECT_EQ("a 102:304::/32 b",
fmt::format("a {} b", SubnetAny(In6Addr{1, 2, 3, 4}, 32)));
+ EXPECT_EQ("a 102:304::/32 b",
+ std::format("a {} b", SubnetAny(In6Addr{1, 2, 3, 4}, 32)));
EXPECT_EQ("a 1.2.3.4/32 b",
fmt::format("a {} b", SubnetAny(In4Addr{1, 2, 3, 4}, 32)));
+ EXPECT_EQ("a 1.2.3.4/32 b",
+ std::format("a {} b", SubnetAny(In4Addr{1, 2, 3, 4}, 32)));
}
TEST(Ops, MaskToPfx)