Skip to content

Commit

Permalink
Fast-path in String.Trim (#84300)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Apr 4, 2023
1 parent bc887b3 commit e935780
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2142,10 +2142,24 @@ public string ToUpperInvariant()
// Trims the whitespace from both ends of the string. Whitespace is defined by
// char.IsWhiteSpace.
//
public string Trim() => TrimWhiteSpaceHelper(TrimType.Both);
public string Trim()
{
if (Length == 0 || (!char.IsWhiteSpace(_firstChar) && !char.IsWhiteSpace(this[^1])))
{
return this;
}
return TrimWhiteSpaceHelper(TrimType.Both);
}

// Removes a set of characters from the beginning and end of this string.
public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both);
public unsafe string Trim(char trimChar)
{
if (Length == 0 || (_firstChar != trimChar && this[^1] != trimChar))
{
return this;
}
return TrimHelper(&trimChar, 1, TrimType.Both);
}

// Removes a set of characters from the beginning and end of this string.
public unsafe string Trim(params char[]? trimChars)
Expand Down

0 comments on commit e935780

Please sign in to comment.