using System.Diagnostics;
using Brutal.Attributes;
namespace Brutal.Localization.FixedBuffers;
///
/// An IFixedBufferWriter that will wrap around and reuse its storage if the length requested for a Span is greater than
/// the remaining storage but less than or equal to its capacity. If you ask for lengths that fit within it's remaining
/// storage it will return part or all of it's remaining storage depending on the length hint. However, if you ask for
/// or a specific size that's larger than the remaining storage, it can give you up
/// to its Capacity by wrapping around. Using with a essentially gives you a writer that will automatically
/// reuse its storage each time you call GetSpan or TryGetSpan.
///
/// The element type stored in the RingBufferWriter.
/// Instantiating a default instance of is an error due to no specification
/// of the underlying buffer. Calls to or will throw in this case.
public ref struct RingBufferWriter : IFixedBufferWriter
{
///
/// Create a RingBufferWriter from a Span.
///
/// The to use as the underlying storage.
/// Thrown if span is empty.
public RingBufferWriter(Span span)
{
if (span.IsEmpty)
throw new ArgumentException("Span must not be empty", nameof(span));
_span = span;
_pos = 0;
}
///
public void Advance(ElementCount count)
{
if (count < 0 || (count > 0 && count > RemainingLength))
throw new ArgumentOutOfRangeException(
nameof(count),
$"Advance count out of range. Valid values are 0-{RemainingLength}"
);
_pos += count;
}
///
/// Thrown if requested length is zero. This limitation may be removed at a later date.
/// Instantiating a default instance of is an error due to no specification
/// of the underlying buffer. Calls to or will throw in this case.
public Span GetSpan(ElementCount lengthHint)
{
if (lengthHint == 0)
throw new ArgumentOutOfRangeException(
nameof(lengthHint),
"Cannot request a zero length span."
);
if (!TryGetSpan(lengthHint, out var spanOut))
throw new ArgumentException(
$"Out of buffer. requested:{lengthHint} available:{spanOut.Length}",
nameof(lengthHint)
);
return spanOut;
}
///
/// Instantiating a default instance of is an error due to no specification
/// of the underlying buffer. Calls to or will throw in this case.
public bool TryGetSpan(ElementCount lengthHint, out Span spanOut)
{
ThrowIfNoCapacity();
var returnLength = lengthHint;
if (lengthHint == LengthHint.NonZeroLengthHint)
returnLength = RemainingLength != 0 ? RemainingLength : Capacity;
else if (lengthHint == LengthHint.MaxLengthHint || lengthHint > Capacity)
returnLength = Capacity;
else if (lengthHint < 0)
throw new ArgumentOutOfRangeException(
nameof(lengthHint),
"lengthHint cannot be negative."
);
Debug.Assert(returnLength <= Capacity);
if (returnLength > RemainingLength)
Reset();
spanOut = RemainingSpan;
return lengthHint == LengthHint.NonZeroLengthHint
|| lengthHint == LengthHint.MaxLengthHint
|| spanOut.Length >= lengthHint;
}
private void ThrowIfNoCapacity()
{
if (Capacity < 1)
throw new InvalidOperationException(
$"Invalid Capacity (0). Did you create a default instance of this writer without a Span?"
);
}
private Span RemainingSpan => _span.Slice(_pos, RemainingLength);
private void Reset() => _pos = 0;
private int RemainingLength => _span.Length - _pos;
private int Capacity => _span.Length;
private int _pos;
private readonly Span _span;
}