Skip to content

Commit

Permalink
Cleanup in SpreadsheetBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Dec 26, 2023
1 parent 30915a0 commit 2e8420c
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions SpreadCheetah/SpreadsheetBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@

namespace SpreadCheetah;

internal sealed class SpreadsheetBuffer
internal sealed class SpreadsheetBuffer(byte[] buffer)
{
private readonly byte[] _buffer;
private readonly byte[] _buffer = buffer;
private int _index;

public SpreadsheetBuffer(byte[] buffer)
{
_buffer = buffer;
}

public Span<byte> GetSpan() => _buffer.AsSpan(_index);
public int FreeCapacity => _buffer.Length - _index;
public void Advance(int bytes) => _index += bytes;
Expand Down Expand Up @@ -72,22 +67,15 @@ public TryWriteInterpolatedStringHandler(int literalLength, int formattedCount,

private readonly Span<byte> GetSpan() => _buffer._buffer.AsSpan(_buffer._index + _pos);

// TODO: Remove or throw? Literals should rather use ReadOnlySpan<byte>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AppendLiteral(string value)
{
if (value is not null)
Debug.Fail("Use ReadOnlySpan<byte> instead of string literals");

if (value is not null && Utf8Helper.TryGetBytes(value, GetSpan(), out var bytesWritten))
{
var dest = GetSpan();
#if NET8_0_OR_GREATER
if (System.Text.Unicode.Utf8.TryWrite(dest, CultureInfo.InvariantCulture, $"{value}", out var bytesWritten))
#else
if (Utf8Helper.TryGetBytes(value, dest, out var bytesWritten))
#endif
{
_pos += bytesWritten;
return true;
}
_pos += bytesWritten;
return true;
}

return Fail();
Expand Down

0 comments on commit 2e8420c

Please sign in to comment.