diff --git a/src/libraries/Common/src/System/StringExtensions.cs b/src/libraries/Common/src/System/StringExtensions.cs new file mode 100644 index 00000000000..800791c2855 --- /dev/null +++ b/src/libraries/Common/src/System/StringExtensions.cs @@ -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); + } + } +} diff --git a/src/libraries/Common/tests/Common.Tests.csproj b/src/libraries/Common/tests/Common.Tests.csproj index 501632e6315..7d004a42434 100644 --- a/src/libraries/Common/tests/Common.Tests.csproj +++ b/src/libraries/Common/tests/Common.Tests.csproj @@ -26,6 +26,9 @@ + + Common\System\StringExtensions.cs + Common\System\IO\PathInternal.cs @@ -41,6 +44,7 @@ Common\System\IO\TaskHelpers.cs + diff --git a/src/libraries/Common/tests/Tests/System/StringExtensions.Tests.cs b/src/libraries/Common/tests/Tests/System/StringExtensions.Tests.cs new file mode 100644 index 00000000000..e40a9b3d929 --- /dev/null +++ b/src/libraries/Common/tests/Tests/System/StringExtensions.Tests.cs @@ -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 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)); + } + } + } +} diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.msbuild b/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.msbuild index 159d3de6508..650cafe9709 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.msbuild +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System.Net.Http.WinHttpHandler.msbuild @@ -14,6 +14,7 @@ + diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseParser.cs b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseParser.cs index f6029fb4a6c..ea251462c0f 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseParser.cs +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpResponseParser.cs @@ -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)) { diff --git a/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj b/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj index d33254f03b8..48f3d7c59a7 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj +++ b/src/libraries/System.Net.Http.WinHttpHandler/tests/UnitTests/System.Net.Http.WinHttpHandler.Unit.Tests.csproj @@ -41,6 +41,9 @@ Common\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs + + Common\System\StringExtensions.cs + Common\System\Net\HttpVersion.cs diff --git a/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs b/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs index bb91237a20a..cdcec2cd203 100644 --- a/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs +++ b/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs @@ -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); } diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index d99c621373a..9ef7898828e 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -217,6 +217,9 @@ Common\Microsoft\Win32\SafeHandles\SafeInteriorHandle.cs + + Common\System\StringExtensions.cs + Common\System\Net\HttpVersion.cs diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.MultiAgent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.MultiAgent.cs index a20a8499fa1..468426d2381 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.MultiAgent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.MultiAgent.cs @@ -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)) { diff --git a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj index 7292984ba0b..3fc107d798b 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj +++ b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj @@ -25,6 +25,9 @@ ProductionCode\Common\System\NotImplemented.cs + + ProductionCode\Common\System\StringExtensions.cs + Common\System\ShouldNotBeInvokedException.cs diff --git a/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj b/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj index 05d0e68a4dd..f27936f3123 100644 --- a/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj +++ b/src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj @@ -55,6 +55,9 @@ + + Common\System\StringExtensions.cs + Common\System\Net\ByteOrder.cs diff --git a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs index 9037bbd95aa..c342ff8a914 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs @@ -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; } diff --git a/src/libraries/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj b/src/libraries/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj index 5e342475d40..850a8ae4a64 100644 --- a/src/libraries/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj +++ b/src/libraries/System.Net.Primitives/tests/UnitTests/System.Net.Primitives.UnitTests.Tests.csproj @@ -71,6 +71,9 @@ ProductionCode\System\Net\Sockets\SocketError.cs + + Common\System\StringExtensions.cs + ProductionCode\Common\System\Net\ByteOrder.cs diff --git a/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj b/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj index e98b9758fc7..6dd01c522f7 100644 --- a/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj +++ b/src/libraries/System.Net.WebHeaderCollection/src/System.Net.WebHeaderCollection.csproj @@ -24,6 +24,9 @@ + + Common\System\StringExtensions.cs + Common\System\Net\HttpKnownHeaderNames.cs diff --git a/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs b/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs index fac6a05bb6c..af0cdf06d98 100644 --- a/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs +++ b/src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs @@ -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;