added implementation of is_valid_ip_address()

This commit is contained in:
Ariel Manzur 2016-10-20 09:58:00 -03:00
parent 672225b710
commit a3131a6b5b
3 changed files with 117 additions and 25 deletions

View file

@ -846,19 +846,58 @@ bool test_28() {
bool test_29() {
bool error = false;
bool state = true;
bool success = false;
IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
printf("ip0 is %ls\n", String(ip0).c_str());
OS::get_singleton()->print("ip0 is %ls\n", String(ip0).c_str());
IP_Address ip(0x0123, 0x4567, 0x89ab, 0xcdef, IP_Address::TYPE_IPV6);
printf("ip6 is %ls\n", String(ip).c_str());
OS::get_singleton()->print("ip6 is %ls\n", String(ip).c_str());
IP_Address ip2("fe80::52e5:49ff:fe93:1baf");
printf("ip6 is %ls\n", String(ip2).c_str());
OS::get_singleton()->print("ip6 is %ls\n", String(ip2).c_str());
IP_Address ip3("::ffff:192.168.0.1");
printf("ip6 is %ls\n", String(ip3).c_str());
OS::get_singleton()->print("ip6 is %ls\n", String(ip3).c_str());
return true;
String ip4 = "192.168.0.1";
success = ip4.is_valid_ip_address();
OS::get_singleton()->print("Is valid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
ip4 = "192.368.0.1";
success = (!ip4.is_valid_ip_address());
OS::get_singleton()->print("Is invalid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
success = ip6.is_valid_ip_address();
OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
success = (!ip6.is_valid_ip_address());
OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
success = (!ip6.is_valid_ip_address());
OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
ip6 = "2001:0db8::0:8a2e:370:7334";
success = (ip6.is_valid_ip_address());
OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
ip6 = "::ffff:192.168.0.1";
success = (ip6.is_valid_ip_address());
OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
if (!success) state = false;
return state;
};
typedef bool (*TestFunc)(void);

View file

@ -1543,11 +1543,11 @@ String::String(const StrRange& p_range) {
copy_from(p_range.c_str,p_range.len);
}
int String::hex_to_int() const {
int String::hex_to_int(bool p_with_prefix) const {
int l = length();
if (l<3)
return 0;
if (p_with_prefix && l<3)
return 0;
const CharType *s=ptr();
@ -1556,15 +1556,16 @@ int String::hex_to_int() const {
if (sign<0) {
s++;
l--;
if (l<2)
if (p_with_prefix && l<2)
return 0;
}
if (s[0]!='0' || s[1]!='x')
return 0;
s+=2;
l-=2;
if (p_with_prefix) {
if (s[0]!='0' || s[1]!='x')
return 0;
s+=2;
l-=2;
};
int hex=0;
@ -3510,6 +3511,36 @@ bool String::is_valid_integer() const {
}
bool String::is_valid_hex_number(bool p_with_prefix) const {
int from = 0;
int len = length();
if (len!=1 && (operator[](0)=='+' || operator[](0)=='-'))
from++;
if (p_with_prefix) {
if (len < 2)
return false;
if (operator[](from) != '0' || operator[](from+1) != 'x') {
return false;
};
from += 2;
};
for (int i=from; i<len; i++) {
CharType c = operator[](i);
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
continue;
return false;
};
return true;
};
bool String::is_valid_float() const {
int len = length();
@ -3646,20 +3677,41 @@ bool String::is_valid_html_color() const {
}
bool String::is_valid_ip_address() const {
Vector<String> ip = split(".");
if (ip.size()!=4)
return false;
for(int i=0;i<ip.size();i++) {
if (find(":") >= 0) {
String n = ip[i];
if (!n.is_valid_integer())
Vector<String> ip = split(":");
for (int i=0; i<ip.size(); i++) {
String n = ip[i];
if (n.empty())
continue;
if (n.is_valid_hex_number(false)) {
int nint = n.hex_to_int(false);
if (nint < 0 || nint > 0xffff)
return false;
continue;
};
if (!n.is_valid_ip_address())
return false;
};
} else {
Vector<String> ip = split(".");
if (ip.size()!=4)
return false;
int val = n.to_int();
if (val<0 || val>255)
return false;
}
for(int i=0;i<ip.size();i++) {
String n = ip[i];
if (!n.is_valid_integer())
return false;
int val = n.to_int();
if (val<0 || val>255)
return false;
}
};
return true;
}

View file

@ -144,7 +144,7 @@ public:
bool is_numeric() const;
double to_double() const;
float to_float() const;
int hex_to_int() const;
int hex_to_int(bool p_with_prefix = true) const;
int to_int() const;
int64_t to_int64() const;
@ -226,6 +226,7 @@ public:
bool is_valid_identifier() const;
bool is_valid_integer() const;
bool is_valid_float() const;
bool is_valid_hex_number(bool p_with_prefix) const;
bool is_valid_html_color() const;
bool is_valid_ip_address() const;