result->append(addr.str());
}
+bool IPAddress::validate(StringPiece ip) {
+ return IPAddressV4::validate(ip) || IPAddressV6::validate(ip);
+}
+
// public static
IPAddressV4 IPAddress::createIPv4(const IPAddress& addr) {
if (addr.isV4()) {
*/
class IPAddress : boost::totally_ordered<IPAddress> {
public:
+ // returns true iff the input string can be parsed as an ip-address
+ static bool validate(StringPiece ip);
+
// return the V4 representation of the address, converting it from V6 to V4 if
// needed. Note that this will throw an IPAddressFormatException if the V6
// address is not IPv4Mapped.
*/
class IPAddressV4 : boost::totally_ordered<IPAddressV4> {
public:
+ // returns true iff the input string can be parsed as an ipv4-address
static bool validate(StringPiece ip);
// create an IPAddressV4 instance from a uint32_t (network byte order)
static constexpr size_t kToFullyQualifiedSize =
8 /*words*/ * 4 /*hex chars per word*/ + 7 /*separators*/;
+ // returns true iff the input string can be parsed as an ipv6-address
static bool validate(StringPiece ip);
/**
IPAddressV6::validate("2620:0000:1cfe:face:b00c:0000:127.127.127.127"));
}
+TEST(IPAddress, validate) {
+ EXPECT_TRUE(IPAddress::validate("0.0.0.0"));
+ EXPECT_TRUE(IPAddress::validate("::"));
+ EXPECT_FALSE(IPAddress::validate("asdf"));
+}
+
// Test addresses constructed using a in[6]_addr value
TEST_P(IPAddressTest, CtorAddress) {
AddressData param = GetParam();