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

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

* Also fix KnownStrings.WhitspaceChars
This commit is contained in:
Jeremy Barton 2024-10-10 09:23:46 -07:00 коммит произвёл GitHub
Родитель 3c5b1c7f50
Коммит 7450b3658f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 12 добавлений и 12 удалений

Просмотреть файл

@ -35,7 +35,7 @@ namespace System.Xaml.MS.Impl
public const string DefaultPrefix = "p"; public const string DefaultPrefix = "p";
public const string ReferenceName = "__ReferenceID"; 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 SpaceChar = ' ';
public const char TabChar = '\t'; public const char TabChar = '\t';
public const char NewlineChar = '\n'; public const char NewlineChar = '\n';

Просмотреть файл

@ -287,9 +287,9 @@ namespace MS.Internal
{ {
foreach (string paramterKey in _parameterDictionary.Keys) foreach (string paramterKey in _parameterDictionary.Keys)
{ {
stringBuilder.Append(LinearWhiteSpaceChars[0]); stringBuilder.Append(_linearWhiteSpaceChars[0]);
stringBuilder.Append(_semicolonSeparator); stringBuilder.Append(_semicolonSeparator);
stringBuilder.Append(LinearWhiteSpaceChars[0]); stringBuilder.Append(_linearWhiteSpaceChars[0]);
stringBuilder.Append(paramterKey); stringBuilder.Append(paramterKey);
stringBuilder.Append(_equalSeparator); stringBuilder.Append(_equalSeparator);
stringBuilder.Append(_parameterDictionary[paramterKey]); stringBuilder.Append(_parameterDictionary[paramterKey]);
@ -399,13 +399,13 @@ namespace MS.Internal
//character of the content type are not Linear White Spaces. So its safe to //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. //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) 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 else
throw new ArgumentException(SR.InvalidLinearWhiteSpaceCharacter); throw new ArgumentException(SR.InvalidLinearWhiteSpaceCharacter);
@ -421,7 +421,7 @@ namespace MS.Internal
private void ParseTypeAndSubType(ReadOnlySpan<char> typeAndSubType) 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. //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('/'); int forwardSlashPos = typeAndSubType.IndexOf('/');
if (forwardSlashPos < 0 || // no slashes if (forwardSlashPos < 0 || // no slashes
@ -460,7 +460,7 @@ namespace MS.Internal
//okay to trim start as there can be spaces before the begining //okay to trim start as there can be spaces before the begining
//of the parameter name. //of the parameter name.
parameterAndValue = parameterAndValue.TrimStart(LinearWhiteSpaceChars); parameterAndValue = parameterAndValue.TrimStart(_linearWhiteSpaceChars);
int equalSignIndex = parameterAndValue.IndexOf(_equalSeparator); int equalSignIndex = parameterAndValue.IndexOf(_equalSeparator);
@ -478,7 +478,7 @@ namespace MS.Internal
ValidateToken(parameterAndValue.Slice(0, equalSignIndex).ToString()), ValidateToken(parameterAndValue.Slice(0, equalSignIndex).ToString()),
ValidateQuotedStringOrToken(parameterAndValue.Slice(parameterStartIndex, parameterValueLength).ToString())); ValidateQuotedStringOrToken(parameterAndValue.Slice(parameterStartIndex, parameterValueLength).ToString()));
parameterAndValue = parameterAndValue.Slice(parameterStartIndex + parameterValueLength).TrimStart(LinearWhiteSpaceChars); parameterAndValue = parameterAndValue.Slice(parameterStartIndex + parameterValueLength).TrimStart(_linearWhiteSpaceChars);
} }
} }
@ -501,7 +501,7 @@ namespace MS.Internal
if (semicolonIndex != -1) 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 = lwsIndex != -1 && lwsIndex < semicolonIndex ? lwsIndex : semicolonIndex;
length += startIndex; // the indexes from IndexOf{Any} are based on slicing from startIndex length += startIndex; // the indexes from IndexOf{Any} are based on slicing from startIndex
} }
@ -635,7 +635,7 @@ namespace MS.Internal
/// </summary> /// </summary>
/// <param name="ch">input character</param> /// <param name="ch">input character</param>
/// <returns></returns> /// <returns></returns>
private static bool IsLinearWhiteSpaceChar(char ch) => LinearWhiteSpaceChars.Contains(ch); private static bool IsLinearWhiteSpaceChar(char ch) => new ReadOnlySpan<char>(_linearWhiteSpaceChars).Contains(ch);
/// <summary> /// <summary>
/// Lazy initialization for the ParameterDictionary /// Lazy initialization for the ParameterDictionary
@ -678,7 +678,7 @@ namespace MS.Internal
]; ];
//Linear White Space characters //Linear White Space characters
private static ReadOnlySpan<char> LinearWhiteSpaceChars => [ private static readonly char[] _linearWhiteSpaceChars = [
' ', // space - \x20 ' ', // space - \x20
'\n', // new line - \x0A '\n', // new line - \x0A
'\r', // carriage return - \x0D '\r', // carriage return - \x0D