Skip to content

Commit

Permalink
Fix str.Remove(str.Length) to not throw (#50096)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Mar 23, 2021
1 parent 4c2f931 commit 3116c4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/libraries/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4496,7 +4496,7 @@ public static void PadRight_NegativeTotalWidth_ThrowsArgumentOutOfRangeException
[InlineData("", 0, 0, "")]
public static void Remove(string s, int startIndex, int count, string expected)
{
if (startIndex + count == s.Length && count != 0)
if (startIndex + count == s.Length)
{
Assert.Equal(expected, s.Remove(startIndex));
}
Expand All @@ -4512,8 +4512,8 @@ public static void Remove_Invalid()
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => s.Remove(-1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => s.Remove(-1, 0));

// Start index >= string.Length
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => s.Remove(s.Length));
// Start index > string.Length
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => s.Remove(s.Length + 1));

// Count < 0
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => s.Remove(0, -1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,8 @@ public string Remove(int startIndex, int count)
// a remove that just takes a startindex.
public string Remove(int startIndex)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);

if (startIndex >= Length)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLessThanLength);
if ((uint)startIndex > Length)
throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex < 0 ? SR.ArgumentOutOfRange_StartIndex : SR.ArgumentOutOfRange_StartIndexLessThanLength);

return Substring(0, startIndex);
}
Expand Down

0 comments on commit 3116c4d

Please sign in to comment.