Remove some unnecessary Substrings in System.Net.Http (dotnet/corefx#34356)
authorStephen Toub <stoub@microsoft.com>
Sat, 5 Jan 2019 18:45:29 +0000 (13:45 -0500)
committerGitHub <noreply@github.com>
Sat, 5 Jan 2019 18:45:29 +0000 (13:45 -0500)
Commit migrated from https://github.com/dotnet/corefx/commit/bcd20ce6fa0c7580ac4976cb50747687d07d96b2

src/libraries/Common/src/System/Net/HttpDateParser.cs
src/libraries/System.Net.Http/src/System/Net/Http/CurlHandler/CurlResponseHeaderReader.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentDispositionHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/DateHeaderParser.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeConditionHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/RetryConditionHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/StringWithQualityHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/WarningHeaderValue.cs

index 4ed23f6..4cfd78e 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Net
 
         // Try the various date formats in the order listed above.
         // We should accept a wide variety of common formats, but only output RFC 1123 style dates.
-        internal static bool TryStringToDate(string input, out DateTimeOffset result) =>
+        internal static bool TryStringToDate(ReadOnlySpan<char> input, out DateTimeOffset result) =>
              DateTimeOffset.TryParseExact(
                  input,
                  s_dateFormats,
index c3a7637..474e5d0 100644 (file)
@@ -66,7 +66,7 @@ namespace System.Net.Http
             response.StatusCode = (HttpStatusCode)statusCode;
 
             // Try to use a known reason phrase instead of allocating a new string.
-            HeaderBufferSpan reasonPhraseSpan = _span.Substring(index);
+            HeaderBufferSpan reasonPhraseSpan = _span.Slice(index);
             string knownReasonPhrase = HttpStatusDescription.Get(response.StatusCode);
             response.ReasonPhrase = reasonPhraseSpan.EqualsOrdinal(knownReasonPhrase) ?
                 knownReasonPhrase :
@@ -95,7 +95,7 @@ namespace System.Net.Http
 
                 CheckResponseMsgFormat(index < _span.Length);
                 CheckResponseMsgFormat(_span[index] == ':');
-                HeaderBufferSpan headerNameSpan = _span.Substring(0, headerNameLength);
+                HeaderBufferSpan headerNameSpan = _span.Slice(0, headerNameLength);
                 if (!HttpKnownHeaderNames.TryGetHeaderName(headerNameSpan.Buffer, headerNameSpan.Length, out headerName))
                 {
                     headerName = headerNameSpan.ToString();
@@ -103,7 +103,7 @@ namespace System.Net.Http
                 CheckResponseMsgFormat(headerName.Length > 0);
 
                 index++;
-                headerValue = _span.Substring(index).Trim().ToString();
+                headerValue = _span.Slice(index).Trim().ToString();
                 return true;
             }
 
@@ -268,12 +268,12 @@ namespace System.Net.Http
                 return true;
             }
 
-            public HeaderBufferSpan Substring(int startIndex)
+            public HeaderBufferSpan Slice(int startIndex)
             {
-                return Substring(startIndex, Length - startIndex);
+                return Slice(startIndex, Length - startIndex);
             }
 
-            public HeaderBufferSpan Substring(int startIndex, int length)
+            public HeaderBufferSpan Slice(int startIndex, int length)
             {
                 Debug.Assert(startIndex >= 0);
                 Debug.Assert(length >= 0);
index fadc33d..d0c50b1 100644 (file)
@@ -148,7 +148,7 @@ namespace System.Net.Http.Headers
             }
             result._scheme = targetScheme != null && string.CompareOrdinal(input, startIndex, targetScheme, 0, schemeLength) == 0 ?
                 targetScheme :
-                result._scheme = input.Substring(startIndex, schemeLength);
+                input.Substring(startIndex, schemeLength);
 
             int current = startIndex + schemeLength;
             int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
index 883019a..de711e9 100644 (file)
@@ -318,11 +318,11 @@ namespace System.Net.Http.Headers
             DateTimeOffset date;
             if (dateParameter != null)
             {
-                string dateString = dateParameter.Value;
+                ReadOnlySpan<char> dateString = dateParameter.Value;
                 // Should have quotes, remove them.
                 if (IsQuoted(dateString))
                 {
-                    dateString = dateString.Substring(1, dateString.Length - 2);
+                    dateString = dateString.Slice(1, dateString.Length - 2);
                 }
                 if (HttpDateParser.TryStringToDate(dateString, out date))
                 {
@@ -460,12 +460,12 @@ namespace System.Net.Http.Headers
         }
 
         // Returns true if the value starts and ends with a quote.
-        private bool IsQuoted(string value)
+        private bool IsQuoted(ReadOnlySpan<char> value)
         {
-            Debug.Assert(value != null);
-
-            return value.Length > 1 && value.StartsWith("\"", StringComparison.Ordinal)
-                && value.EndsWith("\"", StringComparison.Ordinal);
+            return
+                value.Length > 1 &&
+                value[0] == '"' &&
+                value[value.Length - 1] == '"';
         }
 
         // tspecials are required to be in a quoted string.  Only non-ascii needs to be encoded.
index d7fe674..6e5077b 100644 (file)
@@ -34,10 +34,10 @@ namespace System.Net.Http.Headers
                 return false;
             }
 
-            string dateString = value;
+            ReadOnlySpan<char> dateString = value;
             if (index > 0)
             {
-                dateString = value.Substring(index);
+                dateString = value.AsSpan(index);
             }
 
             DateTimeOffset date;
index 41b6e8b..7bb55e3 100644 (file)
@@ -154,7 +154,7 @@ namespace System.Net.Http.Headers
             }
             else
             {
-                if (!HttpDateParser.TryStringToDate(input.Substring(current), out date))
+                if (!HttpDateParser.TryStringToDate(input.AsSpan(current), out date))
                 {
                     return 0;
                 }
index a530c63..81822d9 100644 (file)
@@ -155,7 +155,7 @@ namespace System.Net.Http.Headers
             }
             else
             {
-                if (!HttpDateParser.TryStringToDate(input.Substring(current), out date))
+                if (!HttpDateParser.TryStringToDate(input.AsSpan(current), out date))
                 {
                     return 0;
                 }
index 6b96f46..4a2ba83 100644 (file)
@@ -203,7 +203,7 @@ namespace System.Net.Http.Headers
             }
 
             double quality = 0;
-            if (!double.TryParse(input.Substring(current, qualityLength), NumberStyles.AllowDecimalPoint,
+            if (!double.TryParse(input.AsSpan(current, qualityLength), NumberStyles.AllowDecimalPoint,
                 NumberFormatInfo.InvariantInfo, out quality))
             {
                 return false;
index ecb04ae..821915d 100644 (file)
@@ -301,7 +301,7 @@ namespace System.Net.Http.Headers
                 }
 
                 DateTimeOffset temp;
-                if (!HttpDateParser.TryStringToDate(input.Substring(dateStartIndex, current - dateStartIndex), out temp))
+                if (!HttpDateParser.TryStringToDate(input.AsSpan(dateStartIndex, current - dateStartIndex), out temp))
                 {
                     return false;
                 }