added implementation of is_valid_ip_address()

(cherry picked from commit a3131a6b5b)
This commit is contained in:
Ariel Manzur 2016-10-20 09:58:00 -03:00 committed by Fabio Alessandrelli
parent d8b0070b8c
commit 5546929712
3 changed files with 117 additions and 25 deletions

View File

@ -846,19 +846,58 @@ bool test_28() {
bool test_29() { bool test_29() {
bool error = false;
bool state = true;
bool success = false;
IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); 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); 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"); 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"); 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); 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); 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(); int l = length();
if (l<3) if (p_with_prefix && l<3)
return 0; return 0;
const CharType *s=ptr(); const CharType *s=ptr();
@ -1556,15 +1556,16 @@ int String::hex_to_int() const {
if (sign<0) { if (sign<0) {
s++; s++;
l--; l--;
if (l<2) if (p_with_prefix && l<2)
return 0; return 0;
} }
if (s[0]!='0' || s[1]!='x') if (p_with_prefix) {
return 0; if (s[0]!='0' || s[1]!='x')
return 0;
s+=2; s+=2;
l-=2; l-=2;
};
int hex=0; 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 { bool String::is_valid_float() const {
int len = length(); int len = length();
@ -3646,20 +3677,41 @@ bool String::is_valid_html_color() const {
} }
bool String::is_valid_ip_address() const { bool String::is_valid_ip_address() const {
Vector<String> ip = split("."); if (find(":") >= 0) {
if (ip.size()!=4)
return false;
for(int i=0;i<ip.size();i++) {
String n = ip[i]; Vector<String> ip = split(":");
if (!n.is_valid_integer()) 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; return false;
int val = n.to_int(); for(int i=0;i<ip.size();i++) {
if (val<0 || val>255)
return false; 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; return true;
} }

View File

@ -144,7 +144,7 @@ public:
bool is_numeric() const; bool is_numeric() const;
double to_double() const; double to_double() const;
float to_float() const; float to_float() const;
int hex_to_int() const; int hex_to_int(bool p_with_prefix = true) const;
int to_int() const; int to_int() const;
int64_t to_int64() const; int64_t to_int64() const;
@ -226,6 +226,7 @@ public:
bool is_valid_identifier() const; bool is_valid_identifier() const;
bool is_valid_integer() const; bool is_valid_integer() const;
bool is_valid_float() 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_html_color() const;
bool is_valid_ip_address() const; bool is_valid_ip_address() const;