From caf3debade90be36c17a38f1e3ed854eca8fbd52 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sun, 3 Dec 2023 01:32:45 +0900 Subject: [PATCH] Add Utf8Formatter overloads for .NET 8 --- .../Utf8StringWriter.AppendFormatted.cs | 606 +++++++++++++++++- .../Utf8StringWriter.AppendFormatted.tt | 56 +- 2 files changed, 626 insertions(+), 36 deletions(-) diff --git a/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.cs b/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.cs index 8b29034..ae9fd72 100644 --- a/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.cs +++ b/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.cs @@ -7,7 +7,6 @@ namespace Utf8StringInterpolation; public ref partial struct Utf8StringWriter { #if true - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(bool? value, int alignment = 0, string? format = null) { @@ -27,7 +26,7 @@ public void AppendFormatted(bool value, int alignment = 0, string? format = null { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -94,7 +93,6 @@ void AppendFormattedAlignment(bool value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(byte? value, int alignment = 0, string? format = null) { @@ -114,7 +112,7 @@ public void AppendFormatted(byte value, int alignment = 0, string? format = null { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -181,7 +179,6 @@ void AppendFormattedAlignment(byte value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Decimal? value, int alignment = 0, string? format = null) { @@ -201,7 +198,7 @@ public void AppendFormatted(Decimal value, int alignment = 0, string? format = n { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -268,7 +265,6 @@ void AppendFormattedAlignment(Decimal value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Double? value, int alignment = 0, string? format = null) { @@ -288,7 +284,7 @@ public void AppendFormatted(Double value, int alignment = 0, string? format = nu { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -355,7 +351,6 @@ void AppendFormattedAlignment(Double value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Guid? value, int alignment = 0, string? format = null) { @@ -375,7 +370,7 @@ public void AppendFormatted(Guid value, int alignment = 0, string? format = null { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -442,7 +437,6 @@ void AppendFormattedAlignment(Guid value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Int16? value, int alignment = 0, string? format = null) { @@ -462,7 +456,7 @@ public void AppendFormatted(Int16 value, int alignment = 0, string? format = nul { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -529,7 +523,6 @@ void AppendFormattedAlignment(Int16 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Int32? value, int alignment = 0, string? format = null) { @@ -549,7 +542,7 @@ public void AppendFormatted(Int32 value, int alignment = 0, string? format = nul { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -616,7 +609,6 @@ void AppendFormattedAlignment(Int32 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Int64? value, int alignment = 0, string? format = null) { @@ -636,7 +628,7 @@ public void AppendFormatted(Int64 value, int alignment = 0, string? format = nul { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -703,7 +695,6 @@ void AppendFormattedAlignment(Int64 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(SByte? value, int alignment = 0, string? format = null) { @@ -723,7 +714,7 @@ public void AppendFormatted(SByte value, int alignment = 0, string? format = nul { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -790,7 +781,6 @@ void AppendFormattedAlignment(SByte value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(Single? value, int alignment = 0, string? format = null) { @@ -810,7 +800,7 @@ public void AppendFormatted(Single value, int alignment = 0, string? format = nu { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -877,7 +867,6 @@ void AppendFormattedAlignment(Single value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(UInt16? value, int alignment = 0, string? format = null) { @@ -897,7 +886,7 @@ public void AppendFormatted(UInt16 value, int alignment = 0, string? format = nu { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -964,7 +953,6 @@ void AppendFormattedAlignment(UInt16 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(UInt32? value, int alignment = 0, string? format = null) { @@ -984,7 +972,7 @@ public void AppendFormatted(UInt32 value, int alignment = 0, string? format = nu { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -1051,7 +1039,6 @@ void AppendFormattedAlignment(UInt32 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(UInt64? value, int alignment = 0, string? format = null) { @@ -1071,7 +1058,7 @@ public void AppendFormatted(UInt64 value, int alignment = 0, string? format = nu { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, StandardFormat.Parse(format))) { GrowCore(0); @@ -1138,7 +1125,6 @@ void AppendFormattedAlignment(UInt64 value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(DateTime? value, int alignment = 0, string? format = null) { @@ -1158,7 +1144,7 @@ public void AppendFormatted(DateTime value, int alignment = 0, string? format = { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!value.TryFormat(destination, out bytesWritten, format, formatProvider)) { GrowCore(0); @@ -1225,7 +1211,6 @@ void AppendFormattedAlignment(DateTime value, int alignment, string? format) #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(DateTimeOffset? value, int alignment = 0, string? format = null) { @@ -1245,7 +1230,7 @@ public void AppendFormatted(DateTimeOffset value, int alignment = 0, string? for { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!value.TryFormat(destination, out bytesWritten, format, formatProvider)) { GrowCore(0); @@ -1312,7 +1297,6 @@ void AppendFormattedAlignment(DateTimeOffset value, int alignment, string? forma #endif #if !NET8_0_OR_GREATER - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(TimeSpan? value, int alignment = 0, string? format = null) { @@ -1332,7 +1316,7 @@ public void AppendFormatted(TimeSpan value, int alignment = 0, string? format = { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!value.TryFormat(destination, out bytesWritten, format, formatProvider)) { GrowCore(0); @@ -1399,7 +1383,6 @@ void AppendFormattedAlignment(TimeSpan value, int alignment, string? format) #endif #if true - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(char? value, int alignment = 0, string? format = null) { @@ -1419,7 +1402,7 @@ public void AppendFormatted(char value, int alignment = 0, string? format = null { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!value.TryFormat(destination, out bytesWritten, format, formatProvider)) { GrowCore(0); @@ -1486,6 +1469,417 @@ void AppendFormattedAlignment(char value, int alignment, string? format) #endif +#if NET8_0_OR_GREATER + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(byte? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(byte value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Decimal? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Decimal value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Double? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Double value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Guid? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Guid value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int16? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int16 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int32? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int32 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int64? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Int64 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(SByte? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(SByte value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Single? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(Single value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt16? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt16 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt32? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt32 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt64? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(UInt64 value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + +#endif + // `if typeof(T) == typeof()` will eliminate in JIT. public void AppendFormatted(T value, int alignment = 0, string? format = null) { @@ -1558,6 +1952,150 @@ public void AppendFormatted(T value, int alignment = 0, string? format = null { AppendFormatted(Unsafe.As(ref value), alignment, format); } +#else + else if (typeof(T) == typeof(bool)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(byte)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Decimal)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Double)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Guid)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Int16)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Int32)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Int64)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(SByte)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(Single)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(UInt16)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(UInt32)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + else if (typeof(T) == typeof(UInt64)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } #endif else { diff --git a/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.tt b/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.tt index 4085e11..1cb3726 100644 --- a/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.tt +++ b/src/Utf8StringInterpolation/Utf8StringWriter.AppendFormatted.tt @@ -30,7 +30,6 @@ public ref partial struct Utf8StringWriter { <# foreach(var x in generateTypes) { #> <#= (x.type is not "bool" and not "char") ? "#if !NET8_0_OR_GREATER" : "#if true" #> - [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AppendFormatted(<#= x.type #>? value, int alignment = 0, string? format = null) { @@ -50,7 +49,7 @@ public ref partial struct Utf8StringWriter { if (alignment == 0) { - var bytesWritten = 0; + int bytesWritten; while (!<#= x.format1 #>) { GrowCore(0); @@ -118,6 +117,45 @@ public ref partial struct Utf8StringWriter <# } #> +#if NET8_0_OR_GREATER +<# foreach(var x in utf8FormatterTypes.Where(x => x is not "bool" and not "char")) { #> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(<#= x #>? value, int alignment = 0, string? format = null) + { + if (value == null) + { + if (alignment != 0) + { + AppendFormatted("", alignment); // write only alignment + } + return; + } + AppendFormatted(value.Value, alignment, format); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AppendFormatted(<#= x #> value, int alignment = 0, string? format = null) + { + if (alignment == 0 && format == null) + { + int bytesWritten; + while (!Utf8Formatter.TryFormat(value, destination, out bytesWritten, default)) + { + GrowCore(0); + } + destination = destination.Slice(bytesWritten); + currentWritten += bytesWritten; + return; + } + else + { + AppendFormattedCore(value, alignment, format); + } + } + +<# } #> +#endif + // `if typeof(T) == typeof()` will eliminate in JIT. public void AppendFormatted(T value, int alignment = 0, string? format = null) { @@ -136,6 +174,20 @@ public ref partial struct Utf8StringWriter AppendFormatted(Unsafe.As>(ref value), alignment, format); } <# } #> +#else +<# foreach(var x in utf8FormatterTypes) { #> + else if (typeof(T) == typeof(<#= x #>)) + { + if (alignment == 0 && format == null) + { + AppendFormatted(Unsafe.As>(ref value)); + } + else + { + AppendFormattedCore(value, alignment, format); + } + } +<# } #> #endif else {