Skip to content

Commit

Permalink
Avoid calling soon-to-be-removed string.Trim(ReadOnlySpan<char>) meth…
Browse files Browse the repository at this point in the history
…ods (#9907)

* Avoid calling soon-to-be-removed string.Trim(ReadOnlySpan<char>) methods

* Also fix KnownStrings.WhitspaceChars
  • Loading branch information
bartonjs authored Oct 10, 2024
1 parent 3c5b1c7 commit 7450b36
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static class KnownStrings
public const string DefaultPrefix = "p";

public const string ReferenceName = "__ReferenceID";
public static ReadOnlySpan<char> WhitespaceChars => [' ', '\t', '\n', '\r', '\f'];
public static readonly char[] WhitespaceChars = [' ', '\t', '\n', '\r', '\f'];
public const char SpaceChar = ' ';
public const char TabChar = '\t';
public const char NewlineChar = '\n';
Expand Down
22 changes: 11 additions & 11 deletions src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ public override string ToString()
{
foreach (string paramterKey in _parameterDictionary.Keys)
{
stringBuilder.Append(LinearWhiteSpaceChars[0]);
stringBuilder.Append(_linearWhiteSpaceChars[0]);
stringBuilder.Append(_semicolonSeparator);
stringBuilder.Append(LinearWhiteSpaceChars[0]);
stringBuilder.Append(_linearWhiteSpaceChars[0]);
stringBuilder.Append(paramterKey);
stringBuilder.Append(_equalSeparator);
stringBuilder.Append(_parameterDictionary[paramterKey]);
Expand Down Expand Up @@ -399,13 +399,13 @@ private static void ValidateCarriageReturns(string contentType)
//character of the content type are not Linear White Spaces. So its safe to
//assume that the index will be greater than 0 and less that length-2.

int index = contentType.IndexOf(LinearWhiteSpaceChars[2]);
int index = contentType.IndexOf(_linearWhiteSpaceChars[2]);

while (index != -1)
{
if (contentType[index - 1] == LinearWhiteSpaceChars[1] || contentType[index + 1] == LinearWhiteSpaceChars[1])
if (contentType[index - 1] == _linearWhiteSpaceChars[1] || contentType[index + 1] == _linearWhiteSpaceChars[1])
{
index = contentType.IndexOf(LinearWhiteSpaceChars[2], ++index);
index = contentType.IndexOf(_linearWhiteSpaceChars[2], ++index);
}
else
throw new ArgumentException(SR.InvalidLinearWhiteSpaceCharacter);
Expand All @@ -421,7 +421,7 @@ private static void ValidateCarriageReturns(string contentType)
private void ParseTypeAndSubType(ReadOnlySpan<char> typeAndSubType)
{
//okay to trim at this point the end of the string as Linear White Spaces(LWS) chars are allowed here.
typeAndSubType = typeAndSubType.TrimEnd(LinearWhiteSpaceChars);
typeAndSubType = typeAndSubType.TrimEnd(_linearWhiteSpaceChars);

int forwardSlashPos = typeAndSubType.IndexOf('/');
if (forwardSlashPos < 0 || // no slashes
Expand Down Expand Up @@ -460,7 +460,7 @@ private void ParseParameterAndValue(ReadOnlySpan<char> parameterAndValue)

//okay to trim start as there can be spaces before the begining
//of the parameter name.
parameterAndValue = parameterAndValue.TrimStart(LinearWhiteSpaceChars);
parameterAndValue = parameterAndValue.TrimStart(_linearWhiteSpaceChars);

int equalSignIndex = parameterAndValue.IndexOf(_equalSeparator);

Expand All @@ -478,7 +478,7 @@ private void ParseParameterAndValue(ReadOnlySpan<char> parameterAndValue)
ValidateToken(parameterAndValue.Slice(0, equalSignIndex).ToString()),
ValidateQuotedStringOrToken(parameterAndValue.Slice(parameterStartIndex, parameterValueLength).ToString()));

parameterAndValue = parameterAndValue.Slice(parameterStartIndex + parameterValueLength).TrimStart(LinearWhiteSpaceChars);
parameterAndValue = parameterAndValue.Slice(parameterStartIndex + parameterValueLength).TrimStart(_linearWhiteSpaceChars);
}
}

Expand All @@ -501,7 +501,7 @@ private static int GetLengthOfParameterValue(ReadOnlySpan<char> s, int startInde

if (semicolonIndex != -1)
{
int lwsIndex = s.Slice(startIndex).IndexOfAny(LinearWhiteSpaceChars);
int lwsIndex = s.Slice(startIndex).IndexOfAny(_linearWhiteSpaceChars);
length = lwsIndex != -1 && lwsIndex < semicolonIndex ? lwsIndex : semicolonIndex;
length += startIndex; // the indexes from IndexOf{Any} are based on slicing from startIndex
}
Expand Down Expand Up @@ -635,7 +635,7 @@ private static bool IsAsciiLetterOrDigit(char character) =>
/// </summary>
/// <param name="ch">input character</param>
/// <returns></returns>
private static bool IsLinearWhiteSpaceChar(char ch) => LinearWhiteSpaceChars.Contains(ch);
private static bool IsLinearWhiteSpaceChar(char ch) => new ReadOnlySpan<char>(_linearWhiteSpaceChars).Contains(ch);

/// <summary>
/// Lazy initialization for the ParameterDictionary
Expand Down Expand Up @@ -678,7 +678,7 @@ private void EnsureParameterDictionary()
];

//Linear White Space characters
private static ReadOnlySpan<char> LinearWhiteSpaceChars => [
private static readonly char[] _linearWhiteSpaceChars = [
' ', // space - \x20
'\n', // new line - \x0A
'\r', // carriage return - \x0D
Expand Down

0 comments on commit 7450b36

Please sign in to comment.