mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 15:55:04 -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
@@ -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