Skip to content

Commit

Permalink
Use IUtf8SpanFormattable.TryFormat in a few more places (#86931)
Browse files Browse the repository at this point in the history
* Use IUtf8SpanFormattable.TryFormat in a few more places

* Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs

* Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>

---------

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
  • Loading branch information
stephentoub and eiriktsarpalis committed Jun 1, 2023
1 parent a4dd36d commit 1575ed9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ void WriteHost(Uri requestUri)
_writeBuffer.EnsureAvailableSpace(6);
Span<byte> buffer = _writeBuffer.AvailableSpan;
buffer[0] = (byte)':';
bool success = Utf8Formatter.TryFormat(requestUri.Port, buffer.Slice(1), out int bytesWritten);
bool success = ((uint)requestUri.Port).TryFormat(buffer.Slice(1), out int bytesWritten);
Debug.Assert(success);
_writeBuffer.Commit(bytesWritten + 1);
}
Expand Down Expand Up @@ -1493,7 +1493,7 @@ private async ValueTask FlushThenWriteWithoutBufferingAsync(ReadOnlyMemory<byte>
private ValueTask WriteHexInt32Async(int value, bool async)
{
// Try to format into our output buffer directly.
if (Utf8Formatter.TryFormat(value, _writeBuffer.AvailableSpan, out int bytesWritten, 'X'))
if (value.TryFormat(_writeBuffer.AvailableSpan, out int bytesWritten, "X"))
{
_writeBuffer.Commit(bytesWritten);
return default;
Expand All @@ -1502,7 +1502,10 @@ private ValueTask WriteHexInt32Async(int value, bool async)
// If we don't have enough room, do it the slow way.
if (async)
{
return WriteAsync(Encoding.ASCII.GetBytes(value.ToString("X", CultureInfo.InvariantCulture)));
Span<byte> temp = stackalloc byte[8]; // max length of Int32 as hex
bool formatted = value.TryFormat(temp, out bytesWritten, "X");
Debug.Assert(formatted);
return WriteAsync(temp.Slice(0, bytesWritten).ToArray());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ private static DateOnly ReadCore(ref Utf8JsonReader reader)

public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
#if NET8_0_OR_GREATER
Span<byte> buffer = stackalloc byte[FormatLength];
#else
Span<char> buffer = stackalloc char[FormatLength];
#endif
bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture);
Debug.Assert(formattedSuccessfully && charsWritten == FormatLength);
writer.WriteStringValue(buffer);
}

internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options, bool isWritingExtensionDataProperty)
{
#if NET8_0_OR_GREATER
Span<byte> buffer = stackalloc byte[FormatLength];
#else
Span<char> buffer = stackalloc char[FormatLength];
#endif
bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture);
Debug.Assert(formattedSuccessfully && charsWritten == FormatLength);
writer.WritePropertyName(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ public override void Write(Utf8JsonWriter writer, Version? value, JsonSerializer
}

#if NETCOREAPP
#if NET8_0_OR_GREATER
Span<byte> span = stackalloc byte[MaximumVersionLength];
#else
Span<char> span = stackalloc char[MaximumVersionLength];
#endif
bool formattedSuccessfully = value.TryFormat(span, out int charsWritten);
Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength);
writer.WriteStringValue(span.Slice(0, charsWritten));
Expand All @@ -107,7 +111,11 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Version va
}

#if NETCOREAPP
#if NET8_0_OR_GREATER
Span<byte> span = stackalloc byte[MaximumVersionLength];
#else
Span<char> span = stackalloc char[MaximumVersionLength];
#endif
bool formattedSuccessfully = value.TryFormat(span, out int charsWritten);
Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength);
writer.WritePropertyName(span.Slice(0, charsWritten));
Expand Down

0 comments on commit 1575ed9

Please sign in to comment.