未验证 提交 e9357808 编写于 作者: E Egor Bogatov 提交者: GitHub

Fast-path in String.Trim (#84300)

上级 bc887b38
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册