diff --git a/src/ComputeSharp.Core/Helpers/FormatInterpolatedStringHandler.cs b/src/ComputeSharp.Core/Helpers/FormatInterpolatedStringHandler.cs deleted file mode 100644 index 1edcaf7e..00000000 --- a/src/ComputeSharp.Core/Helpers/FormatInterpolatedStringHandler.cs +++ /dev/null @@ -1,319 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Buffers; -using System.Globalization; -using System.Runtime.InteropServices; - -namespace System.Runtime.CompilerServices; - -/// -/// Provides a handler used by the language compiler to process interpolated strings into instances. -/// -[InterpolatedStringHandler] -internal ref struct FormatInterpolatedStringHandler -{ - /// - /// Minimum size array to rent from the pool. - /// - private const int MinimumArrayPoolLength = 256; - - /// - /// The format to use to print individual values to the target buffer. - /// - private readonly string? format; - - /// - /// Optional provider to pass to calls. - /// - private readonly IFormatProvider? formatProvider; - - /// - /// Array rented from the array pool and used to back . - /// - private char[]? arrayToReturnToPool; - - /// - /// The span to write into. - /// - private Span characters; - - /// - /// Position at which to write the next character. - /// - private int position; - - /// - /// Whether provides an . - /// - private readonly bool hasCustomFormatter; - - /// - /// Creates a handler used to translate 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 format to use, if available. - /// An object that supplies culture-specific formatting information. - /// A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten. - /// 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 FormatInterpolatedStringHandler(int literalLength, int formattedCount, string? format, IFormatProvider? formatProvider, Span initialBuffer) - { - this.format = format; - this.formatProvider = formatProvider; - this.characters = initialBuffer; - this.arrayToReturnToPool = null; - this.position = 0; - this.hasCustomFormatter = formatProvider is not null && HasCustomFormatter(formatProvider); - } - - /// - /// Creates a new with the specified arguments. - /// - /// The format to use, if available. - /// An object that supplies culture-specific formatting information. - /// A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten. - /// The input instance responsible for formatting the resulting . - /// The resulting formatted instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Create( - string? format, - IFormatProvider? formatProvider, - Span initialBuffer, - [InterpolatedStringHandlerArgument("format", "formatProvider", "initialBuffer")] ref FormatInterpolatedStringHandler handler) - { - return handler.ToStringAndClear(); - } - - /// Gets the built and clears the handler. - /// The built string. - /// - /// This releases any resources used by the handler. The method should be invoked only - /// once and as the last thing performed on the handler. Subsequent use is erroneous, ill-defined, - /// and may destabilize the process, as may using any other copies of the handler after ToStringAndClear - /// is called on any one of them. - /// - public string ToStringAndClear() - { -#if NET6_0_OR_GREATER - string result = new(this.characters.Slice(0, this.position)); -#else - string result = this.characters.Slice(0, this.position).ToString(); -#endif - char[]? toReturn = this.arrayToReturnToPool; - - this = default; - - if (toReturn is not null) - { - ArrayPool.Shared.Return(toReturn); - } - - return result; - } - - /// - /// Writes the specified string to the handler. - /// - /// The string to write. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLiteral(string value) - { - if (value.Length == 1) - { - Span chars = this.characters; - int pos = this.position; - - if ((uint)pos < (uint)chars.Length) - { - chars[pos] = value[0]; - - this.position = pos + 1; - } - else - { - GrowThenCopyString(value); - } - - return; - } - - if (value.Length == 2) - { - Span chars = this.characters; - int pos = this.position; - - if ((uint)pos < chars.Length - 1) - { - Unsafe.WriteUnaligned( - ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos)), -#if NET6_0_OR_GREATER - Unsafe.ReadUnaligned(ref Unsafe.As(ref Unsafe.AsRef(in value.GetPinnableReference())))); -#else - Unsafe.ReadUnaligned(ref Unsafe.As(ref MemoryMarshal.GetReference(value.AsSpan())))); -#endif - - this.position = pos + 2; - } - else - { - GrowThenCopyString(value); - } - - return; - } - - AppendStringDirect(value); - } - - /// - /// Writes the specified string to the handler. - /// - /// The string to write. - private void AppendStringDirect(string value) - { -#if NET6_0_OR_GREATER - if (value.TryCopyTo(this.characters.Slice(this.position))) -#else - if (value.AsSpan().TryCopyTo(this.characters.Slice(this.position))) -#endif - { - this.position += value.Length; - } - else - { - GrowThenCopyString(value); - } - } - - /// - /// Writes the specified value to the handler. - /// - /// The value to write. - public void AppendFormatted(string value) - { - AppendLiteral(value); - } - - /// - /// Writes the specified value to the handler. - /// - /// The value to write. - public void AppendFormatted(T value) -#if NET6_0_OR_GREATER - where T : ISpanFormattable -#else - where T : IFormattable -#endif - { - if (this.hasCustomFormatter) - { - AppendCustomFormatter(value, this.format); - return; - } - -#if NET6_0_OR_GREATER - int charsWritten; - - while (!value.TryFormat(this.characters.Slice(this.position), out charsWritten, this.format, this.formatProvider)) - { - Grow(); - } - - this.position += charsWritten; -#else - AppendStringDirect(value.ToString(this.format, this.formatProvider)); -#endif - } - - /// - /// Gets whether the provider provides a custom formatter. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static bool HasCustomFormatter(IFormatProvider provider) - { - return - provider.GetType() != typeof(CultureInfo) && - provider.GetFormat(typeof(ICustomFormatter)) != null; - } - - /// - /// Formats the value using the custom formatter from the provider. - /// - /// The value to write. - /// The format string. - [MethodImpl(MethodImplOptions.NoInlining)] - private void AppendCustomFormatter(T value, string? format) - { - ICustomFormatter? formatter = (ICustomFormatter?)this.formatProvider?.GetFormat(typeof(ICustomFormatter)); - - if (formatter is not null && formatter.Format(format, value, this.formatProvider) is string customFormatted) - { - AppendStringDirect(customFormatted); - } - } - - /// - /// Fallback for fast path in when there's not enough space in the destination. - /// - /// The string to write. - [MethodImpl(MethodImplOptions.NoInlining)] - private void GrowThenCopyString(string value) - { - Grow(value.Length); - -#if NET6_0_OR_GREATER - value.CopyTo(this.characters.Slice(this.position)); -#else - value.AsSpan().CopyTo(this.characters.Slice(this.position)); -#endif - - this.position += value.Length; - } - - /// - /// Grows to have the capacity to store at least beyond . - /// - [MethodImpl(MethodImplOptions.NoInlining)] - private void Grow(int additionalChars) - { - GrowCore((uint)this.position + (uint)additionalChars); - } - -#if NET6_0_OR_GREATER - /// - /// Grows the size of . - /// - [MethodImpl(MethodImplOptions.NoInlining)] - private void Grow() - { - GrowCore((uint)this.characters.Length + 1); - } -#endif - - /// - /// Grow the size of to at least the specified . - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void GrowCore(uint requiredMinCapacity) - { - uint newCapacity = Math.Max(requiredMinCapacity, Math.Min((uint)this.characters.Length * 2, 0x3FFFFFDF)); -#if NET6_0_OR_GREATER - int arraySize = (int)Math.Clamp(newCapacity, MinimumArrayPoolLength, int.MaxValue); -#else - int arraySize = (int)Math.Min(Math.Max(newCapacity, MinimumArrayPoolLength), int.MaxValue); -#endif - char[] newArray = ArrayPool.Shared.Rent(arraySize); - - this.characters.Slice(0, this.position).CopyTo(newArray); - - char[]? toReturn = this.arrayToReturnToPool; - - this.characters = this.arrayToReturnToPool = newArray; - - if (toReturn is not null) - { - ArrayPool.Shared.Return(toReturn); - } - } -} \ No newline at end of file