From 1575ed9a9e0386b0e6ea1c336bcc760a0218f2d6 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 31 May 2023 21:04:59 -0400 Subject: [PATCH] Use IUtf8SpanFormattable.TryFormat in a few more places (#86931) * 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 --------- Co-authored-by: Eirik Tsarpalis --- .../System/Net/Http/SocketsHttpHandler/HttpConnection.cs | 9 ++++++--- .../Serialization/Converters/Value/DateOnlyConverter.cs | 8 ++++++++ .../Serialization/Converters/Value/VersionConverter.cs | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs index 76f904d79b203..d79d7233f3907 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs @@ -390,7 +390,7 @@ void WriteHost(Uri requestUri) _writeBuffer.EnsureAvailableSpace(6); Span 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); } @@ -1493,7 +1493,7 @@ private async ValueTask FlushThenWriteWithoutBufferingAsync(ReadOnlyMemory 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; @@ -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 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 { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DateOnlyConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DateOnlyConverter.cs index 16b4e5e82ec6d..9917f47fcf8db 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DateOnlyConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DateOnlyConverter.cs @@ -57,7 +57,11 @@ private static DateOnly ReadCore(ref Utf8JsonReader reader) public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) { +#if NET8_0_OR_GREATER + Span buffer = stackalloc byte[FormatLength]; +#else Span buffer = stackalloc char[FormatLength]; +#endif bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture); Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); writer.WriteStringValue(buffer); @@ -65,7 +69,11 @@ public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializer internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { +#if NET8_0_OR_GREATER + Span buffer = stackalloc byte[FormatLength]; +#else Span buffer = stackalloc char[FormatLength]; +#endif bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture); Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); writer.WritePropertyName(buffer); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs index c90ee22ad2856..ed07581ded9af 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/VersionConverter.cs @@ -85,7 +85,11 @@ public override void Write(Utf8JsonWriter writer, Version? value, JsonSerializer } #if NETCOREAPP +#if NET8_0_OR_GREATER + Span span = stackalloc byte[MaximumVersionLength]; +#else Span span = stackalloc char[MaximumVersionLength]; +#endif bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength); writer.WriteStringValue(span.Slice(0, charsWritten)); @@ -107,7 +111,11 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Version va } #if NETCOREAPP +#if NET8_0_OR_GREATER + Span span = stackalloc byte[MaximumVersionLength]; +#else Span span = stackalloc char[MaximumVersionLength]; +#endif bool formattedSuccessfully = value.TryFormat(span, out int charsWritten); Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength); writer.WritePropertyName(span.Slice(0, charsWritten));