Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Bug fix in BufferWriter #2170

Merged
merged 1 commit into from
Mar 20, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public void WriteBytes(ReadOnlyMemory<byte> bytes)

public bool TryWriteBytes(ReadOnlyMemory<byte> bytes, TransformationFormat format)
{
if (!TryWriteBytes(bytes.Span))
var span = bytes.Span;
if (!span.TryCopyTo(Free))
{
return false;
}
int written = span.Length;

int written = bytes.Length;
if (format.TryTransform(Free, ref written))
{
_written += written;
Expand Down
12 changes: 12 additions & 0 deletions tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Buffers.Operations;
using System.Buffers.Text;
using System.Buffers.Writer;
using System.Diagnostics;
Expand Down Expand Up @@ -59,6 +60,17 @@ public void WritePlainText()
var result = _sink.ToString();
Assert.Equal(s_response, _sink.ToString());
}

[Fact]
public void BufferWriterTransform()
{
byte[] buffer = new byte[10];
var writer = BufferWriter.Create(buffer.AsSpan());
var transformation = new TransformationFormat(new RemoveTransformation(2));
ReadOnlyMemory<byte> value = new byte[] { 1, 2, 3 };
writer.WriteBytes(value, transformation);
Assert.Equal(-1, buffer.AsSpan().IndexOf((byte)2));
}
}

class Sink : IBufferWriter<byte>
Expand Down