Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZStringWriter #50

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions src/ZString/ZStringWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cysharp.Text
{
/// <summary>
/// A <see cref="TextWriter"/> implementation that is backed with <see cref="Utf16ValueStringBuilder"/>.
/// </summary>
/// <remarks>
/// It's important to make sure the writer is always properly disposed.
/// </remarks>
public sealed class ZStringWriter : TextWriter
{
private Utf16ValueStringBuilder sb;
private bool isOpen;
private UnicodeEncoding encoding;

/// <summary>
/// Creates a new instance using <see cref="CultureInfo.CurrentCulture"/> as format provider.
/// </summary>
public ZStringWriter() : this(CultureInfo.CurrentCulture)
{
}

/// <summary>
/// Creates a new instance with given format provider.
/// </summary>
public ZStringWriter(IFormatProvider formatProvider) : base(formatProvider)
{
sb = ZString.CreateStringBuilder();
isOpen = true;
}

/// <summary>
/// Disposes this instance, operations are no longer allowed.
/// </summary>
public override void Close()
{
Dispose(true);
}

protected override void Dispose(bool disposing)
{
sb.Dispose();
isOpen = false;
base.Dispose(disposing);
}

public override Encoding Encoding => encoding = encoding ?? new UnicodeEncoding(false, false);

public override void Write(char value)
{
AssertNotDisposed();

sb.Append(value);
}

public override void Write(char[] buffer, int index, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
if (buffer.Length - index < count)
{
throw new ArgumentException();
}
AssertNotDisposed();

sb.Append(buffer.AsSpan(index, count));
}

public override void Write(string value)
{
AssertNotDisposed();

if (value != null)
{
sb.Append(value);
}
}

public override Task WriteAsync(char value)
{
Write(value);
return Task.CompletedTask;
}

public override Task WriteAsync(string value)
{
Write(value);
return Task.CompletedTask;
}

public override Task WriteAsync(char[] buffer, int index, int count)
{
Write(buffer, index, count);
return Task.CompletedTask;
}

public override Task WriteLineAsync(char value)
{
WriteLine(value);
return Task.CompletedTask;
}

public override Task WriteLineAsync(string value)
{
WriteLine(value);
return Task.CompletedTask;
}

public override Task WriteLineAsync(char[] buffer, int index, int count)
{
WriteLine(buffer, index, count);
return Task.CompletedTask;
}

public override void Write(bool value)
{
AssertNotDisposed();
sb.Append(value);
}

public override void Write(decimal value)
{
AssertNotDisposed();
sb.Append(value);
}

/// <summary>
/// No-op.
/// </summary>
public override Task FlushAsync()
{
return Task.CompletedTask;
}

/// <summary>
/// Materializes the current state from underlying string builder.
/// </summary>
public override string ToString()
{
return sb.ToString();
}

#if !NETSTANDARD2_0

public override void Write(ReadOnlySpan<char> buffer)
{
AssertNotDisposed();

sb.Append(buffer);
}

public override void WriteLine(ReadOnlySpan<char> buffer)
{
AssertNotDisposed();

sb.Append(buffer);
WriteLine();
}

public override Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

Write(buffer.Span);
return Task.CompletedTask;
}

public override Task WriteLineAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

WriteLine(buffer.Span);
return Task.CompletedTask;
}
#endif

private void AssertNotDisposed()
{
if (!isOpen)
{
throw new ObjectDisposedException(nameof(sb));
}
}
}
}
37 changes: 37 additions & 0 deletions tests/ZString.Tests/ZStringWriterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Cysharp.Text;
using Xunit;

namespace ZStringTests
{
public class ZStringWriterTest
{
[Fact]
public void DoubleDisposeTest()
{
var sb = new ZStringWriter();
sb.Dispose();
sb.Dispose(); // call more than once
}

[Fact]
public void BasicWrites()
{
using (var writer = new ZStringWriter())
{
writer.Write("text1".AsSpan());
writer.Write("text2");
writer.Write('c');
writer.Write(true);
writer.Write(123);
writer.Write(456f);
writer.Write(789d);
writer.Write("end".AsMemory());
writer.WriteLine();

var expected = "text1text2cTrue123456789end" + Environment.NewLine;
Assert.Equal(expected, writer.ToString());
}
}
}
}