net/addr/ip: Add InAnyAddr FromStr conversion
Change-Id: Id8803cc3368408ee8c2208eeb50d64dc467949bc
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/stdplus/net/addr/ip.hpp b/include/stdplus/net/addr/ip.hpp
index d42605c..afc7d45 100644
--- a/include/stdplus/net/addr/ip.hpp
+++ b/include/stdplus/net/addr/ip.hpp
@@ -268,6 +268,20 @@
}
};
+template <>
+struct FromStr<InAnyAddr>
+{
+ template <typename CharT>
+ constexpr InAnyAddr operator()(std::basic_string_view<CharT> sv) const
+ {
+ if (sv.find(':') == sv.npos)
+ {
+ return FromStr<In4Addr>{}(sv);
+ }
+ return FromStr<In6Addr>{}(sv);
+ }
+};
+
} // namespace stdplus
template <>
diff --git a/src/net/addr/ip.cpp b/src/net/addr/ip.cpp
index e411054..96b78cd 100644
--- a/src/net/addr/ip.cpp
+++ b/src/net/addr/ip.cpp
@@ -6,4 +6,5 @@
template char* ToStr<In4Addr>::operator()(char*, In4Addr) const noexcept;
template In6Addr FromStr<In6Addr>::operator()(std::string_view) const;
template char* ToStr<In6Addr>::operator()(char*, In6Addr) const noexcept;
+template InAnyAddr FromStr<InAnyAddr>::operator()(std::string_view) const;
} // namespace stdplus
diff --git a/test/net/addr/ip.cpp b/test/net/addr/ip.cpp
index 7296a1d..74e72b2 100644
--- a/test/net/addr/ip.cpp
+++ b/test/net/addr/ip.cpp
@@ -153,4 +153,14 @@
std::hash<InAnyAddr>{}(In6Addr{});
}
+TEST(FromStr, InAnyAddr)
+{
+ constexpr FromStr<InAnyAddr> fs;
+ EXPECT_EQ((In4Addr{}), fs("0.0.0.0"sv));
+ EXPECT_EQ((In6Addr{}), fs("::"sv));
+ EXPECT_EQ(
+ (In6Addr{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}),
+ fs("::ffff:192.168.0.1"sv));
+}
+
} // namespace stdplus