summaryrefslogtreecommitdiffstats
path: root/core/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp96
1 files changed, 72 insertions, 24 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 0c26fe90c6..27bb8eac72 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -1547,20 +1547,20 @@ String::String(const StrRange& p_range) {
int String::hex_to_int(bool p_with_prefix) const {
- int l = length();
+ int l = length();
if (p_with_prefix && l<3)
return 0;
- const CharType *s=ptr();
+ const CharType *s=ptr();
- int sign = s[0]=='-' ? -1 : 1;
+ int sign = s[0]=='-' ? -1 : 1;
- if (sign<0) {
- s++;
- l--;
+ if (sign<0) {
+ s++;
+ l--;
if (p_with_prefix && l<2)
- return 0;
- }
+ return 0;
+ }
if (p_with_prefix) {
if (s[0]!='0' || s[1]!='x')
@@ -1569,26 +1569,74 @@ int String::hex_to_int(bool p_with_prefix) const {
l-=2;
};
- int hex=0;
+ int hex=0;
- while(*s) {
+ while(*s) {
- CharType c = LOWERCASE(*s);
- int n;
- if (c>='0' && c<='9') {
- n=c-'0';
- } else if (c>='a' && c<='f') {
- n=(c-'a')+10;
- } else {
- return 0;
- }
+ CharType c = LOWERCASE(*s);
+ int n;
+ if (c>='0' && c<='9') {
+ n=c-'0';
+ } else if (c>='a' && c<='f') {
+ n=(c-'a')+10;
+ } else {
+ return 0;
+ }
- hex*=16;
- hex+=n;
- s++;
- }
+ hex*=16;
+ hex+=n;
+ s++;
+ }
+
+ return hex*sign;
+
+}
+
+
+int64_t String::hex_to_int64(bool p_with_prefix) const {
+
+ int l = length();
+ if (p_with_prefix && l<3)
+ return 0;
+
+ const CharType *s=ptr();
+
+ int64_t sign = s[0]=='-' ? -1 : 1;
+
+ if (sign<0) {
+ s++;
+ l--;
+ if (p_with_prefix && l<2)
+ return 0;
+ }
+
+ if (p_with_prefix) {
+ if (s[0]!='0' || s[1]!='x')
+ return 0;
+ s+=2;
+ l-=2;
+ };
+
+ int64_t hex=0;
+
+ while(*s) {
+
+ CharType c = LOWERCASE(*s);
+ int64_t n;
+ if (c>='0' && c<='9') {
+ n=c-'0';
+ } else if (c>='a' && c<='f') {
+ n=(c-'a')+10;
+ } else {
+ return 0;
+ }
+
+ hex*=16;
+ hex+=n;
+ s++;
+ }
- return hex*sign;
+ return hex*sign;
}