diff --git a/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs b/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs index 9a16a49e..d890842d 100644 --- a/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs +++ b/src/ComputeSharp/Core/Helpers/FormatInterpolatedStringHandler.cs @@ -133,6 +133,7 @@ public ref struct FormatInterpolatedStringHandler if ((uint)pos < (uint)chars.Length) { chars[pos] = value[0]; + position = pos + 1; } else diff --git a/src/ComputeSharp/Core/Helpers/TryWriteFormatInterpolatedStringHandler.cs b/src/ComputeSharp/Core/Helpers/TryWriteFormatInterpolatedStringHandler.cs new file mode 100644 index 00000000..7f97ec73 --- /dev/null +++ b/src/ComputeSharp/Core/Helpers/TryWriteFormatInterpolatedStringHandler.cs @@ -0,0 +1,240 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; +using System.Runtime.InteropServices; + +namespace System.Runtime.CompilerServices; + +/// +/// Provides a handler used by the language compiler to format interpolated strings into character spans. +/// +[EditorBrowsable(EditorBrowsableState.Never)] +[InterpolatedStringHandler] +public ref struct TryWriteFormatInterpolatedStringHandler +{ + /// + /// The destination buffer. + /// + private readonly Span destination; + + /// + /// The format to use to print individual values to the target buffer. + /// + private readonly ReadOnlySpan format; + + /// + /// The value of , as a . + /// + private string? formatAsString; + + /// + /// Optional provider to pass to or calls. + /// + private readonly IFormatProvider? formatProvider; + + /// + /// The number of characters written to . + /// + internal int position; + + /// + /// Indicates whether all formatting operations have succeeded. + /// + internal bool success; + + /// + /// Whether provides an . + /// + private readonly bool hasCustomFormatter; + + /// + /// Creates a handler used to write an interpolated string into a . + /// + /// The number of constant characters outside of interpolation expressions in the interpolated string. + /// The number of interpolation expressions in the interpolated string. + /// The destination buffer. + /// The format to use, if available. + /// An object that supplies culture-specific formatting information. + /// Upon return, true if the destination may be long enough to support the formatting, or false if it won't be. + /// This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly. + public TryWriteFormatInterpolatedStringHandler(int literalLength, int formattedCount, Span destination, ReadOnlySpan format, IFormatProvider? formatProvider, out bool shouldAppend) + { + this.destination = destination; + this.format = format; + this.formatAsString = null; + this.formatProvider = formatProvider; + position = 0; + success = shouldAppend = destination.Length >= literalLength; + hasCustomFormatter = formatProvider is not null && FormatInterpolatedStringHandler.HasCustomFormatter(formatProvider); + } + + /// + /// Writes the specified interpolated string to the character span. + /// + /// The span to which the interpolated string should be formatted. + /// The format to use, if available. + /// An object that supplies culture-specific formatting information. + /// The interpolated string. + /// The number of characters written to the span. + /// true if the entire interpolated string could be formatted successfully; otherwise, false. + public static bool TryWrite( + Span destination, + ReadOnlySpan format, + IFormatProvider? formatProvider, + [InterpolatedStringHandlerArgument("destination", "format", "formatProvider")] ref TryWriteFormatInterpolatedStringHandler handler, + out int charsWritten) + { + if (handler.success) + { + charsWritten = handler.position; + + return true; + } + + charsWritten = 0; + + return false; + } + + /// + /// Writes the specified to the handler. + /// + /// The to write. + /// Whether the value could be formatted to the span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool AppendLiteral(string value) + { + if (value.Length == 1) + { + Span destination = this.destination; + int pos = position; + + if ((uint)pos < (uint)destination.Length) + { + destination[pos] = value[0]; + + position = pos + 1; + + return true; + } + + return Fail(); + } + + if (value.Length == 2) + { + Span destination = this.destination; + int pos = position; + + if ((uint)pos < destination.Length - 1) + { + Unsafe.WriteUnaligned( + ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(destination), pos)), + Unsafe.ReadUnaligned(ref Unsafe.As(ref Unsafe.AsRef(in value.GetPinnableReference())))); + + position = pos + 2; + + return true; + } + + return Fail(); + } + + return AppendStringDirect(value); + } + + /// + /// Writes the specified string to the handler. + /// + /// The string to write. + /// Whether the value could be appended to the span. + private bool AppendStringDirect(string value) + { + if (value.TryCopyTo(destination.Slice(position))) + { + position += value.Length; + return true; + } + + return Fail(); + } + + /// + /// Writes the specified value to the handler. + /// + /// The value to write. + public bool AppendFormatted(T value) + where T : ISpanFormattable + { + if (hasCustomFormatter) + { + return AppendCustomFormatter(value, formatAsString ??= format.ToString()); + } + + int charsWritten; + + if (value.TryFormat(destination.Slice(position), out charsWritten, format, formatProvider)) + { + position += charsWritten; + + return true; + } + + return Fail(); + } + + /// + /// Writes the specified value to the handler. + /// + /// The value to write. + public bool AppendFormatted(string? value) + { + if (hasCustomFormatter) + { + return AppendCustomFormatter(value, format: null); + } + + if (value is null) + { + return true; + } + + if (value.TryCopyTo(destination.Slice(position))) + { + position += value.Length; + + return true; + } + + return Fail(); + } + + /// + /// Formats the value using the custom formatter from the provider. + /// + /// The value to write. + /// The format string. + [MethodImpl(MethodImplOptions.NoInlining)] + private bool AppendCustomFormatter(T value, string? format) + { + ICustomFormatter? formatter = (ICustomFormatter?)formatProvider?.GetFormat(typeof(ICustomFormatter)); + + if (formatter is not null && formatter.Format(format, value, formatProvider) is string customFormatted) + { + return AppendStringDirect(customFormatted); + } + + return true; + } + + /// + /// Marks formatting as having failed and returns false. + /// + private bool Fail() + { + success = false; + + return false; + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs index 07d00f73..a32c2e3f 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double2.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)] public unsafe partial struct Double2 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -414,6 +414,19 @@ public unsafe partial struct Double2 $"<{this.x}{separator} {this.y}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs index 3e58733f..ff5ded3d 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double3.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 24, Pack = 8)] public unsafe partial struct Double3 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -1495,6 +1495,19 @@ public unsafe partial struct Double3 $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs b/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs index 7d59f9f6..ad947e14 100644 --- a/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Double/Double4.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 32, Pack = 8)] public unsafe partial struct Double4 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -4136,6 +4136,19 @@ public unsafe partial struct Double4 $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs index 66289283..ee6009e6 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float2.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] public unsafe partial struct Float2 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -414,6 +414,19 @@ public unsafe partial struct Float2 $"<{this.x}{separator} {this.y}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs index ea7003c9..1b966379 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float3.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] public unsafe partial struct Float3 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -1495,6 +1495,19 @@ public unsafe partial struct Float3 $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs b/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs index 1f7f084b..459f05d6 100644 --- a/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Float/Float4.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] public unsafe partial struct Float4 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -4136,6 +4136,19 @@ public unsafe partial struct Float4 $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs index 3d201d3f..c414ac78 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int2.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] public unsafe partial struct Int2 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -414,6 +414,19 @@ public unsafe partial struct Int2 $"<{this.x}{separator} {this.y}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs index cf8fa1a5..477d88f5 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int3.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] public unsafe partial struct Int3 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -1495,6 +1495,19 @@ public unsafe partial struct Int3 $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs b/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs index 14103406..328a21b5 100644 --- a/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs +++ b/src/ComputeSharp/Core/Primitives/Int/Int4.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] public unsafe partial struct Int4 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -4136,6 +4136,19 @@ public unsafe partial struct Int4 $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs index 698dfc83..4f9a8d65 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt2.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)] public unsafe partial struct UInt2 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -414,6 +414,19 @@ public unsafe partial struct UInt2 $"<{this.x}{separator} {this.y}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs index f05fb538..9defa766 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt3.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)] public unsafe partial struct UInt3 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -1495,6 +1495,19 @@ public unsafe partial struct UInt3 $"<{this.x}{separator} {this.y}{separator} {this.z}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs b/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs index 8e8e2d19..ed418f79 100644 --- a/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs +++ b/src/ComputeSharp/Core/Primitives/UInt/UInt4.g.cs @@ -15,7 +15,7 @@ namespace ComputeSharp; [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)] public unsafe partial struct UInt4 #if !SOURCE_GENERATOR - : IFormattable + : ISpanFormattable #endif { /// @@ -4136,6 +4136,19 @@ public unsafe partial struct UInt4 $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>"); } + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteFormatInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>", + out charsWritten); + } + #endif /// diff --git a/src/ComputeSharp/Core/Primitives/VectorType.ttinclude b/src/ComputeSharp/Core/Primitives/VectorType.ttinclude index 34939eef..9d474cf3 100644 --- a/src/ComputeSharp/Core/Primitives/VectorType.ttinclude +++ b/src/ComputeSharp/Core/Primitives/VectorType.ttinclude @@ -50,7 +50,7 @@ void GenerateVectorProperties(string typeName, int elementSize) string elementTypeName = Regex.Match(typeName, @"^[A-Za-z]+").Value; string hlslElementTypeName = elementTypeName.ToLower(); int i = int.Parse(Regex.Match(typeName, @"\d+$").Value); - string formattable = elementTypeName == "Bool" ? "" : "IFormattable"; + string formattable = elementTypeName == "Bool" ? "" : "ISpanFormattable"; if (elementTypeName != "Bool") { @@ -193,6 +193,19 @@ public unsafe partial struct <#=typeName#> stackalloc char[64], $"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>"); } + + /// + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) + { + string separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator; + + return TryWriteInterpolatedStringHandler.TryWrite( + destination, + format, + provider, + $"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>", + out charsWritten); + } <# }