-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5575e13
commit 6ac3225
Showing
2 changed files
with
386 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters