Move primitives to ComputeSharp.Core
このコミットが含まれているのは:
1 親
cc298770ca
コミット
e48949cd48
78ファイル変更
+369
-245
検索条件に一致するファイルはありません
@@ -0,0 +1,315 @@
|
||||
// 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;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a handler used by the language compiler to process interpolated strings into <see cref="string"/> instances.
|
||||
/// </summary>
|
||||
[InterpolatedStringHandler]
|
||||
internal ref struct FormatInterpolatedStringHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Minimum size array to rent from the pool.
|
||||
/// </summary>
|
||||
private const int MinimumArrayPoolLength = 256;
|
||||
|
||||
/// <summary>
|
||||
/// The format to use to print individual values to the target buffer.
|
||||
/// </summary>
|
||||
private readonly string? format;
|
||||
|
||||
/// <summary>
|
||||
/// Optional provider to pass to <see cref="IFormattable.ToString(string?, IFormatProvider?)"/> calls.
|
||||
/// </summary>
|
||||
private readonly IFormatProvider? formatProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Array rented from the array pool and used to back <see cref="characters"/>.
|
||||
/// </summary>
|
||||
private char[]? arrayToReturnToPool;
|
||||
|
||||
/// <summary>
|
||||
/// The span to write into.
|
||||
/// </summary>
|
||||
private Span<char> characters;
|
||||
|
||||
/// <summary>
|
||||
/// Position at which to write the next character.
|
||||
/// </summary>
|
||||
private int position;
|
||||
|
||||
/// <summary>
|
||||
/// Whether <see cref="formatProvider"/> provides an <see cref="ICustomFormatter"/>.
|
||||
/// </summary>
|
||||
private readonly bool hasCustomFormatter;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a handler used to translate an interpolated string into a <see cref="string"/>.
|
||||
/// </summary>
|
||||
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
|
||||
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
|
||||
/// <param name="format">The format to use, if available.</param>
|
||||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <param name="initialBuffer">A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten.</param>
|
||||
/// <remarks>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.</remarks>
|
||||
public FormatInterpolatedStringHandler(int literalLength, int formattedCount, string? format, IFormatProvider? formatProvider, Span<char> initialBuffer)
|
||||
{
|
||||
this.format = format;
|
||||
this.formatProvider = formatProvider;
|
||||
characters = initialBuffer;
|
||||
arrayToReturnToPool = null;
|
||||
position = 0;
|
||||
hasCustomFormatter = formatProvider is not null && HasCustomFormatter(formatProvider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="string"/> with the specified arguments.
|
||||
/// </summary>
|
||||
/// <param name="format">The format to use, if available.</param>
|
||||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <param name="initialBuffer">A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten.</param>
|
||||
/// <param name="handler">The input <see cref="FormatInterpolatedStringHandler"/> instance responsible for formatting the resulting <see cref="string"/>.</param>
|
||||
/// <returns>The resulting formatted <see cref="string"/> instance.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string Create(
|
||||
string? format,
|
||||
IFormatProvider? formatProvider,
|
||||
Span<char> initialBuffer,
|
||||
[InterpolatedStringHandlerArgument("format", "formatProvider", "initialBuffer")] ref FormatInterpolatedStringHandler handler)
|
||||
{
|
||||
return handler.ToStringAndClear();
|
||||
}
|
||||
|
||||
/// <summary>Gets the built <see cref="string"/> and clears the handler.</summary>
|
||||
/// <returns>The built string.</returns>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
public string ToStringAndClear()
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
string result = new(characters.Slice(0, position));
|
||||
#else
|
||||
string result = characters.Slice(0, position).ToString();
|
||||
#endif
|
||||
char[]? toReturn = arrayToReturnToPool;
|
||||
|
||||
this = default;
|
||||
|
||||
if (toReturn is not null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(toReturn);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The string to write.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void AppendLiteral(string value)
|
||||
{
|
||||
if (value.Length == 1)
|
||||
{
|
||||
Span<char> chars = characters;
|
||||
int pos = position;
|
||||
|
||||
if ((uint)pos < (uint)chars.Length)
|
||||
{
|
||||
chars[pos] = value[0];
|
||||
|
||||
position = pos + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowThenCopyString(value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.Length == 2)
|
||||
{
|
||||
Span<char> chars = characters;
|
||||
int pos = position;
|
||||
|
||||
if ((uint)pos < chars.Length - 1)
|
||||
{
|
||||
Unsafe.WriteUnaligned(
|
||||
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos)),
|
||||
#if NET6_0_OR_GREATER
|
||||
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref Unsafe.AsRef(in value.GetPinnableReference()))));
|
||||
#else
|
||||
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(value.AsSpan()))));
|
||||
#endif
|
||||
|
||||
position = pos + 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowThenCopyString(value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
AppendStringDirect(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The string to write.</param>
|
||||
private void AppendStringDirect(string value)
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
if (value.TryCopyTo(characters.Slice(position)))
|
||||
#else
|
||||
if (value.AsSpan().TryCopyTo(characters.Slice(position)))
|
||||
#endif
|
||||
{
|
||||
position += value.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowThenCopyString(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified value to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public void AppendFormatted(string value)
|
||||
{
|
||||
AppendLiteral(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified value to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public void AppendFormatted<T>(T value)
|
||||
#if NET6_0_OR_GREATER
|
||||
where T : ISpanFormattable
|
||||
#else
|
||||
where T : IFormattable
|
||||
#endif
|
||||
{
|
||||
if (hasCustomFormatter)
|
||||
{
|
||||
AppendCustomFormatter(value, format);
|
||||
return;
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
int charsWritten;
|
||||
|
||||
while (!value.TryFormat(characters.Slice(position), out charsWritten, format, formatProvider))
|
||||
{
|
||||
Grow();
|
||||
}
|
||||
|
||||
position += charsWritten;
|
||||
#else
|
||||
AppendStringDirect(value.ToString(format, formatProvider));
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the provider provides a custom formatter.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static bool HasCustomFormatter(IFormatProvider provider)
|
||||
{
|
||||
return
|
||||
provider.GetType() != typeof(CultureInfo) &&
|
||||
provider.GetFormat(typeof(ICustomFormatter)) != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats the value using the custom formatter from the provider.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
/// <param name="format">The format string.</param>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void AppendCustomFormatter<T>(T value, string? format)
|
||||
{
|
||||
ICustomFormatter? formatter = (ICustomFormatter?)formatProvider?.GetFormat(typeof(ICustomFormatter));
|
||||
|
||||
if (formatter is not null && formatter.Format(format, value, formatProvider) is string customFormatted)
|
||||
{
|
||||
AppendStringDirect(customFormatted);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fallback for fast path in <see cref="AppendStringDirect"/> when there's not enough space in the destination.
|
||||
/// </summary>
|
||||
/// <param name="value">The string to write.</param>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void GrowThenCopyString(string value)
|
||||
{
|
||||
Grow(value.Length);
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
value.CopyTo(characters.Slice(position));
|
||||
#else
|
||||
value.AsSpan().CopyTo(characters.Slice(position));
|
||||
#endif
|
||||
|
||||
position += value.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grows <see cref="characters"/> to have the capacity to store at least <paramref name="additionalChars"/> beyond <see cref="position"/>.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void Grow(int additionalChars)
|
||||
{
|
||||
GrowCore((uint)position + (uint)additionalChars);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grows the size of <see cref="characters"/>.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void Grow()
|
||||
{
|
||||
GrowCore((uint)characters.Length + 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grow the size of <see cref="characters"/> to at least the specified <paramref name="requiredMinCapacity"/>.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void GrowCore(uint requiredMinCapacity)
|
||||
{
|
||||
uint newCapacity = Math.Max(requiredMinCapacity, Math.Min((uint)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<char>.Shared.Rent(arraySize);
|
||||
|
||||
characters.Slice(0, position).CopyTo(newArray);
|
||||
|
||||
char[]? toReturn = arrayToReturnToPool;
|
||||
|
||||
characters = arrayToReturnToPool = newArray;
|
||||
|
||||
if (toReturn is not null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(toReturn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a handler used by the language compiler to format interpolated strings into character spans.
|
||||
/// </summary>
|
||||
[InterpolatedStringHandler]
|
||||
internal ref struct TryWriteFormatInterpolatedStringHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// The destination buffer.
|
||||
/// </summary>
|
||||
private readonly Span<char> destination;
|
||||
|
||||
/// <summary>
|
||||
/// The format to use to print individual values to the target buffer.
|
||||
/// </summary>
|
||||
private readonly ReadOnlySpan<char> format;
|
||||
|
||||
/// <summary>
|
||||
/// The value of <see cref="format"/>, as a <see cref="string"/>.
|
||||
/// </summary>
|
||||
private string? formatAsString;
|
||||
|
||||
/// <summary>
|
||||
/// Optional provider to pass to <see cref="IFormattable.ToString(string?, IFormatProvider?)"/> calls.
|
||||
/// </summary>
|
||||
private readonly IFormatProvider? formatProvider;
|
||||
|
||||
/// <summary>
|
||||
/// The number of characters written to <see cref="destination"/>.
|
||||
/// </summary>
|
||||
internal int position;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether all formatting operations have succeeded.
|
||||
/// </summary>
|
||||
internal bool success;
|
||||
|
||||
/// <summary>
|
||||
/// Whether <see cref="formatProvider"/> provides an <see cref="ICustomFormatter"/>.
|
||||
/// </summary>
|
||||
private readonly bool hasCustomFormatter;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a handler used to write an interpolated string into a <see cref="Span{Char}"/>.
|
||||
/// </summary>
|
||||
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
|
||||
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
|
||||
/// <param name="destination">The destination buffer.</param>
|
||||
/// <param name="format">The format to use, if available.</param>
|
||||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <param name="shouldAppend">Upon return, true if the destination may be long enough to support the formatting, or false if it won't be.</param>
|
||||
/// <remarks>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.</remarks>
|
||||
public TryWriteFormatInterpolatedStringHandler(int literalLength, int formattedCount, Span<char> destination, ReadOnlySpan<char> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified interpolated string to the character span.
|
||||
/// </summary>
|
||||
/// <param name="destination">The span to which the interpolated string should be formatted.</param>
|
||||
/// <param name="format">The format to use, if available.</param>
|
||||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <param name="handler">The interpolated string.</param>
|
||||
/// <param name="charsWritten">The number of characters written to the span.</param>
|
||||
/// <returns>true if the entire interpolated string could be formatted successfully; otherwise, false.</returns>
|
||||
public static bool TryWrite(
|
||||
Span<char> destination,
|
||||
ReadOnlySpan<char> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified <see cref="string"/> to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="string"/> to write.</param>
|
||||
/// <returns>Whether the value could be formatted to the span.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AppendLiteral(string value)
|
||||
{
|
||||
if (value.Length == 1)
|
||||
{
|
||||
Span<char> 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<char> destination = this.destination;
|
||||
int pos = position;
|
||||
|
||||
if ((uint)pos < destination.Length - 1)
|
||||
{
|
||||
Unsafe.WriteUnaligned(
|
||||
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(destination), pos)),
|
||||
#if NET6_0_OR_GREATER
|
||||
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref Unsafe.AsRef(in value.GetPinnableReference()))));
|
||||
#else
|
||||
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(value.AsSpan()))));
|
||||
#endif
|
||||
|
||||
position = pos + 2;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return Fail();
|
||||
}
|
||||
|
||||
return AppendStringDirect(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The string to write.</param>
|
||||
/// <returns>Whether the value could be appended to the span.</returns>
|
||||
private bool AppendStringDirect(string value)
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
if (value.TryCopyTo(destination.Slice(position)))
|
||||
#else
|
||||
if (value.AsSpan().TryCopyTo(destination.Slice(position)))
|
||||
#endif
|
||||
{
|
||||
position += value.Length;
|
||||
return true;
|
||||
}
|
||||
|
||||
return Fail();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified value to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public bool AppendFormatted<T>(T value)
|
||||
#if NET6_0_OR_GREATER
|
||||
where T : ISpanFormattable
|
||||
#else
|
||||
where T : IFormattable
|
||||
#endif
|
||||
{
|
||||
if (hasCustomFormatter)
|
||||
{
|
||||
return AppendCustomFormatter(value, formatAsString ??= format.ToString());
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
int charsWritten;
|
||||
|
||||
if (value.TryFormat(destination.Slice(position), out charsWritten, format, formatProvider))
|
||||
{
|
||||
position += charsWritten;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return Fail();
|
||||
#else
|
||||
return AppendStringDirect(value.ToString(formatAsString ??= format.ToString(), formatProvider));
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified value to the handler.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public bool AppendFormatted(string? value)
|
||||
{
|
||||
if (hasCustomFormatter)
|
||||
{
|
||||
return AppendCustomFormatter(value, format: null);
|
||||
}
|
||||
|
||||
if (value is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
if (value.TryCopyTo(destination.Slice(position)))
|
||||
#else
|
||||
if (value.AsSpan().TryCopyTo(destination.Slice(position)))
|
||||
#endif
|
||||
{
|
||||
position += value.Length;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return Fail();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats the value using the custom formatter from the provider.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to write.</param>
|
||||
/// <param name="format">The format string.</param>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private bool AppendCustomFormatter<T>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks formatting as having failed and returns false.
|
||||
/// </summary>
|
||||
private bool Fail()
|
||||
{
|
||||
success = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする