Skip to content

Commit 24e3d41

Browse files
committed
ISpanFormattable
1 parent 032569f commit 24e3d41

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: TypeForwardedTo(typeof(ISpanFormattable))]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace System
2+
{
3+
/// <summary>Provides functionality to format the string representation of an object into a span.</summary>
4+
public interface ISpanFormattable : IFormattable
5+
{
6+
/// <summary>Tries to format the value of the current instance into the provided span of characters.</summary>
7+
/// <param name="destination">When this method returns, this instance's value formatted as a span of characters.</param>
8+
/// <param name="charsWritten">When this method returns, the number of characters that were written in <paramref name="destination"/>.</param>
9+
/// <param name="format">A span containing the characters that represent a standard or custom format string that defines the acceptable format for <paramref name="destination"/>.</param>
10+
/// <param name="provider">An optional object that supplies culture-specific formatting information for <paramref name="destination"/>.</param>
11+
/// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</returns>
12+
/// <remarks>
13+
/// An implementation of this interface should produce the same string of characters as an implementation of <see cref="IFormattable.ToString(string?, IFormatProvider?)"/>
14+
/// on the same type.
15+
/// TryFormat should return false only if there is not enough space in the destination buffer. Any other failures should throw an exception.
16+
/// </remarks>
17+
bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider);
18+
}
19+
}

src/MonoMod.Backports/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler,is_std,is_fx,lt_core_6.0.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,17 @@ public void AppendFormatted<T>(T value)
317317
string? s;
318318
if (value is IFormattable)
319319
{
320-
// If the value can format itself directly into our buffer, do so.
321-
/*if (value is ISpanFormattable) {
322-
int charsWritten;
323-
while (!((ISpanFormattable) value).TryFormat(_chars.Slice(_pos), out charsWritten, default, _provider)) // constrained call avoiding boxing for value types
324-
{
325-
Grow();
326-
}
327-
328-
_pos += charsWritten;
329-
return;
330-
}*/
320+
// If the value can format itself directly into our buffer, do so.
321+
if (value is ISpanFormattable) {
322+
int charsWritten;
323+
while (!((ISpanFormattable) value).TryFormat(_chars.Slice(_pos), out charsWritten, default, _provider)) // constrained call avoiding boxing for value types
324+
{
325+
Grow();
326+
}
327+
328+
_pos += charsWritten;
329+
return;
330+
}
331331

332332
s = ((IFormattable)value).ToString(format: null, _provider); // constrained call avoiding boxing for value types
333333
}
@@ -376,7 +376,7 @@ public void AppendFormatted<T>(T value, string? format)
376376
if (value is IFormattable)
377377
{
378378
// If the value can format itself directly into our buffer, do so.
379-
/*if (value is ISpanFormattable) {
379+
if (value is ISpanFormattable) {
380380
int charsWritten;
381381
while (!((ISpanFormattable) value).TryFormat(_chars.Slice(_pos), out charsWritten, format, _provider)) // constrained call avoiding boxing for value types
382382
{
@@ -385,7 +385,7 @@ public void AppendFormatted<T>(T value, string? format)
385385

386386
_pos += charsWritten;
387387
return;
388-
}*/
388+
}
389389

390390
s = ((IFormattable)value).ToString(format, _provider); // constrained call avoiding boxing for value types
391391
}

0 commit comments

Comments
 (0)