From a3e3bf822761c477d3a297fe004496ffc6c7b10d Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Thu, 28 Jan 2021 07:39:05 -0500 Subject: Make hex_to_int and bin_to_int handle the prefix automatically Also add BinToInt to C# --- .../GodotSharp/GodotSharp/Core/StringExtensions.cs | 53 +++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'modules/mono') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 0700f197ff..2b7bfb2a59 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. // @@ -493,11 +523,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] == '-') @@ -506,10 +545,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); } // -- cgit v1.2.3 From e829b7aee48cfc988abea5a42bdbf02638a16513 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Sun, 29 Nov 2020 22:43:38 -0500 Subject: Unify URI encoding/decoding and add to C# http_escape and percent_encode have been unified into uri_encode, and http_unescape and percent_decode have been unified into uri_decode. --- .../GodotSharp/GodotSharp/Core/StringExtensions.cs | 45 ++++++++++++++-------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'modules/mono') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 2b7bfb2a59..4dc182fb73 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -479,7 +479,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) { @@ -932,22 +932,6 @@ namespace Godot return s; } - // - // Decode a percent-encoded string. See [method percent_encode]. - // - public static string PercentDecode(this string instance) - { - return Uri.UnescapeDataString(instance); - } - - // - // Percent-encode a string. This is meant to encode parameters in a URL when sending a HTTP GET request and bodies of form-urlencoded POST request. - // - public static string PercentEncode(this string instance) - { - return Uri.EscapeDataString(instance); - } - // // If the string is a path, this concatenates [code]file[/code] at the end of the string as a subpath. E.g. [code]"this/is".plus_file("path") == "this/is/path"[/code]. // @@ -1210,6 +1194,33 @@ namespace Godot return Encoding.UTF8.GetBytes(instance); } + /// + /// Decodes a string in URL encoded format. This is meant to + /// decode parameters in a URL when receiving an HTTP request. + /// This mostly wraps around `System.Uri.UnescapeDataString()`, + /// but also handles `+`. + /// See for encoding. + /// + /// The string to decode. + /// The unescaped string. + public static string URIDecode(this string instance) + { + return Uri.UnescapeDataString(instance.Replace("+", "%20")); + } + + /// + /// Encodes a string to URL friendly format. This is meant to + /// encode parameters in a URL when sending an HTTP request. + /// This wraps around `System.Uri.EscapeDataString()`. + /// See for decoding. + /// + /// The string to encode. + /// The escaped string. + public static string URIEncode(this string instance) + { + return Uri.EscapeDataString(instance); + } + // // Return a copy of the string with special characters escaped using the XML standard. // -- cgit v1.2.3