diff --git a/SpreadCheetah/SpreadsheetBuffer.cs b/SpreadCheetah/SpreadsheetBuffer.cs index 9faef969..81e668a4 100644 --- a/SpreadCheetah/SpreadsheetBuffer.cs +++ b/SpreadCheetah/SpreadsheetBuffer.cs @@ -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 GetSpan() => _buffer.AsSpan(_index); public int FreeCapacity => _buffer.Length - _index; public void Advance(int bytes) => _index += bytes; @@ -72,22 +67,15 @@ public TryWriteInterpolatedStringHandler(int literalLength, int formattedCount, private readonly Span GetSpan() => _buffer._buffer.AsSpan(_buffer._index + _pos); - // TODO: Remove or throw? Literals should rather use ReadOnlySpan [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool AppendLiteral(string value) { - if (value is not null) + Debug.Fail("Use ReadOnlySpan 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();