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

@@ -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;
}