Skip to content

Commit 458106a

Browse files
committed
Merge pull request #91203 from pirey0/stringcomp_fix
C#: Fix `StringExtensions.CompareTo` IndexOutOfRangeException
2 parents 84013a3 + c02540b commit 458106a

File tree

1 file changed

+8
-53
lines changed

1 file changed

+8
-53
lines changed

modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -417,76 +417,31 @@ private static string CamelcaseToUnderscore(this string instance, bool lowerCase
417417
}
418418

419419
/// <summary>
420-
/// Performs a case-sensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
420+
/// Performs a case-sensitive comparison to another string and returns an integer that indicates their relative position in the sort order.
421421
/// </summary>
422422
/// <seealso cref="NocasecmpTo(string, string)"/>
423423
/// <seealso cref="CompareTo(string, string, bool)"/>
424424
/// <param name="instance">The string to compare.</param>
425425
/// <param name="to">The other string to compare.</param>
426-
/// <returns>-1 if less, 0 if equal and +1 if greater.</returns>
426+
/// <returns>An integer that indicates the lexical relationship between the two comparands.</returns>
427427
public static int CasecmpTo(this string instance, string to)
428428
{
429429
return instance.CompareTo(to, caseSensitive: true);
430430
}
431431

432432
/// <summary>
433-
/// Performs a comparison to another string, return -1 if less, 0 if equal and +1 if greater.
433+
/// Performs a comparison to another string and returns an integer that indicates their relative position in the sort order.
434434
/// </summary>
435435
/// <param name="instance">The string to compare.</param>
436436
/// <param name="to">The other string to compare.</param>
437437
/// <param name="caseSensitive">
438438
/// If <see langword="true"/>, the comparison will be case sensitive.
439439
/// </param>
440-
/// <returns>-1 if less, 0 if equal and +1 if greater.</returns>
440+
/// <returns>An integer that indicates the lexical relationship between the two comparands.</returns>
441+
[Obsolete("Use string.Compare instead.")]
441442
public static int CompareTo(this string instance, string to, bool caseSensitive = true)
442443
{
443-
if (string.IsNullOrEmpty(instance))
444-
return string.IsNullOrEmpty(to) ? 0 : -1;
445-
446-
if (string.IsNullOrEmpty(to))
447-
return 1;
448-
449-
int instanceIndex = 0;
450-
int toIndex = 0;
451-
452-
if (caseSensitive) // Outside while loop to avoid checking multiple times, despite some code duplication.
453-
{
454-
while (true)
455-
{
456-
if (to[toIndex] == 0 && instance[instanceIndex] == 0)
457-
return 0; // We're equal
458-
if (instance[instanceIndex] == 0)
459-
return -1; // If this is empty, and the other one is not, then we're less... I think?
460-
if (to[toIndex] == 0)
461-
return 1; // Otherwise the other one is smaller...
462-
if (instance[instanceIndex] < to[toIndex]) // More than
463-
return -1;
464-
if (instance[instanceIndex] > to[toIndex]) // Less than
465-
return 1;
466-
467-
instanceIndex++;
468-
toIndex++;
469-
}
470-
}
471-
else
472-
{
473-
while (true)
474-
{
475-
if (to[toIndex] == 0 && instance[instanceIndex] == 0)
476-
return 0; // We're equal
477-
if (instance[instanceIndex] == 0)
478-
return -1; // If this is empty, and the other one is not, then we're less... I think?
479-
if (to[toIndex] == 0)
480-
return 1; // Otherwise the other one is smaller..
481-
if (char.ToUpperInvariant(instance[instanceIndex]) < char.ToUpperInvariant(to[toIndex])) // More than
482-
return -1;
483-
if (char.ToUpperInvariant(instance[instanceIndex]) > char.ToUpperInvariant(to[toIndex])) // Less than
484-
return 1;
485-
486-
instanceIndex++;
487-
toIndex++;
488-
}
489-
}
444+
return string.Compare(instance, to, !caseSensitive);
490445
}
491446

492447
/// <summary>
@@ -1297,13 +1252,13 @@ public static string Md5Text(this string instance)
12971252
}
12981253

12991254
/// <summary>
1300-
/// Perform a case-insensitive comparison to another string, return -1 if less, 0 if equal and +1 if greater.
1255+
/// Performs a case-insensitive comparison to another string and returns an integer that indicates their relative position in the sort order.
13011256
/// </summary>
13021257
/// <seealso cref="CasecmpTo(string, string)"/>
13031258
/// <seealso cref="CompareTo(string, string, bool)"/>
13041259
/// <param name="instance">The string to compare.</param>
13051260
/// <param name="to">The other string to compare.</param>
1306-
/// <returns>-1 if less, 0 if equal and +1 if greater.</returns>
1261+
/// <returns>An integer that indicates the lexical relationship between the two comparands.</returns>
13071262
public static int NocasecmpTo(this string instance, string to)
13081263
{
13091264
return instance.CompareTo(to, caseSensitive: false);

0 commit comments

Comments
 (0)