summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp90
1 files changed, 71 insertions, 19 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 2e907381f7..f9c10615b3 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -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())
- return false;
- int val = n.to_int();
- if (val<0 || val>255)
+ 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;
- }
+ 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;
}