From 226528097e8a953934b2156faf63b222117587e0 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Thu, 28 Jan 2021 09:22:13 -0500 Subject: [PATCH] [3.2] Backport hex_to_int/bin_to_int zero check and C# changes --- core/ustring.cpp | 10 ++-- .../GodotSharp/Core/StringExtensions.cs | 55 ++++++++++++++++--- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index 31147992b0..5dc7221976 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1670,9 +1670,10 @@ String::String(const StrRange &p_range) { } int String::hex_to_int(bool p_with_prefix) const { - - if (p_with_prefix && length() < 3) + int len = length(); + if (len == 0 || (p_with_prefix && len < 3)) { return 0; + } const CharType *s = ptr(); @@ -1755,9 +1756,10 @@ int64_t String::hex_to_int64(bool p_with_prefix) const { } int64_t String::bin_to_int64(bool p_with_prefix) const { - - if (p_with_prefix && length() < 3) + int len = length(); + if (len == 0 || (p_with_prefix && len < 3)) { return 0; + } const CharType *s = ptr(); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index c2f0b9a974..407f993bdb 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -97,6 +97,36 @@ namespace Godot return b; } + /// + /// Converts a string containing a binary number into an integer. + /// Binary strings can either be prefixed with `0b` or not, + /// and they can also start with a `-` before the optional prefix. + /// + /// The string to convert. + /// The converted string. + public static int BinToInt(this string instance) + { + if (instance.Length == 0) + { + return 0; + } + + int sign = 1; + + if (instance[0] == '-') + { + sign = -1; + instance = instance.Substring(1); + } + + if (instance.StartsWith("0b")) + { + instance = instance.Substring(2); + } + + return sign * Convert.ToInt32(instance, 2);; + } + // // Return the amount of substrings in string. // @@ -457,7 +487,7 @@ namespace Godot /// /// Returns a hexadecimal representation of this byte as a string. /// - /// The byte to encode. + /// The byte to encode. /// The hexadecimal representation of this byte. internal static string HexEncode(this byte b) { @@ -501,11 +531,20 @@ namespace Godot return ret; } - // - // Convert a string containing an hexadecimal number into an int. - // + /// + /// Converts a string containing a hexadecimal number into an integer. + /// Hexadecimal strings can either be prefixed with `0x` or not, + /// and they can also start with a `-` before the optional prefix. + /// + /// The string to convert. + /// The converted string. public static int HexToInt(this string instance) { + if (instance.Length == 0) + { + return 0; + } + int sign = 1; if (instance[0] == '-') @@ -514,10 +553,12 @@ namespace Godot instance = instance.Substring(1); } - if (!instance.StartsWith("0x")) - return 0; + if (instance.StartsWith("0x")) + { + instance = instance.Substring(2); + } - return sign * int.Parse(instance.Substring(2), NumberStyles.HexNumber); + return sign * int.Parse(instance, NumberStyles.HexNumber); } //