Skip to content

Commit

Permalink
Streamline String.Substring (#73882)
Browse files Browse the repository at this point in the history
- Split the one-arg Substring from the two-arg Substring to avoid unnecessary checks in the former
- Employ the same argument validation checks as Span, and then delegate to a helper that does more detailed checking to throw the right exception
- Avoid duplicative checks in the body
- Reorder checks in one-arg overload to do success paths before error paths where possible
  • Loading branch information
stephentoub authored Aug 13, 2022
1 parent 4c24000 commit a037f0c
Showing 1 changed file with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1798,43 +1798,65 @@ private static void CheckStringSplitOptions(StringSplitOptions options)

// Returns a substring of this string.
//
public string Substring(int startIndex) => Substring(startIndex, Length - startIndex);

public string Substring(int startIndex, int length)
public string Substring(int startIndex)
{
if (startIndex < 0)
if (startIndex == 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
return this;
}

if (startIndex > Length)
int length = Length - startIndex;
if (length == 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLargerThanLength);
return Empty;
}

if (length < 0)
if ((uint)startIndex > (uint)Length)
{
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength);
ThrowSubstringArgumentOutOfRange(startIndex, length);
}

if (startIndex > Length - length)
return InternalSubString(startIndex, length);
}

public string Substring(int startIndex, int length)
{
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
if ((ulong)(uint)startIndex + (ulong)(uint)length > (ulong)(uint)Length)
#else
if ((uint)startIndex > (uint)Length || (uint)length > (uint)(Length - startIndex))
#endif
{
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexLength);
ThrowSubstringArgumentOutOfRange(startIndex, length);
}

if (length == 0)
{
return string.Empty;
return Empty;
}

if (startIndex == 0 && length == this.Length)
if (length == Length)
{
Debug.Assert(startIndex == 0);
return this;
}

return InternalSubString(startIndex, length);
}

[DoesNotReturn]
private void ThrowSubstringArgumentOutOfRange(int startIndex, int length)
{
(string paramName, string message) =
startIndex < 0 ? (nameof(startIndex), SR.ArgumentOutOfRange_StartIndex) :
startIndex > Length ? (nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLargerThanLength) :
length < 0 ? (nameof(length), SR.ArgumentOutOfRange_NegativeLength) :
(nameof(length), SR.ArgumentOutOfRange_IndexLength);

throw new ArgumentOutOfRangeException(paramName, message);
}

private string InternalSubString(int startIndex, int length)
{
Debug.Assert(startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!");
Expand Down

0 comments on commit a037f0c

Please sign in to comment.