Skip to content

Commit

Permalink
expose TextWriter Write StringBuilder when no memory ref (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Sep 14, 2024
1 parent 555c5ff commit ea8be86
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
28 changes: 17 additions & 11 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ static partial class Polyfill

#endif

#if FeatureMemory
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Equivalent to Write(stringBuilder.ToString()) however it uses the
Expand All @@ -240,16 +239,21 @@ static partial class Polyfill
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder)")]
public static void Write(this TextWriter target, StringBuilder? value)
{
if (value != null)
if (value == null)
{
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
return;
}

#if FeatureMemory
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
#else
target.Write(value.ToString());
#endif
}

#if FeatureValueTask
/// <summary>
/// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
Expand All @@ -273,16 +277,19 @@ static partial class Polyfill

async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel)
{
#if FeatureValueTask && FeatureMemory
foreach (ReadOnlyMemory<char> chunk in builder.GetChunks())
{
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
#else
await target.WriteAsync(builder.ToString());
#endif
}
}
#endif
#endif

#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
#if (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) && FeatureMemory
#if FeatureValueTask

/// <summary>
Expand Down Expand Up @@ -385,10 +392,9 @@ static partial class Polyfill
}
}
#endif
#endif
}
```
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L201' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L207' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: 338**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 339**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -806,6 +806,7 @@ The class `Polyfill` includes the following extension methods:

#### TextWriter

* `Task FlushAsync(CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.flushasync#system-io-textwriter-flushasync(system-threading-cancellationtoken))
* `void Write(StringBuilder)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder))
* `void Write(ReadOnlySpan<Char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-readonlyspan((system-char))))
* `Task WriteAsync(StringBuilder, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
Expand Down
8 changes: 3 additions & 5 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,10 @@ async Task TextWriter_Methods()
TextWriter target = new StringWriter();
target.Write(new StringBuilder());
await target.FlushAsync(CancellationToken.None);
await target.WriteAsync(new StringBuilder());
#if FeatureMemory
//TODO: expose without FeatureMemory
target.WriteAsync(new StringBuilder());
var span = "a".AsSpan();
target.WriteLine(span);
target.Write(span);
target.WriteLine("a".AsSpan());
target.Write("a".AsSpan());
var memory = "a".AsMemory();
await target.WriteLineAsync(memory);
await target.WriteAsync(memory);
Expand Down
26 changes: 16 additions & 10 deletions src/Polyfill/Polyfill_TextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static Task FlushAsync(this TextWriter target, CancellationToken cancella

#endif

#if FeatureMemory
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Equivalent to Write(stringBuilder.ToString()) however it uses the
Expand All @@ -52,16 +51,21 @@ public static Task FlushAsync(this TextWriter target, CancellationToken cancella
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder)")]
public static void Write(this TextWriter target, StringBuilder? value)
{
if (value != null)
if (value == null)
{
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
return;
}

#if FeatureMemory
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
#else
target.Write(value.ToString());
#endif
}

#if FeatureValueTask
/// <summary>
/// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
Expand All @@ -85,16 +89,19 @@ public static Task WriteAsync(this TextWriter target, StringBuilder? value, Canc

async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel)
{
#if FeatureValueTask && FeatureMemory
foreach (ReadOnlyMemory<char> chunk in builder.GetChunks())
{
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
#else
await target.WriteAsync(builder.ToString());
#endif
}
}
#endif
#endif

#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
#if (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0) && FeatureMemory
#if FeatureValueTask

/// <summary>
Expand Down Expand Up @@ -197,5 +204,4 @@ public static void WriteLine(
}
}
#endif
#endif
}

0 comments on commit ea8be86

Please sign in to comment.