[3.2] Backport hex_to_int/bin_to_int zero check and C# changes
This commit is contained in:
parent
bc90bcb65c
commit
226528097e
2 changed files with 54 additions and 11 deletions
|
@ -1670,9 +1670,10 @@ String::String(const StrRange &p_range) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::hex_to_int(bool p_with_prefix) const {
|
int String::hex_to_int(bool p_with_prefix) const {
|
||||||
|
int len = length();
|
||||||
if (p_with_prefix && length() < 3)
|
if (len == 0 || (p_with_prefix && len < 3)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const CharType *s = ptr();
|
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 {
|
int64_t String::bin_to_int64(bool p_with_prefix) const {
|
||||||
|
int len = length();
|
||||||
if (p_with_prefix && length() < 3)
|
if (len == 0 || (p_with_prefix && len < 3)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const CharType *s = ptr();
|
const CharType *s = ptr();
|
||||||
|
|
||||||
|
|
|
@ -97,6 +97,36 @@ namespace Godot
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="instance">The string to convert.</param>
|
||||||
|
/// <returns>The converted string.</returns>
|
||||||
|
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);;
|
||||||
|
}
|
||||||
|
|
||||||
// <summary>
|
// <summary>
|
||||||
// Return the amount of substrings in string.
|
// Return the amount of substrings in string.
|
||||||
// </summary>
|
// </summary>
|
||||||
|
@ -457,7 +487,7 @@ namespace Godot
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a hexadecimal representation of this byte as a string.
|
/// Returns a hexadecimal representation of this byte as a string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bytes">The byte to encode.</param>
|
/// <param name="b">The byte to encode.</param>
|
||||||
/// <returns>The hexadecimal representation of this byte.</returns>
|
/// <returns>The hexadecimal representation of this byte.</returns>
|
||||||
internal static string HexEncode(this byte b)
|
internal static string HexEncode(this byte b)
|
||||||
{
|
{
|
||||||
|
@ -501,11 +531,20 @@ namespace Godot
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// <summary>
|
/// <summary>
|
||||||
// Convert a string containing an hexadecimal number into an int.
|
/// Converts a string containing a hexadecimal number into an integer.
|
||||||
// </summary>
|
/// Hexadecimal strings can either be prefixed with `0x` or not,
|
||||||
|
/// and they can also start with a `-` before the optional prefix.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="instance">The string to convert.</param>
|
||||||
|
/// <returns>The converted string.</returns>
|
||||||
public static int HexToInt(this string instance)
|
public static int HexToInt(this string instance)
|
||||||
{
|
{
|
||||||
|
if (instance.Length == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int sign = 1;
|
int sign = 1;
|
||||||
|
|
||||||
if (instance[0] == '-')
|
if (instance[0] == '-')
|
||||||
|
@ -514,10 +553,12 @@ namespace Godot
|
||||||
instance = instance.Substring(1);
|
instance = instance.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!instance.StartsWith("0x"))
|
if (instance.StartsWith("0x"))
|
||||||
return 0;
|
{
|
||||||
|
instance = instance.Substring(2);
|
||||||
|
}
|
||||||
|
|
||||||
return sign * int.Parse(instance.Substring(2), NumberStyles.HexNumber);
|
return sign * int.Parse(instance, NumberStyles.HexNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
// <summary>
|
// <summary>
|
||||||
|
|
Loading…
Reference in a new issue