Skip to content

Commit

Permalink
Fixed incorrect position within the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Dec 28, 2024
1 parent bc96d79 commit 5fa4f6b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 12 deletions.
73 changes: 68 additions & 5 deletions src/DotNext.Tests/Buffers/BufferWriterSlimTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.Numerics;
using System.Text;
using static System.Globalization.CultureInfo;
Expand All @@ -18,15 +19,15 @@ public static void GrowableBuffer()
Equal(2, builder.Capacity);
Equal(2, builder.FreeCapacity);

builder.Write(stackalloc int[] { 10, 20 });
builder.Write([10, 20]);
Equal(2, builder.WrittenCount);
Equal(2, builder.Capacity);
Equal(0, builder.FreeCapacity);

Equal(10, builder[0]);
Equal(20, builder[1]);

builder.Write(stackalloc int[] { 30, 40 });
builder.Write([30, 40]);
Equal(4, builder.WrittenCount);
True(builder.Capacity >= 2);
Equal(30, builder[2]);
Expand All @@ -38,17 +39,17 @@ public static void GrowableBuffer()

builder.Clear(true);
Equal(0, builder.WrittenCount);
builder.Write(stackalloc int[] { 50, 60, 70, 80 });
builder.Write([50, 60, 70, 80]);
Equal(4, builder.WrittenCount);
True(builder.Capacity >= 2);
Equal(50, builder[0]);
Equal(60, builder[1]);
Equal(70, builder[2]);
Equal(80, builder[3]);

builder.Clear(false);
builder.Clear();
Equal(0, builder.WrittenCount);
builder.Write(stackalloc int[] { 10, 20, 30, 40 });
builder.Write([10, 20, 30, 40]);
Equal(4, builder.WrittenCount);
True(builder.Capacity >= 2);
Equal(10, builder[0]);
Expand Down Expand Up @@ -454,4 +455,66 @@ public static void EncodeDecodeString(string encodingName, LengthFormat? format)
Equal(expected, actual.Span);
}
}

[Fact]
public static void AddList()
{
var writer = new BufferWriterSlim<int>();
try
{
writer.AddAll(new List<int> { 1, 2 });
Equal([1, 2], writer.WrittenSpan);
}
finally
{
writer.Dispose();
}
}

[Fact]
public static void AddArray()
{
var writer = new BufferWriterSlim<int>();
try
{
writer.AddAll([1, 2]);
Equal([1, 2], writer.WrittenSpan);
}
finally
{
writer.Dispose();
}
}

[Fact]
public static void AddString()
{
var writer = new BufferWriterSlim<char>();
try
{
const string expected = "ab";
writer.AddAll(expected);

Equal(expected, writer.WrittenSpan);
}
finally
{
writer.Dispose();
}
}

[Fact]
public static void AddCountableCollection()
{
var writer = new BufferWriterSlim<int>();
try
{
writer.AddAll(ImmutableList.Create(1, 2));
Equal([1, 2], writer.WrittenSpan);
}
finally
{
writer.Dispose();
}
}
}
19 changes: 12 additions & 7 deletions src/DotNext/Buffers/BufferWriterSlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,24 +362,27 @@ public void AddAll(IEnumerable<T> collection)
{
ArgumentNullException.ThrowIfNull(collection);

ReadOnlySpan<T> input;
switch (collection)
{
case List<T> list:
Write(CollectionsMarshal.AsSpan(list));
input = CollectionsMarshal.AsSpan(list);
break;
case T[] array:
Write(new ReadOnlySpan<T>(array));
input = array;
break;
case string str:
Write(Unsafe.BitCast<ReadOnlyMemory<char>, ReadOnlyMemory<T>>(str.AsMemory()).Span);
input = Unsafe.BitCast<ReadOnlyMemory<char>, ReadOnlyMemory<T>>(str.AsMemory()).Span;
break;
case ArraySegment<T> segment:
Write(segment.AsSpan());
input = segment;
break;
default:
WriteSlow(collection);
break;
return;
}

Write(input);
}

private void WriteSlow(IEnumerable<T> collection)
Expand All @@ -388,10 +391,12 @@ private void WriteSlow(IEnumerable<T> collection)
if (collection.TryGetNonEnumeratedCount(out var count))
{
var buffer = InternalGetSpan(count);
for (var i = 0; i < buffer.Length && enumerator.MoveNext(); i++)
for (count = 0; count < buffer.Length && enumerator.MoveNext(); count++)
{
buffer[i] = enumerator.Current;
buffer[count] = enumerator.Current;
}

position += count;
}

while (enumerator.MoveNext())
Expand Down

0 comments on commit 5fa4f6b

Please sign in to comment.