mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Replace Substring(...).Trim() with SubstringTrim(...)
System.Net.WebHeaderCollection already had a SubstringTrim helper that's used instead of Substring(...).Trim() to avoid first creating the substring and then trimming it, instead just offsetting the startIndex and length appropriately to do the Substring once. I've factored this helper out into Common, added unit tests, cleaned it up a bit, and used it in several more places in System.Net. Commit migrated from https://github.com/dotnet/corefx/commit/4e45e641c9d97c3a5d3b6fec4f6ffc33a2405fee
This commit is contained in:
1 parent
e89a783d4b
commit
ba797defff
15 files changed
+132
-44
No files matched your search
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace System
|
||||
{
|
||||
internal static class StringExtensions
|
||||
{
|
||||
internal static string SubstringTrim(this string value, int startIndex)
|
||||
{
|
||||
return SubstringTrim(value, startIndex, value.Length - startIndex);
|
||||
}
|
||||
|
||||
internal static string SubstringTrim(this string value, int startIndex, int length)
|
||||
{
|
||||
Debug.Assert(value != null, "string must be non-null");
|
||||
Debug.Assert(startIndex >= 0, "startIndex must be non-negative");
|
||||
Debug.Assert(length >= 0, "length must be non-negative");
|
||||
Debug.Assert(startIndex <= value.Length - length, "startIndex + length must be <= value.Length");
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int endIndex = startIndex + length - 1;
|
||||
|
||||
while (startIndex <= endIndex && char.IsWhiteSpace(value[startIndex]))
|
||||
{
|
||||
startIndex++;
|
||||
}
|
||||
|
||||
while (endIndex >= startIndex && char.IsWhiteSpace(value[endIndex]))
|
||||
{
|
||||
endIndex--;
|
||||
}
|
||||
|
||||
int newLength = endIndex - startIndex + 1;
|
||||
Debug.Assert(newLength >= 0 && newLength <= value.Length, "Expected resulting length to be within value's length");
|
||||
|
||||
return
|
||||
newLength == 0 ? string.Empty :
|
||||
newLength == value.Length ? value :
|
||||
value.Substring(startIndex, newLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,9 @@
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\src\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\src\System\IO\PathInternal.cs">
|
||||
<Link>Common\System\IO\PathInternal.cs</Link>
|
||||
</Compile>
|
||||
@@ -41,6 +44,7 @@
|
||||
<Compile Include="..\src\System\IO\TaskHelpers.cs">
|
||||
<Link>Common\System\IO\TaskHelpers.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Tests\System\StringExtensions.Tests.cs" />
|
||||
<Compile Include="Tests\System\IO\PathInternal.Tests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="'$(TargetsWindows)'=='true'">
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Tests.System
|
||||
{
|
||||
public class StringExtensionTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("", 0, 0, "")]
|
||||
[InlineData(" ", 0, 0, "")]
|
||||
[InlineData(" ", 0, 2, "")]
|
||||
[InlineData(" ", 1, 1, "")]
|
||||
[InlineData("hello", 1, 0, "")]
|
||||
[InlineData("hello", 0, 5, "hello")]
|
||||
[InlineData("hello", 1, 4, "ello")]
|
||||
[InlineData("hello", 1, 3, "ell")]
|
||||
[InlineData("hello", 2, 1, "l")]
|
||||
[InlineData("hello", 4, 1, "o")]
|
||||
[InlineData(" hello\t\t ", 0, 9, "hello")]
|
||||
[InlineData(" hello\r\t ", 1, 5, "hello")]
|
||||
[InlineData(" hello\t\n ", 6, 3, "")]
|
||||
[InlineData(" hello\t\n ", 8, 1, "")]
|
||||
[InlineData(" hello\t\n ", 9, 0, "")]
|
||||
public void SubstringTrim_VariousInputsOutputs(string source, int startIndex, int length, string expectedResult)
|
||||
{
|
||||
Action<string> validate = result =>
|
||||
{
|
||||
Assert.Equal(expectedResult, result);
|
||||
if (result.Length == 0)
|
||||
{
|
||||
Assert.Same(string.Empty, result);
|
||||
}
|
||||
else if (result.Length == source.Length)
|
||||
{
|
||||
Assert.Same(source, result);
|
||||
}
|
||||
};
|
||||
|
||||
validate(source.SubstringTrim(startIndex, length));
|
||||
|
||||
if (length == source.Length - startIndex)
|
||||
{
|
||||
validate(source.SubstringTrim(startIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -14,6 +14,7 @@
|
||||
<CompileItem Include="$(CommonPath)\Interop\Windows\winhttp\Interop.winhttp_types.cs" />
|
||||
<CompileItem Include="$(CommonPath)\Interop\Windows\winhttp\Interop.winhttp.cs" />
|
||||
<CompileItem Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs" />
|
||||
<CompileItem Include="$(CommonPath)\System\StringExtensions.cs" />
|
||||
<CompileItem Include="$(CommonPath)\System\Net\HttpVersion.cs" />
|
||||
<CompileItem Include="$(CommonPath)\System\Net\UriScheme.cs" />
|
||||
<CompileItem Include="$(CommonPath)\System\Net\Http\HttpHandlerDefaults.cs" />
|
||||
|
||||
+1
-1
@@ -198,7 +198,7 @@ namespace System.Net.Http
|
||||
if (colonIndex > 0)
|
||||
{
|
||||
string headerName = responseHeaderArray[i].Substring(0, colonIndex);
|
||||
string headerValue = responseHeaderArray[i].Substring(colonIndex + 1).Trim(); // Normalize header value by trimming white space.
|
||||
string headerValue = responseHeaderArray[i].SubstringTrim(colonIndex + 1); // Normalize header value by trimming white space.
|
||||
|
||||
if (!response.Headers.TryAddWithoutValidation(headerName, headerValue))
|
||||
{
|
||||
|
||||
+3
@@ -41,6 +41,9 @@
|
||||
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs">
|
||||
<Link>Common\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpVersion.cs">
|
||||
<Link>Common\System\Net\HttpVersion.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -303,11 +303,9 @@ namespace System.Net.Mail
|
||||
|
||||
Debug.Assert(index < 0 || data[index] == MailBnfHelper.Comma, "Mis-alligned index: " + index);
|
||||
|
||||
// Do not include the Comma (if any)
|
||||
displayName = data.Substring(index + 1, startingIndex - index);
|
||||
|
||||
// Because there were no bounding quotes, trim extra whitespace
|
||||
displayName = displayName.Trim();
|
||||
// Do not include the Comma (if any), and because there were no bounding quotes,
|
||||
// trim extra whitespace.
|
||||
displayName = data.SubstringTrim(index + 1, startingIndex - index);
|
||||
}
|
||||
return NormalizeOrThrow(displayName);
|
||||
}
|
||||
|
||||
@@ -217,6 +217,9 @@
|
||||
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeInteriorHandle.cs">
|
||||
<Link>Common\Microsoft\Win32\SafeHandles\SafeInteriorHandle.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpVersion.cs">
|
||||
<Link>Common\System\Net\HttpVersion.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -565,7 +565,7 @@ namespace System.Net.Http
|
||||
if (colonIndex > 0)
|
||||
{
|
||||
string headerName = responseHeader.Substring(0, colonIndex);
|
||||
string headerValue = responseHeader.Substring(colonIndex + 1).Trim();
|
||||
string headerValue = responseHeader.SubstringTrim(colonIndex + 1);
|
||||
|
||||
if (!response.Headers.TryAddWithoutValidation(headerName, headerValue))
|
||||
{
|
||||
|
||||
@@ -25,6 +25,9 @@
|
||||
<Compile Include="$(CommonPath)\System\NotImplemented.cs">
|
||||
<Link>ProductionCode\Common\System\NotImplemented.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>ProductionCode\Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonTestPath)\System\ShouldNotBeInvokedException.cs">
|
||||
<Link>Common\System\ShouldNotBeInvokedException.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -55,6 +55,9 @@
|
||||
<Compile Include="System\Security\Authentication\ExtendedProtection\ChannelBinding.cs" />
|
||||
<Compile Include="System\Security\Authentication\ExtendedProtection\ChannelBindingKind.cs" />
|
||||
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\ByteOrder.cs" >
|
||||
<Link>Common\System\Net\ByteOrder.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -1009,7 +1009,7 @@ namespace System.Net
|
||||
// Gets the full string of the cookie
|
||||
internal string GetCookieString()
|
||||
{
|
||||
return _tokenStream.Substring(_cookieStartIndex, _cookieLength).Trim();
|
||||
return _tokenStream.SubstringTrim(_cookieStartIndex, _cookieLength);
|
||||
}
|
||||
|
||||
// Extract
|
||||
@@ -1021,11 +1021,9 @@ namespace System.Net
|
||||
|
||||
if (_tokenLength != 0)
|
||||
{
|
||||
tokenString = _tokenStream.Substring(_start, _tokenLength);
|
||||
if (!Quoted)
|
||||
{
|
||||
tokenString = tokenString.Trim();
|
||||
}
|
||||
tokenString = Quoted ?
|
||||
_tokenStream.Substring(_start, _tokenLength) :
|
||||
_tokenStream.SubstringTrim(_start, _tokenLength);
|
||||
}
|
||||
return tokenString;
|
||||
}
|
||||
|
||||
+3
@@ -71,6 +71,9 @@
|
||||
<Compile Include="..\..\src\System\Net\Sockets\SocketError.cs" >
|
||||
<Link>ProductionCode\System\Net\Sockets\SocketError.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\ByteOrder.cs" >
|
||||
<Link>ProductionCode\Common\System\Net\ByteOrder.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
<Compile Include="System\Net\HttpRequestHeader.cs" />
|
||||
<Compile Include="System\Net\HeaderInfo.cs" />
|
||||
<Compile Include="System\Net\HeaderInfoTable.cs" />
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpKnownHeaderNames.cs">
|
||||
<Link>Common\System\Net\HttpKnownHeaderNames.cs</Link>
|
||||
</Compile>
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace System.Net
|
||||
}
|
||||
else if ((value[i] == ',') && !inquote)
|
||||
{
|
||||
tempStringCollection.Add(SubstringTrim(value, startIndex, length));
|
||||
tempStringCollection.Add(value.SubstringTrim(startIndex, length));
|
||||
startIndex = i + 1;
|
||||
length = 0;
|
||||
continue;
|
||||
@@ -43,7 +43,7 @@ namespace System.Net
|
||||
// Now add the last of the header values to the stringtable.
|
||||
if (startIndex < value.Length && length > 0)
|
||||
{
|
||||
tempStringCollection.Add(SubstringTrim(value, startIndex, length));
|
||||
tempStringCollection.Add(value.SubstringTrim(startIndex, length));
|
||||
}
|
||||
|
||||
return tempStringCollection.ToArray();
|
||||
@@ -105,35 +105,6 @@ namespace System.Net
|
||||
{ HttpKnownHeaderNames.WWWAuthenticate, new HeaderInfo(HttpKnownHeaderNames.WWWAuthenticate, false, true, true, s_singleParser) }
|
||||
};
|
||||
|
||||
private static string SubstringTrim(string value, int startIndex, int length)
|
||||
{
|
||||
int offset = 0;
|
||||
while (offset < length && char.IsWhiteSpace(value[startIndex + offset]))
|
||||
{
|
||||
offset++;
|
||||
}
|
||||
|
||||
int end = length - 1;
|
||||
while (end >= offset && char.IsWhiteSpace(value[startIndex + end]))
|
||||
{
|
||||
end--;
|
||||
}
|
||||
|
||||
int newLength = end - offset + 1;
|
||||
if (newLength == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int newStartIndex = startIndex + offset;
|
||||
if (newStartIndex == 0 && newLength == value.Length)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return value.Substring(newStartIndex, newLength);
|
||||
}
|
||||
|
||||
internal static HeaderInfo GetKnownHeaderInfo(string name)
|
||||
{
|
||||
HeaderInfo tempHeaderInfo;
|
||||
|
||||
Reference in new issue
Block a user