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

Commit

Permalink
Bug fix in BufferWriter (#2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Mar 20, 2018
1 parent 765492d commit 0236c48
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 0236c48

Please sign in to comment.