diff --git a/contributing.md b/contributing.md index d40c23d..c74dc15 100644 --- a/contributing.md +++ b/contributing.md @@ -230,7 +230,6 @@ static partial class Polyfill #endif -#if FeatureMemory #if !NETCOREAPP3_0_OR_GREATER /// /// Equivalent to Write(stringBuilder.ToString()) however it uses the @@ -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 chunk in value.GetChunks()) - { - target.Write(chunk.Span); - } + return; } + +#if FeatureMemory + foreach (ReadOnlyMemory chunk in value.GetChunks()) + { + target.Write(chunk.Span); + } +#else + target.Write(value.ToString()); +#endif } -#if FeatureValueTask /// /// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the /// StringBuilder.GetChunks() method to avoid creating the intermediate string @@ -273,16 +277,19 @@ static partial class Polyfill async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel) { +#if FeatureValueTask && FeatureMemory foreach (ReadOnlyMemory 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 /// @@ -385,10 +392,9 @@ static partial class Polyfill } } #endif -#endif } ``` -snippet source | anchor +snippet source | anchor diff --git a/readme.md b/readme.md index 252899c..1439c4a 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: 338** +**API count: 339** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -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)` [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)) diff --git a/src/Consume/Consume.cs b/src/Consume/Consume.cs index bcfbc85..c10c844 100644 --- a/src/Consume/Consume.cs +++ b/src/Consume/Consume.cs @@ -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); diff --git a/src/Polyfill/Polyfill_TextWriter.cs b/src/Polyfill/Polyfill_TextWriter.cs index d26c06f..2890687 100644 --- a/src/Polyfill/Polyfill_TextWriter.cs +++ b/src/Polyfill/Polyfill_TextWriter.cs @@ -42,7 +42,6 @@ public static Task FlushAsync(this TextWriter target, CancellationToken cancella #endif -#if FeatureMemory #if !NETCOREAPP3_0_OR_GREATER /// /// Equivalent to Write(stringBuilder.ToString()) however it uses the @@ -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 chunk in value.GetChunks()) - { - target.Write(chunk.Span); - } + return; } + +#if FeatureMemory + foreach (ReadOnlyMemory chunk in value.GetChunks()) + { + target.Write(chunk.Span); + } +#else + target.Write(value.ToString()); +#endif } -#if FeatureValueTask /// /// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the /// StringBuilder.GetChunks() method to avoid creating the intermediate string @@ -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 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 /// @@ -197,5 +204,4 @@ public static void WriteLine( } } #endif -#endif } \ No newline at end of file