Skip to content

Commit

Permalink
,
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 18, 2024
1 parent 5575e13 commit 6ac3225
Show file tree
Hide file tree
Showing 2 changed files with 386 additions and 1 deletion.
385 changes: 385 additions & 0 deletions src/Polyfill/Polyfill_TryFormatToByteSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
// <auto-generated />

using System.Text;

#pragma warning disable

namespace Polyfills;

using System;
using System.Diagnostics.CodeAnalysis;

static partial class Polyfill
{
#if FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X)

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timespan.tryformat#system-timespan-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this TimeSpan target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] ReadOnlySpan<char> format = default, IFormatProvider? formatProvider = null)
{
string result;

if (format.Length == 0)
{
result = target.ToString(null, formatProvider);
}
else
{
result = target.ToString(format.ToString(), formatProvider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryformat#system-guid-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char)))
public static bool TryFormat(this Guid target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.GuidFormat)] ReadOnlySpan<char> format = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(null);
}
else
{
result = target.ToString(format.ToString());
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.sbyte.tryformat#system-sbyte-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this sbyte target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryformat#system-byte-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this byte target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int16.tryformat#system-int16-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this short target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.uint16.tryformat#system-uint16-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this ushort target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat#system-int32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this int target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat#system-uint32-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this uint target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryformat#system-int64-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this long target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat#system-uint64-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this ulong target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat#system-single-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this float target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat#system-double-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this double target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat#system-decimal-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this decimal target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

#endif

#if (FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X)) || NET6_0
/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryformat#system-datetimeoffset-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this DateTimeOffset target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryformat#system-datetime-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this DateTime target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

#endif
#if NET6_0

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.dateonly.tryformat#system-dateonly-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this DateOnly target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}

/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this TimeOnly target, Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;

if (format.Length == 0)
{
result = target.ToString(provider);
}
else
{
result = target.ToString(format.ToString(), provider);
}

return CopyToSpan(utf8Destination, out bytesWritten, result);
}
#endif

#if NET6_0 || (FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X))
static bool CopyToSpan(Span<byte> destination, out int bytesWritten, string result)
{
if (result.Length == 0)
{
bytesWritten = 0;
return true;
}

var bytes = Encoding.UTF8.GetBytes(result);

if (bytes.Length > destination.Length)
{
bytesWritten = 0;
return false;
}

bytes.CopyTo(destination);
bytesWritten = bytes.Length;
return true;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public static bool TryFormat(this DateOnly target, Span<char> destination, out i
/// <summary>
/// Tries to format the value of the current instance into the provided span of characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat#system-timeonly-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)
public static bool TryFormat(this TimeOnly target, Span<char> destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
string result;
Expand Down

0 comments on commit 6ac3225

Please sign in to comment.