diff --git a/apiCount.include.md b/apiCount.include.md index 00492ad3..155e0e80 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 416** \ No newline at end of file +**API count: 433** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index a9ec1fd2..1c0d34a9 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -7,6 +7,7 @@ #### byte + * `bool TryFormat(byte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(byte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryformat#system-byte-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -39,6 +40,7 @@ #### DateOnly + * `bool TryFormat(DateOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.dateonly.tryformat#system-dateonly-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -47,6 +49,7 @@ * `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds) * `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond) * `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond) + * `bool TryFormat(DateTime, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateTime, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryformat#system-datetime-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -55,11 +58,13 @@ * `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds) * `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond) * `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond) + * `bool TryFormat(DateTimeOffset, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateTimeOffset, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryformat#system-datetimeoffset-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### decimal + * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat#system-decimal-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -71,6 +76,7 @@ #### double + * `bool TryFormat(double, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(double, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat#system-double-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -97,11 +103,13 @@ #### float + * `bool TryFormat(float, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(float, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat#system-single-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### Guid + * `bool TryFormat(Guid, Span, int, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryformat#system-guid-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char)))) * `bool TryFormat(Guid, Span, int, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryformat#system-guid-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char)))) @@ -181,6 +189,7 @@ #### int + * `bool TryFormat(int, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(int, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat#system-int32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -204,6 +213,7 @@ #### long + * `bool TryFormat(long, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(long, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryformat#system-int64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -270,11 +280,13 @@ #### sbyte + * `bool TryFormat(sbyte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(sbyte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.sbyte.tryformat#system-sbyte-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### short + * `bool TryFormat(short, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(short, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int16.tryformat#system-int16-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -383,13 +395,15 @@ #### TimeOnly - * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat) + * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) + * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) #### TimeSpan * `int Microseconds(TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.microseconds) * `int Nanoseconds(TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.nanoseconds) + * `bool TryFormat(TimeSpan, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(TimeSpan, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.tryformat#system-timespan-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -404,16 +418,19 @@ #### uint + * `bool TryFormat(uint, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(uint, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat#system-uint32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### ulong + * `bool TryFormat(ulong, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(ulong, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat#system-uint64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### ushort + * `bool TryFormat(ushort, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(ushort, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint16.tryformat#system-uint16-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) diff --git a/readme.md b/readme.md index 13b7abff..7049b94a 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru * `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0` -**API count: 416** +**API count: 433** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -458,6 +458,7 @@ The class `Polyfill` includes the following extension methods: #### byte + * `bool TryFormat(byte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(byte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryformat#system-byte-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -490,6 +491,7 @@ The class `Polyfill` includes the following extension methods: #### DateOnly + * `bool TryFormat(DateOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.dateonly.tryformat#system-dateonly-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -498,6 +500,7 @@ The class `Polyfill` includes the following extension methods: * `DateTime AddMicroseconds(DateTime, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.addmicroseconds) * `int Microsecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.microsecond) * `int Nanosecond(DateTime)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.nanosecond) + * `bool TryFormat(DateTime, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateTime, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryformat#system-datetime-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -506,11 +509,13 @@ The class `Polyfill` includes the following extension methods: * `DateTimeOffset AddMicroseconds(DateTimeOffset, double)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.addmicroseconds) * `int Microsecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.microsecond) * `int Nanosecond(DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.nanosecond) + * `bool TryFormat(DateTimeOffset, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(DateTimeOffset, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryformat#system-datetimeoffset-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### decimal + * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat#system-decimal-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -522,6 +527,7 @@ The class `Polyfill` includes the following extension methods: #### double + * `bool TryFormat(double, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(double, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat#system-double-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -548,11 +554,13 @@ The class `Polyfill` includes the following extension methods: #### float + * `bool TryFormat(float, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(float, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.single.tryformat#system-single-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### Guid + * `bool TryFormat(Guid, Span, int, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryformat#system-guid-tryformat(system-span((system-byte))-system-int32@-system-readonlyspan((system-char)))) * `bool TryFormat(Guid, Span, int, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryformat#system-guid-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char)))) @@ -632,6 +640,7 @@ The class `Polyfill` includes the following extension methods: #### int + * `bool TryFormat(int, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(int, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryformat#system-int32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -655,6 +664,7 @@ The class `Polyfill` includes the following extension methods: #### long + * `bool TryFormat(long, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(long, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryformat#system-int64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -721,11 +731,13 @@ The class `Polyfill` includes the following extension methods: #### sbyte + * `bool TryFormat(sbyte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(sbyte, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.sbyte.tryformat#system-sbyte-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### short + * `bool TryFormat(short, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(short, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.int16.tryformat#system-int16-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -834,13 +846,15 @@ The class `Polyfill` includes the following extension methods: #### TimeOnly - * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly.tryformat) + * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) + * `bool TryFormat(TimeOnly, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) #### TimeSpan * `int Microseconds(TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.microseconds) * `int Nanoseconds(TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.nanoseconds) + * `bool TryFormat(TimeSpan, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(TimeSpan, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.tryformat#system-timespan-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) @@ -855,16 +869,19 @@ The class `Polyfill` includes the following extension methods: #### uint + * `bool TryFormat(uint, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(uint, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint32.tryformat#system-uint32-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### ulong + * `bool TryFormat(ulong, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(ulong, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint64.tryformat#system-uint64-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) #### ushort + * `bool TryFormat(ushort, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](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)) * `bool TryFormat(ushort, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.uint16.tryformat#system-uint16-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) diff --git a/src/Polyfill/Polyfill_TryFormatToByteSpan.cs b/src/Polyfill/Polyfill_TryFormatToByteSpan.cs new file mode 100644 index 00000000..693a81e5 --- /dev/null +++ b/src/Polyfill/Polyfill_TryFormatToByteSpan.cs @@ -0,0 +1,381 @@ +// + +#pragma warning disable + +#if FeatureMemory +namespace Polyfills; + +using System.Text; +using System; +using System.Diagnostics.CodeAnalysis; + +static partial class Polyfill +{ +#if !NET8_0_OR_GREATER + + static bool CopyToSpan(Span 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; + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.GuidFormat)] ReadOnlySpan format = default) + { + string result; + + if (format.Length == 0) + { + result = target.ToString(null); + } + else + { + result = target.ToString(format.ToString()); + } + + return CopyToSpan(utf8Destination, out bytesWritten, result); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan 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 || NET7_0 + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.DateOnlyFormat)] ReadOnlySpan 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); + } + + /// + /// Tries to format the value of the current instance into the provided span of characters. + /// + //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 utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan 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 +} +#endif \ No newline at end of file diff --git a/src/Polyfill/Polyfill_TryFormat.cs b/src/Polyfill/Polyfill_TryFormatToCharSpan.cs similarity index 98% rename from src/Polyfill/Polyfill_TryFormat.cs rename to src/Polyfill/Polyfill_TryFormatToCharSpan.cs index 6d7a19ed..631e3dfe 100644 --- a/src/Polyfill/Polyfill_TryFormat.cs +++ b/src/Polyfill/Polyfill_TryFormatToCharSpan.cs @@ -2,6 +2,7 @@ #pragma warning disable +#if FeatureMemory namespace Polyfills; using System; @@ -9,7 +10,25 @@ namespace Polyfills; static partial class Polyfill { -#if FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X) + static bool CopyToSpan(Span destination, out int charsWritten, string result) + { + if (result.Length == 0) + { + charsWritten = 0; + return true; + } + + if (result.Length > destination.Length) + { + charsWritten = 0; + return false; + } + + charsWritten = result.Length; + return result.TryCopyTo(destination); + } + +#if NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X /// /// Tries to format the value of the current instance into the provided span of characters. @@ -282,7 +301,7 @@ public static bool TryFormat(this bool target, Span destination, out int c } #endif -#if (FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X)) || NET6_0 +#if (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X) || NET6_0 /// /// Tries to format the value of the current instance into the provided span of characters. /// @@ -349,7 +368,7 @@ public static bool TryFormat(this DateOnly target, Span destination, out i /// /// Tries to format the value of the current instance into the provided span of characters. /// - //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 destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.TimeOnlyFormat)] ReadOnlySpan format = default, IFormatProvider? provider = default) { string result; @@ -366,24 +385,5 @@ public static bool TryFormat(this TimeOnly target, Span destination, out i return CopyToSpan(destination, out charsWritten, result); } #endif - -#if NET6_0 || (FeatureMemory && (NETFRAMEWORK || NETSTANDARD || NETCOREAPP2X)) - static bool CopyToSpan(Span destination, out int charsWritten, string result) - { - if (result.Length == 0) - { - charsWritten = 0; - return true; - } - - if (result.Length > destination.Length) - { - charsWritten = 0; - return false; - } - - charsWritten = result.Length; - return result.TryCopyTo(destination); - } -#endif -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Tests/PolyfillTests_TryFormatToByteSpan.cs b/src/Tests/PolyfillTests_TryFormatToByteSpan.cs new file mode 100644 index 00000000..293afff4 --- /dev/null +++ b/src/Tests/PolyfillTests_TryFormatToByteSpan.cs @@ -0,0 +1,534 @@ +using System.Globalization; + +partial class PolyfillTests +{ + [Test] + public void TryFormatSByte_ToByteSpan() + { + sbyte value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSByteSmaller_ToByteSpan() + { + sbyte value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSByteLarger_ToByteSpan() + { + sbyte value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatByte_ToByteSpan() + { + byte value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatByteSmaller_ToByteSpan() + { + byte value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatByteLarger_ToByteSpan() + { + byte value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatInt16_ToByteSpan() + { + short value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt16Smaller_ToByteSpan() + { + short value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt16Larger_ToByteSpan() + { + short value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatUInt16_ToByteSpan() + { + ushort value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt16Smaller_ToByteSpan() + { + ushort value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt16Larger_ToByteSpan() + { + ushort value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatInt32_ToByteSpan() + { + var value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt32Smaller_ToByteSpan() + { + var value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt32Larger_ToByteSpan() + { + var value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatUInt32_ToByteSpan() + { + uint value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt32Smaller_ToByteSpan() + { + uint value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt32Larger_ToByteSpan() + { + uint value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatInt64_ToByteSpan() + { + long value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt64Smaller_ToByteSpan() + { + long value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatInt64Larger_ToByteSpan() + { + long value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatUInt64_ToByteSpan() + { + ulong value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(buffer.Length, written); + } + + [Test] + public void TryFormatUInt64Smaller_ToByteSpan() + { + ulong value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatUInt64Larger_ToByteSpan() + { + ulong value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatSingle_ToByteSpan() + { + float value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSingleSmaller_ToByteSpan() + { + float value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatSingleLarger_ToByteSpan() + { + float value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatGuid_ToByteSpan() + { + var value = new Guid("97008c2d-2114-4396-ae19-392c8e6f8f1b"); + Span buffer = stackalloc byte[36]; + var result = value.TryFormat(buffer, out var written); + Assert.True(result); + Assert.AreEqual("97008c2d-2114-4396-ae19-392c8e6f8f1b", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(buffer.Length, written); + } + + [Test] + public void TryFormatGuidSmaller_ToByteSpan() + { + var value = new Guid("97008c2d-2114-4396-ae19-392c8e6f8f1b"); + Span buffer = stackalloc byte[40]; + var result = value.TryFormat(buffer, out var written); + Assert.True(result); + Assert.AreEqual("97008c2d-2114-4396-ae19-392c8e6f8f1b\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(36, written); + } + + [Test] + public void TryFormatGuidLarger_ToByteSpan() + { + var value = new Guid("97008c2d-2114-4396-ae19-392c8e6f8f1b"); + Span buffer = stackalloc byte[35]; + var result = value.TryFormat(buffer, out var written); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatDouble_ToByteSpan() + { + double value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDoubleSmaller_ToByteSpan() + { + double value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDoubleLarger_ToByteSpan() + { + double value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatDecimal_ToByteSpan() + { + decimal value = 9; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDecimalSmaller_ToByteSpan() + { + decimal value = 9; + Span buffer = stackalloc byte[2]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("9\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(1, written); + } + + [Test] + public void TryFormatDecimalLarger_ToByteSpan() + { + decimal value = 99; + Span buffer = stackalloc byte[1]; + var result = value.TryFormat(buffer, out var written, provider: CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatDateTimeOffset_ToByteSpan() + { + var value = new DateTimeOffset(new DateTime(2001, 10, 1), TimeSpan.Zero); + Span buffer = stackalloc byte[29]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeOffsetSmaller_ToByteSpan() + { + var value = new DateTimeOffset(new DateTime(2001, 10, 1), TimeSpan.Zero); + Span buffer = stackalloc byte[40]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeOffsetLarger_ToByteSpan() + { + var value = new DateTimeOffset(new DateTime(2001, 10, 1), TimeSpan.Zero); + Span buffer = stackalloc byte[28]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatDateTime_ToByteSpan() + { + var value = new DateTime(2001, 10, 1); + Span buffer = stackalloc byte[29]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeSmaller_ToByteSpan() + { + var value = new DateTime(2001, 10, 1); + Span buffer = stackalloc byte[40]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001 00:00:00 GMT\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(29, written); + } + + [Test] + public void TryFormatDateTimeLarger_ToByteSpan() + { + var value = new DateTime(2001, 10, 1); + Span buffer = stackalloc byte[28]; + var result = value.TryFormat(buffer, out var written, format: "R".AsSpan(), CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + +#if NET6_0_OR_GREATER + [Test] + public void TryFormatDate_ToByteSpan() + { + var value = new DateOnly(2001, 10, 1); + Span buffer = stackalloc byte[16]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(16, written); + } + + [Test] + public void TryFormatDateSmaller_ToByteSpan() + { + var value = new DateOnly(2001, 10, 1); + Span buffer = stackalloc byte[20]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("Mon, 01 Oct 2001\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(16, written); + } + + [Test] + public void TryFormatDateLarger_ToByteSpan() + { + var value = new DateOnly(2001, 10, 1); + Span buffer = stackalloc byte[15]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } + + [Test] + public void TryFormatTime_ToByteSpan() + { + var value = new TimeOnly(10, 1); + Span buffer = stackalloc byte[8]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("10:01:00", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(8, written); + } + + [Test] + public void TryFormatTimeSmaller_ToByteSpan() + { + var value = new TimeOnly(10, 1); + Span buffer = stackalloc byte[10]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.True(result); + Assert.AreEqual("10:01:00\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(8, written); + } + + [Test] + public void TryFormatTimeLarger_ToByteSpan() + { + var value = new TimeOnly(10, 1); + Span buffer = stackalloc byte[7]; + var result = value.TryFormat(buffer, out var written, format: "R", CultureInfo.InvariantCulture); + Assert.False(result); + Assert.AreEqual("\0\0\0\0\0\0\0", Encoding.UTF8.GetString(buffer)); + Assert.AreEqual(0, written); + } +#endif +} \ No newline at end of file diff --git a/src/Tests/PolyfillTests_TryFormat.cs b/src/Tests/PolyfillTests_TryFormatToCharSpan.cs similarity index 100% rename from src/Tests/PolyfillTests_TryFormat.cs rename to src/Tests/PolyfillTests_TryFormatToCharSpan.cs