// 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,
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 :
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();
CheckResponseMsgFormat(headerName.Length > 0);
index++;
- headerValue = _span.Substring(index).Trim().ToString();
+ headerValue = _span.Slice(index).Trim().ToString();
return true;
}
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);
}
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);
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))
{
}
// 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.
return false;
}
- string dateString = value;
+ ReadOnlySpan<char> dateString = value;
if (index > 0)
{
- dateString = value.Substring(index);
+ dateString = value.AsSpan(index);
}
DateTimeOffset date;
}
else
{
- if (!HttpDateParser.TryStringToDate(input.Substring(current), out date))
+ if (!HttpDateParser.TryStringToDate(input.AsSpan(current), out date))
{
return 0;
}
}
else
{
- if (!HttpDateParser.TryStringToDate(input.Substring(current), out date))
+ if (!HttpDateParser.TryStringToDate(input.AsSpan(current), out date))
{
return 0;
}
}
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;
}
DateTimeOffset temp;
- if (!HttpDateParser.TryStringToDate(input.Substring(dateStartIndex, current - dateStartIndex), out temp))
+ if (!HttpDateParser.TryStringToDate(input.AsSpan(dateStartIndex, current - dateStartIndex), out temp))
{
return false;
}