From c02540bd666b418b4faff5bdfa38b121db4a8c10 Mon Sep 17 00:00:00 2001 From: "Luca Martinelli (laptop)" Date: Fri, 26 Apr 2024 10:04:55 +0200 Subject: [PATCH] fixed StringExtensions.CompareTo IndexOutOfRangeException fixes #91118 marked CompareTo obsolete dropped CompareTo [-1,1] range Co-authored-by: Raul Santos --- .../GodotSharp/Core/StringExtensions.cs | 61 +++---------------- 1 file changed, 8 insertions(+), 53 deletions(-) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 37f319b69714..d8a3e916996a 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -417,76 +417,31 @@ namespace Godot } /// - /// Performs a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater. + /// Performs a case-sensitive comparison to another string and returns an integer that indicates their relative position in the sort order. /// /// /// /// The string to compare. /// The other string to compare. - /// -1 if less, 0 if equal and +1 if greater. + /// An integer that indicates the lexical relationship between the two comparands. public static int CasecmpTo(this string instance, string to) { return instance.CompareTo(to, caseSensitive: true); } /// - /// Performs a comparison to another string, return -1 if less, 0 if equal and +1 if greater. + /// Performs a comparison to another string and returns an integer that indicates their relative position in the sort order. /// /// The string to compare. /// The other string to compare. /// /// If , the comparison will be case sensitive. /// - /// -1 if less, 0 if equal and +1 if greater. + /// An integer that indicates the lexical relationship between the two comparands. + [Obsolete("Use string.Compare instead.")] public static int CompareTo(this string instance, string to, bool caseSensitive = true) { - if (string.IsNullOrEmpty(instance)) - return string.IsNullOrEmpty(to) ? 0 : -1; - - if (string.IsNullOrEmpty(to)) - return 1; - - int instanceIndex = 0; - int toIndex = 0; - - if (caseSensitive) // Outside while loop to avoid checking multiple times, despite some code duplication. - { - while (true) - { - if (to[toIndex] == 0 && instance[instanceIndex] == 0) - return 0; // We're equal - if (instance[instanceIndex] == 0) - return -1; // If this is empty, and the other one is not, then we're less... I think? - if (to[toIndex] == 0) - return 1; // Otherwise the other one is smaller... - if (instance[instanceIndex] < to[toIndex]) // More than - return -1; - if (instance[instanceIndex] > to[toIndex]) // Less than - return 1; - - instanceIndex++; - toIndex++; - } - } - else - { - while (true) - { - if (to[toIndex] == 0 && instance[instanceIndex] == 0) - return 0; // We're equal - if (instance[instanceIndex] == 0) - return -1; // If this is empty, and the other one is not, then we're less... I think? - if (to[toIndex] == 0) - return 1; // Otherwise the other one is smaller.. - if (char.ToUpperInvariant(instance[instanceIndex]) < char.ToUpperInvariant(to[toIndex])) // More than - return -1; - if (char.ToUpperInvariant(instance[instanceIndex]) > char.ToUpperInvariant(to[toIndex])) // Less than - return 1; - - instanceIndex++; - toIndex++; - } - } + return string.Compare(instance, to, !caseSensitive); } /// @@ -1297,13 +1252,13 @@ namespace Godot } /// - /// Perform a case-insensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater. + /// Performs a case-insensitive comparison to another string and returns an integer that indicates their relative position in the sort order. /// /// /// /// The string to compare. /// The other string to compare. - /// -1 if less, 0 if equal and +1 if greater. + /// An integer that indicates the lexical relationship between the two comparands. public static int NocasecmpTo(this string instance, string to) { return instance.CompareTo(to, caseSensitive: false);