Skip to content

Commit

Permalink
Added ability to modify the number of written items
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Oct 6, 2023
1 parent d71d531 commit 20cea21
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/DotNext.Tests/Buffers/BufferWriterSlimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,31 @@ public static void AdvanceRewind()
buffer.Advance(1);
Equal(42, buffer[0]);
}

[Fact]
public static void ChangeWrittenCount()
{
var buffer = new BufferWriterSlim<int>(stackalloc int[3]);

var raised = false;
try
{
buffer.WrittenCount = 4;
}
catch (ArgumentOutOfRangeException)
{
raised = true;
}

True(raised);

buffer.Add(42);
Equal(1, buffer.WrittenCount);

buffer.WrittenCount = 0;
Equal(0, buffer.WrittenCount);

buffer.WrittenCount = 1;
Equal(42, buffer[0]);
}
}
17 changes: 17 additions & 0 deletions src/DotNext.Tests/Buffers/BufferWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,21 @@ public static void Concatenation()
writer.Concat(("Hello, ", "world!").AsReadOnlySpan());
Equal("Hello, world!", writer.BuildString());
}

[Fact]
public static void ChangeWrittenCount()
{
using var buffer = new PooledArrayBufferWriter<int>();

Throws<ArgumentOutOfRangeException>(() => buffer.WrittenCount = 1);

buffer.Add(42);
Equal(1, buffer.WrittenCount);

buffer.WrittenCount = 0;
Equal(0, buffer.WrittenCount);

buffer.WrittenCount = 1;
Equal(42, buffer[0]);
}
}
52 changes: 52 additions & 0 deletions src/DotNext.Tests/Buffers/SpanReaderWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,56 @@ public static void WriteFormattable()
Equal(2, writer.WrittenCount);
Equal("2A", new string(writer.WrittenSpan));
}

[Fact]
public static void ChangeWrittenCount()
{
var writer = new SpanWriter<char>(stackalloc char[32]);
Equal(0, writer.WrittenCount);

writer.WrittenCount = 10;
Equal(10, writer.WrittenCount);

writer.WrittenCount = 32;
Equal(32, writer.WrittenCount);
True(writer.RemainingSpan.IsEmpty);

var raised = false;
try
{
writer.WrittenCount = -1;
}
catch (ArgumentOutOfRangeException)
{
raised = true;
}

True(raised);
}

[Fact]
public static void ChangeConsumedCount()
{
var writer = new SpanReader<char>(stackalloc char[32]);
Equal(0, writer.ConsumedCount);

writer.ConsumedCount = 10;
Equal(10, writer.ConsumedCount);

writer.ConsumedCount = 32;
Equal(32, writer.ConsumedCount);
True(writer.RemainingSpan.IsEmpty);

var raised = false;
try
{
writer.ConsumedCount = -1;
}
catch (ArgumentOutOfRangeException)
{
raised = true;
}

True(raised);
}
}
20 changes: 18 additions & 2 deletions src/DotNext/Buffers/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,25 @@ public virtual TagList MeasurementTags
ReadOnlyMemory<T> ISupplier<ReadOnlyMemory<T>>.Invoke() => WrittenMemory;

/// <summary>
/// Gets the amount of data written to the underlying memory so far.
/// Gets or sets the amount of data written to the underlying memory so far.
/// </summary>
public int WrittenCount => position;
/// <exception cref="ArgumentOutOfRangeException"></exception>
public int WrittenCount
{
get => position;
set
{
if ((uint)value > (uint)Capacity)
ThrowArgumentOutOfRangeException();

position = value;

[DoesNotReturn]
[StackTraceHidden]
static void ThrowArgumentOutOfRangeException()
=> throw new ArgumentOutOfRangeException(nameof(value));
}
}

/// <inheritdoc />
long IGrowableBuffer<T>.WrittenCount => WrittenCount;
Expand Down
17 changes: 16 additions & 1 deletion src/DotNext/Buffers/BufferWriterSlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,22 @@ public BufferWriterSlim(int initialCapacity, MemoryAllocator<T>? allocator = nul
/// <summary>
/// Gets the amount of data written to the underlying memory so far.
/// </summary>
public readonly int WrittenCount => position;
public int WrittenCount
{
readonly get => position;
set
{
if ((uint)value > (uint)Capacity)
ThrowArgumentOutOfRangeException();

position = value;

[DoesNotReturn]
[StackTraceHidden]
static void ThrowArgumentOutOfRangeException()
=> throw new ArgumentOutOfRangeException(nameof(value));
}
}

/// <summary>
/// Gets the total amount of space within the underlying memory.
Expand Down
17 changes: 16 additions & 1 deletion src/DotNext/Buffers/SpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ public readonly ref readonly T Current
/// <summary>
/// Gets the number of consumed elements.
/// </summary>
public readonly int ConsumedCount => position;
public int ConsumedCount
{
readonly get => position;
set
{
if ((uint)value > (uint)span.Length)
ThrowArgumentOutOfRangeException();

position = value;

[StackTraceHidden]
[DoesNotReturn]
static void ThrowArgumentOutOfRangeException()
=> throw new ArgumentOutOfRangeException(nameof(value));
}
}

/// <summary>
/// Gets the number of unread elements.
Expand Down
17 changes: 16 additions & 1 deletion src/DotNext/Buffers/SpanWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,22 @@ public readonly ref T Current
/// <summary>
/// Gets the number of occupied elements in the underlying span.
/// </summary>
public readonly int WrittenCount => position;
public int WrittenCount
{
readonly get => position;
set
{
if ((uint)value > span.Length)
ThrowArgumentOutOfRangeException();

position = value;

[DoesNotReturn]
[StackTraceHidden]
static void ThrowArgumentOutOfRangeException()
=> throw new ArgumentOutOfRangeException(nameof(value));
}
}

/// <summary>
/// Gets the remaining part of the span.
Expand Down

0 comments on commit 20cea21

Please sign in to comment.