diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2021-01-28 07:39:05 -0500 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2021-01-28 07:43:53 -0500 |
commit | a3e3bf822761c477d3a297fe004496ffc6c7b10d (patch) | |
tree | 60b1748d82a703388947294d927fde87534286b6 /core | |
parent | 726967f45318359d95e3b0c359e088ca6d430292 (diff) |
Make hex_to_int and bin_to_int handle the prefix automatically
Also add BinToInt to C#
Diffstat (limited to 'core')
-rw-r--r-- | core/string/ustring.cpp | 22 | ||||
-rw-r--r-- | core/string/ustring.h | 4 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 4 |
3 files changed, 13 insertions, 17 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 64311a7cd7..a38395b1c7 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -2097,8 +2097,9 @@ String::String(const StrRange &p_range) { copy_from(p_range.c_str, p_range.len); } -int64_t String::hex_to_int(bool p_with_prefix) const { - if (p_with_prefix && length() < 3) { +int64_t String::hex_to_int() const { + int len = length(); + if (len == 0) { return 0; } @@ -2110,10 +2111,7 @@ int64_t String::hex_to_int(bool p_with_prefix) const { s++; } - if (p_with_prefix) { - if (s[0] != '0' || s[1] != 'x') { - return 0; - } + if (len > 2 && s[0] == '0' && s[1] == 'x') { s += 2; } @@ -2140,8 +2138,9 @@ int64_t String::hex_to_int(bool p_with_prefix) const { return hex * sign; } -int64_t String::bin_to_int(bool p_with_prefix) const { - if (p_with_prefix && length() < 3) { +int64_t String::bin_to_int() const { + int len = length(); + if (len == 0) { return 0; } @@ -2153,10 +2152,7 @@ int64_t String::bin_to_int(bool p_with_prefix) const { s++; } - if (p_with_prefix) { - if (s[0] != '0' || s[1] != 'b') { - return 0; - } + if (len > 2 && s[0] == '0' && s[1] == 'b') { s += 2; } @@ -4251,7 +4247,7 @@ bool String::is_valid_ip_address() const { continue; } if (n.is_valid_hex_number(false)) { - int64_t nint = n.hex_to_int(false); + int64_t nint = n.hex_to_int(); if (nint < 0 || nint > 0xffff) { return false; } diff --git a/core/string/ustring.h b/core/string/ustring.h index 9b7b7a21d4..e3c747af26 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -318,8 +318,8 @@ public: bool is_numeric() const; double to_float() const; - int64_t hex_to_int(bool p_with_prefix = true) const; - int64_t bin_to_int(bool p_with_prefix = true) const; + int64_t hex_to_int() const; + int64_t bin_to_int() const; int64_t to_int() const; static int64_t to_int(const char *p_str, int p_len = -1); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 6523c597cf..bb31b9c864 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -968,8 +968,8 @@ static void _register_variant_builtin_methods() { bind_method(String, to_int, sarray(), varray()); bind_method(String, to_float, sarray(), varray()); - bind_method(String, hex_to_int, sarray("with_prefix"), varray(true)); - bind_method(String, bin_to_int, sarray("with_prefix"), varray(true)); + bind_method(String, hex_to_int, sarray(), varray()); + bind_method(String, bin_to_int, sarray(), varray()); bind_method(String, lpad, sarray("min_length", "character"), varray(" ")); bind_method(String, rpad, sarray("min_length", "character"), varray(" ")); |