diff --git a/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs index 8f1ffa6c..b0f33783 100644 --- a/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs @@ -10,36 +10,27 @@ internal abstract class BooleanCellValueWriter : CellValueWriter private static ReadOnlySpan BeginStyledBooleanCell => " BeginBooleanFormulaCell => ""u8; - protected abstract bool TryWriteCell(CellWriterState state); + protected abstract bool TryWriteCell(SpreadsheetBuffer buffer); + protected abstract bool TryWriteCellWithReference(CellWriterState state); protected abstract bool TryWriteEndStyleValue(Span bytes, out int bytesWritten); protected abstract bool TryWriteEndFormulaValue(Span bytes, out int bytesWritten); - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - return TryWriteCell(state); + return TryWriteCell(buffer); } - public override bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer) { - return TryWriteCell(styleId, state); + return TryWriteCell(styleId, buffer); } - private bool TryWriteCell(StyleId styleId, CellWriterState state) + private bool TryWriteCell(StyleId styleId, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\" s=\""u8.TryCopyTo(bytes, ref written)) return false; - } - + if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; if (!TryWriteEndStyleValue(bytes.Slice(written), out var endLength)) return false; written += endLength; @@ -48,12 +39,22 @@ private bool TryWriteCell(StyleId styleId, CellWriterState state) return true; } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); + var written = 0; + + if (styleId is null) + { + if (!BeginBooleanFormulaCell.TryCopyTo(bytes, ref written)) return false; + } + else + { + if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + } - if (!TryWriteFormulaCellStart(styleId, state, bytes, out var written)) return false; if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; if (!TryWriteEndFormulaValue(bytes.Slice(written), out var endLength)) return false; @@ -61,37 +62,55 @@ public override bool TryWriteCell(string formulaText, in DataCell cachedValue, S return true; } - private static bool TryWriteFormulaCellStart(StyleId? styleId, CellWriterState state, Span bytes, out int bytesWritten) + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) { - bytesWritten = 0; + return TryWriteCellWithReference(state); + } + + public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state) + { + return TryWriteCellWithReference(styleId, state); + } + + private bool TryWriteCellWithReference(StyleId styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); var written = 0; - if (styleId is null) - { - if (!state.WriteCellReferenceAttributes) - return BeginBooleanFormulaCell.TryCopyTo(bytes, ref bytesWritten); + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"b\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!TryWriteEndStyleValue(bytes.Slice(written), out var endLength)) return false; + written += endLength; - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\">"u8.TryCopyTo(bytes, ref written)) return false; + buffer.Advance(written); + return true; + } - bytesWritten = written; - return true; - } + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; - if (!state.WriteCellReferenceAttributes) + if (styleId is null) { - if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"b\">"u8.TryCopyTo(bytes, ref written)) return false; } else { if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; if (!"\" t=\"b\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; } - if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; - if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; + if (!TryWriteEndFormulaValue(bytes.Slice(written), out var endLength)) return false; - bytesWritten = written; + buffer.Advance(written + endLength); return true; } @@ -112,38 +131,43 @@ public override bool TryWriteEndElement(in Cell cell, SpreadsheetBuffer buffer) return false; } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - { - if (!BeginBooleanFormulaCell.TryCopyTo(bytes, ref written)) return false; - buffer.Advance(written); - return true; - } - - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\">"u8.TryCopyTo(bytes, ref written)) return false; - + if (!BeginBooleanFormulaCell.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; - } - else + if (!BeginStyledBooleanCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (styleId is null) { if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!"\" t=\"b\">"u8.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"b\" s=\""u8.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; @@ -151,9 +175,10 @@ public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? return true; } - public override bool WriteStartElement(CellWriterState state) => TryWriteCell(state); - - public override bool WriteStartElement(StyleId styleId, CellWriterState state) => TryWriteCell(styleId, state); + public override bool WriteStartElement(SpreadsheetBuffer buffer) => TryWriteCell(buffer); + public override bool WriteStartElement(StyleId styleId, SpreadsheetBuffer buffer) => TryWriteCell(styleId, buffer); + public override bool WriteStartElementWithReference(CellWriterState state) => TryWriteCellWithReference(state); + public override bool WriteStartElementWithReference(StyleId styleId, CellWriterState state) => TryWriteCellWithReference(styleId, state); /// /// Returns false because the value is written together with the end element in . diff --git a/SpreadCheetah/CellValueWriters/Boolean/FalseBooleanCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Boolean/FalseBooleanCellValueWriter.cs index be63b393..9c5ccfb6 100644 --- a/SpreadCheetah/CellValueWriters/Boolean/FalseBooleanCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Boolean/FalseBooleanCellValueWriter.cs @@ -9,21 +9,25 @@ internal sealed class FalseBooleanCellValueWriter : BooleanCellValueWriter private static ReadOnlySpan EndStyleFalseBooleanValue => "\">0"u8; private static ReadOnlySpan EndFormulaFalseBooleanValue => "0"u8; - protected override bool TryWriteCell(CellWriterState state) + protected override bool TryWriteCell(SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!FalseBooleanCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected override bool TryWriteCellWithReference(CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!FalseBooleanCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\">0"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"b\">0"u8.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; diff --git a/SpreadCheetah/CellValueWriters/Boolean/TrueBooleanCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Boolean/TrueBooleanCellValueWriter.cs index 072fc749..30c51347 100644 --- a/SpreadCheetah/CellValueWriters/Boolean/TrueBooleanCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Boolean/TrueBooleanCellValueWriter.cs @@ -9,21 +9,25 @@ internal sealed class TrueBooleanCellValueWriter : BooleanCellValueWriter private static ReadOnlySpan EndStyleTrueBooleanValue => "\">1"u8; private static ReadOnlySpan EndFormulaTrueBooleanValue => "1"u8; - protected override bool TryWriteCell(CellWriterState state) + protected override bool TryWriteCell(SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!TrueBooleanCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected override bool TryWriteCellWithReference(CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!TrueBooleanCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"b\">1"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"b\">1"u8.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; diff --git a/SpreadCheetah/CellValueWriters/CellValueWriter.cs b/SpreadCheetah/CellValueWriters/CellValueWriter.cs index a95bd212..604e4f19 100644 --- a/SpreadCheetah/CellValueWriters/CellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/CellValueWriter.cs @@ -20,12 +20,18 @@ internal abstract class CellValueWriter public static CellValueWriter FalseBoolean { get; } = new FalseBooleanCellValueWriter(); public static CellValueWriter String { get; } = new StringCellValueWriter(); - public abstract bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state); - public abstract bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterState state); - public abstract bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state); - public abstract bool WriteStartElement(CellWriterState state); - public abstract bool WriteStartElement(StyleId styleId, CellWriterState state); - public abstract bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state); + public abstract bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer); + public abstract bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer); + public abstract bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer); + public abstract bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state); + public abstract bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state); + public abstract bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state); + public abstract bool WriteStartElement(SpreadsheetBuffer buffer); + public abstract bool WriteStartElement(StyleId styleId, SpreadsheetBuffer buffer); + public abstract bool WriteStartElementWithReference(CellWriterState state); + public abstract bool WriteStartElementWithReference(StyleId styleId, CellWriterState state); + public abstract bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer); + public abstract bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state); public abstract bool CanWriteValuePieceByPiece(in DataCell cell); public abstract bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer buffer, ref int valueIndex); public abstract bool TryWriteEndElement(SpreadsheetBuffer buffer); diff --git a/SpreadCheetah/CellValueWriters/NullValueWriter.cs b/SpreadCheetah/CellValueWriters/NullValueWriter.cs index cf47a579..57a0ce0c 100644 --- a/SpreadCheetah/CellValueWriters/NullValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/NullValueWriter.cs @@ -8,18 +8,33 @@ internal sealed class NullValueWriter : NullValueWriterBase { protected override int GetStyleId(StyleId styleId) => styleId.Id; - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - return TryWriteCell(state); + return TryWriteCell(buffer); } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - return TryWriteCell(formulaText, styleId?.Id, state); + return TryWriteCell(formulaText, styleId?.Id, buffer); } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) { - return WriteFormulaStartElement(styleId?.Id, state); + return TryWriteCellWithReference(state); + } + + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + return TryWriteCellWithReference(formulaText, styleId?.Id, state); + } + + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) + { + return WriteFormulaStartElement(styleId?.Id, buffer); + } + + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + return WriteFormulaStartElementWithReference(styleId?.Id, state); } } diff --git a/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs b/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs index e29be0c3..74da17af 100644 --- a/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs +++ b/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs @@ -13,42 +13,23 @@ internal abstract class NullValueWriterBase : CellValueWriter private static ReadOnlySpan EndStyleNullValue => "\"/>"u8; private static ReadOnlySpan EndFormulaEndCell => ""u8; - protected static bool TryWriteCell(CellWriterState state) + protected static bool TryWriteCell(SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!NullDataCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\"/>"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!NullDataCell.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - protected static bool TryWriteCell(int styleId, CellWriterState state) + protected static bool TryWriteCell(int styleId, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; - } - + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId, bytes, ref written)) return false; if (!EndStyleNullValue.TryCopyTo(bytes, ref written)) return false; @@ -56,12 +37,11 @@ protected static bool TryWriteCell(int styleId, CellWriterState state) return true; } - protected static bool TryWriteCell(string formulaText, int? styleId, CellWriterState state) + protected static bool TryWriteCell(string formulaText, int? styleId, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); - if (!NumberCellValueWriterBase.TryWriteFormulaCellStart(styleId, state, bytes, out var written)) return false; + if (!NumberCellValueWriterBase.TryWriteFormulaCellStart(styleId, bytes, out var written)) return false; if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; if (!EndFormulaEndCell.TryCopyTo(bytes, ref written)) return false; @@ -69,9 +49,55 @@ protected static bool TryWriteCell(string formulaText, int? styleId, CellWriterS return true; } - public override bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer) + { + return TryWriteCell(GetStyleId(styleId), buffer); + } + + protected static bool TryWriteCellWithReference(CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\"/>"u8.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state) { - return TryWriteCell(GetStyleId(styleId), state); + return TryWriteCellWithReference(GetStyleId(styleId), state); + } + + protected static bool TryWriteCellWithReference(int styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId, bytes, ref written)) return false; + if (!EndStyleNullValue.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected static bool TryWriteCellWithReference(string formulaText, int? styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + + if (!NumberCellValueWriterBase.TryWriteFormulaCellStartWithReference(styleId, state, bytes, out var written)) return false; + if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; + if (!EndFormulaEndCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; } public override bool TryWriteEndElement(SpreadsheetBuffer buffer) => true; @@ -91,38 +117,43 @@ public override bool TryWriteEndElement(in Cell cell, SpreadsheetBuffer buffer) return false; } - protected static bool WriteFormulaStartElement(int? styleId, CellWriterState state) + protected static bool WriteFormulaStartElement(int? styleId, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - { - if (!FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref written)) return false; - buffer.Advance(written); - return true; - } - - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; - + if (!FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected static bool WriteFormulaStartElementWithReference(int? styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (styleId is null) { if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; + if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; @@ -130,9 +161,10 @@ protected static bool WriteFormulaStartElement(int? styleId, CellWriterState sta return true; } - public override bool WriteStartElement(CellWriterState state) => TryWriteCell(state); - - public override bool WriteStartElement(StyleId styleId, CellWriterState state) => TryWriteCell(GetStyleId(styleId), state); + public override bool WriteStartElement(SpreadsheetBuffer buffer) => TryWriteCell(buffer); + public override bool WriteStartElement(StyleId styleId, SpreadsheetBuffer buffer) => TryWriteCell(GetStyleId(styleId), buffer); + public override bool WriteStartElementWithReference(CellWriterState state) => TryWriteCellWithReference(state); + public override bool WriteStartElementWithReference(StyleId styleId, CellWriterState state) => TryWriteCellWithReference(GetStyleId(styleId), state); /// /// Returns false because there is no value to write for 'null'. diff --git a/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriter.cs index 4b3ae70c..ec4eb6cc 100644 --- a/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriter.cs @@ -8,18 +8,33 @@ internal abstract class NumberCellValueWriter : NumberCellValueWriterBase { protected override int GetStyleId(StyleId styleId) => styleId.Id; - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - return TryWriteCell(cell, state); + return TryWriteCell(cell, buffer); } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - return TryWriteCell(formulaText, cachedValue, styleId?.Id, state); + return TryWriteCell(formulaText, cachedValue, styleId?.Id, buffer); } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) { - return WriteFormulaStartElement(styleId?.Id, state); + return TryWriteCellWithReference(cell, state); + } + + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + return TryWriteCellWithReference(formulaText, cachedValue, styleId?.Id, state); + } + + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) + { + return WriteFormulaStartElement(styleId?.Id, buffer); + } + + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + return WriteFormulaStartElementWithReference(styleId?.Id, state); } } diff --git a/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriterBase.cs b/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriterBase.cs index e4249ccb..01659e39 100644 --- a/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriterBase.cs +++ b/SpreadCheetah/CellValueWriters/Number/NumberCellValueWriterBase.cs @@ -13,21 +13,29 @@ internal abstract class NumberCellValueWriterBase : CellValueWriter private static ReadOnlySpan EndStyleBeginValue => "\">"u8; private static ReadOnlySpan EndDefaultCell => ""u8; - protected bool TryWriteCell(in DataCell cell, CellWriterState state) + protected bool TryWriteCell(in DataCell cell, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginDataCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!BeginDataCell.TryCopyTo(bytes, ref written)) return false; + if (!TryWriteValue(cell, bytes.Slice(written), out var valueLength)) return false; + written += valueLength; + + if (!EndDefaultCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected bool TryWriteCell(in DataCell cell, int styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId, bytes, ref written)) return false; + if (!EndStyleBeginValue.TryCopyTo(bytes, ref written)) return false; if (!TryWriteValue(cell, bytes.Slice(written), out var valueLength)) return false; written += valueLength; @@ -38,22 +46,57 @@ protected bool TryWriteCell(in DataCell cell, CellWriterState state) return true; } - protected bool TryWriteCell(in DataCell cell, int styleId, CellWriterState state) + protected bool TryWriteCell(string formulaText, in DataCell cachedValue, int? styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + + if (!TryWriteFormulaCellStart(styleId, bytes, out var written)) return false; + if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; + if (!FormulaCellHelper.EndFormulaBeginCachedValue.TryCopyTo(bytes, ref written)) return false; + if (!TryWriteValue(cachedValue, bytes.Slice(written), out var valueLength)) return false; + written += valueLength; + + if (!FormulaCellHelper.EndCachedValueEndCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer) + { + return TryWriteCell(cell, GetStyleId(styleId), buffer); + } + + public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state) + { + return TryWriteCellWithReference(cell, GetStyleId(styleId), state); + } + + protected bool TryWriteCellWithReference(in DataCell cell, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; - } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; + if (!TryWriteValue(cell, bytes.Slice(written), out var valueLength)) return false; + written += valueLength; + + if (!EndDefaultCell.TryCopyTo(bytes, ref written)) return false; + buffer.Advance(written); + return true; + } + + protected bool TryWriteCellWithReference(in DataCell cell, int styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId, bytes, ref written)) return false; if (!EndStyleBeginValue.TryCopyTo(bytes, ref written)) return false; @@ -66,12 +109,12 @@ protected bool TryWriteCell(in DataCell cell, int styleId, CellWriterState state return true; } - protected bool TryWriteCell(string formulaText, in DataCell cachedValue, int? styleId, CellWriterState state) + protected bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, int? styleId, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); - if (!TryWriteFormulaCellStart(styleId, state, bytes, out var written)) return false; + if (!TryWriteFormulaCellStartWithReference(styleId, state, bytes, out var written)) return false; if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; if (!FormulaCellHelper.EndFormulaBeginCachedValue.TryCopyTo(bytes, ref written)) return false; if (!TryWriteValue(cachedValue, bytes.Slice(written), out var valueLength)) return false; @@ -83,21 +126,31 @@ protected bool TryWriteCell(string formulaText, in DataCell cachedValue, int? st return true; } - public override bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterState state) + public static bool TryWriteFormulaCellStart(int? styleId, Span bytes, out int bytesWritten) { - return TryWriteCell(cell, GetStyleId(styleId), state); + bytesWritten = 0; + var written = 0; + + if (styleId is null) + { + return FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref bytesWritten); + } + + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + + bytesWritten = written; + return true; } - public static bool TryWriteFormulaCellStart(int? styleId, CellWriterState state, Span bytes, out int bytesWritten) + public static bool TryWriteFormulaCellStartWithReference(int? styleId, CellWriterState state, Span bytes, out int bytesWritten) { bytesWritten = 0; var written = 0; if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - return FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref bytesWritten); - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; @@ -105,16 +158,8 @@ public static bool TryWriteFormulaCellStart(int? styleId, CellWriterState state, return true; } - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; - } - + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; @@ -122,50 +167,79 @@ public static bool TryWriteFormulaCellStart(int? styleId, CellWriterState state, return true; } - public override bool WriteStartElement(CellWriterState state) + public override bool WriteStartElement(SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!BeginDataCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteStartElement(StyleId styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(GetStyleId(styleId), bytes, ref written)) return false; + if (!EndStyleBeginValue.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteStartElementWithReference(CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginDataCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - public override bool WriteStartElement(StyleId styleId, CellWriterState state) + public override bool WriteStartElementWithReference(StyleId styleId, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(GetStyleId(styleId), bytes, ref written)) return false; + if (!EndStyleBeginValue.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + protected static bool WriteFormulaStartElement(int? styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (styleId is null) { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; + if (!FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref written)) return false; + buffer.Advance(written); + return true; } - if (!SpanHelper.TryWrite(GetStyleId(styleId), bytes, ref written)) return false; - if (!EndStyleBeginValue.TryCopyTo(bytes, ref written)) return false; + if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - protected static bool WriteFormulaStartElement(int? styleId, CellWriterState state) + protected static bool WriteFormulaStartElementWithReference(int? styleId, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); @@ -173,13 +247,6 @@ protected static bool WriteFormulaStartElement(int? styleId, CellWriterState sta if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - { - if (!FormulaCellHelper.BeginNumberFormulaCell.TryCopyTo(bytes, ref written)) return false; - buffer.Advance(written); - return true; - } - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; if (!"\">"u8.TryCopyTo(bytes, ref written)) return false; @@ -187,16 +254,8 @@ protected static bool WriteFormulaStartElement(int? styleId, CellWriterState sta return true; } - if (!state.WriteCellReferenceAttributes) - { - if (!StyledCellHelper.BeginStyledNumberCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; - } - + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!StyledCellHelper.EndReferenceBeginStyleId.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Value, bytes, ref written)) return false; if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; diff --git a/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs b/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs index 606603a2..8f1f3e59 100644 --- a/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs @@ -14,22 +14,27 @@ internal sealed class StringCellValueWriter : CellValueWriter private static ReadOnlySpan EndStyleBeginInlineString => "\">"u8; private static ReadOnlySpan EndStringCell => ""u8; - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStringCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"inlineStr\">"u8.TryCopyTo(bytes, ref written)) return false; - } + if (!BeginStringCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(cell.StringValue, bytes, ref written)) return false; + if (!EndStringCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!EndStyleBeginInlineString.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(cell.StringValue, bytes, ref written)) return false; if (!EndStringCell.TryCopyTo(bytes, ref written)) return false; @@ -37,24 +42,39 @@ public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyli return true; } - public override bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) + if (styleId is null) { - if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; + if (!BeginStringFormulaCell.TryCopyTo(bytes, ref written)) return false; } else { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!BeginStyledStringFormulaCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; } - if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; - if (!EndStyleBeginInlineString.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; + if (!FormulaCellHelper.EndFormulaBeginCachedValue.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(cachedValue.StringValue, bytes, ref written)) return false; + if (!FormulaCellHelper.EndCachedValueEndCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"inlineStr\">"u8.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(cell.StringValue, bytes, ref written)) return false; if (!EndStringCell.TryCopyTo(bytes, ref written)) return false; @@ -62,52 +82,48 @@ public override bool TryWriteCell(in DataCell cell, StyleId styleId, CellWriterS return true; } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); + var written = 0; - if (!TryWriteFormulaCellStart(styleId, state, bytes, out var written)) return false; - if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; - if (!FormulaCellHelper.EndFormulaBeginCachedValue.TryCopyTo(bytes, ref written)) return false; - if (!SpanHelper.TryWrite(cachedValue.StringValue, bytes, ref written)) return false; - if (!FormulaCellHelper.EndCachedValueEndCell.TryCopyTo(bytes, ref written)) return false; + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!EndStyleBeginInlineString.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(cell.StringValue, bytes, ref written)) return false; + if (!EndStringCell.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - private static bool TryWriteFormulaCellStart(StyleId? styleId, CellWriterState state, Span bytes, out int bytesWritten) + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) { - bytesWritten = 0; + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); var written = 0; if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - return BeginStringFormulaCell.TryCopyTo(bytes, ref bytesWritten); - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; if (!"\" t=\"str\">"u8.TryCopyTo(bytes, ref written)) return false; - - bytesWritten = written; - return true; - } - - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStyledStringFormulaCell.TryCopyTo(bytes, ref written)) return false; } else { if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; if (!"\" t=\"str\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; } - if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; - if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(formulaText, bytes, ref written)) return false; + if (!FormulaCellHelper.EndFormulaBeginCachedValue.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(cachedValue.StringValue, bytes, ref written)) return false; + if (!FormulaCellHelper.EndCachedValueEndCell.TryCopyTo(bytes, ref written)) return false; - bytesWritten = written; + buffer.Advance(written); return true; } @@ -134,38 +150,19 @@ public override bool TryWriteEndElement(in Cell cell, SpreadsheetBuffer buffer) return true; } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { - var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; if (styleId is null) { - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStringFormulaCell.TryCopyTo(bytes, ref written)) return false; - buffer.Advance(written); - return true; - } - - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"str\">"u8.TryCopyTo(bytes, ref written)) return false; - + if (!BeginStringFormulaCell.TryCopyTo(bytes, ref written)) return false; buffer.Advance(written); return true; } - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; - } - + if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; @@ -173,42 +170,75 @@ public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? return true; } - public override bool WriteStartElement(CellWriterState state) + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStringCell.TryCopyTo(bytes, ref written)) return false; - } - else + if (styleId is null) { if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"inlineStr\">"u8.TryCopyTo(bytes, ref written)) return false; + if (!"\" t=\"str\">"u8.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!FormulaCellHelper.EndStyleBeginFormula.TryCopyTo(bytes, ref written)) return false; + buffer.Advance(written); return true; } - public override bool WriteStartElement(StyleId styleId, CellWriterState state) + public override bool WriteStartElement(SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!BeginStringCell.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteStartElement(StyleId styleId, SpreadsheetBuffer buffer) + { + var bytes = buffer.GetSpan(); + var written = 0; + + if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; + if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; + if (!EndStyleBeginInlineString.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteStartElementWithReference(CellWriterState state) { var buffer = state.Buffer; var bytes = buffer.GetSpan(); var written = 0; - if (!state.WriteCellReferenceAttributes) - { - if (!BeginStyledStringCell.TryCopyTo(bytes, ref written)) return false; - } - else - { - if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; - if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; - } + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"inlineStr\">"u8.TryCopyTo(bytes, ref written)) return false; + + buffer.Advance(written); + return true; + } + + public override bool WriteStartElementWithReference(StyleId styleId, CellWriterState state) + { + var buffer = state.Buffer; + var bytes = buffer.GetSpan(); + var written = 0; + if (!TryWriteCellStartWithReference(state, bytes, ref written)) return false; + if (!"\" t=\"inlineStr\" s=\""u8.TryCopyTo(bytes, ref written)) return false; if (!SpanHelper.TryWrite(styleId.Id, bytes, ref written)) return false; if (!EndStyleBeginInlineString.TryCopyTo(bytes, ref written)) return false; diff --git a/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs index f5614fbd..1df01cd8 100644 --- a/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs @@ -15,23 +15,43 @@ protected override bool TryWriteValue(in DataCell cell, Span destination, return Utf8Formatter.TryFormat(cell.NumberValue.DoubleValue, destination, out bytesWritten); } - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { var defaultStyleId = defaultStyling?.DateTimeStyleId; return defaultStyleId is not null - ? TryWriteCell(cell, defaultStyleId.Value, state) - : TryWriteCell(cell, state); + ? TryWriteCell(cell, defaultStyleId.Value, buffer) + : TryWriteCell(cell, buffer); } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; - return TryWriteCell(formulaText, cachedValue, actualStyleId, state); + return TryWriteCell(formulaText, cachedValue, actualStyleId, buffer); } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + { + var defaultStyleId = defaultStyling?.DateTimeStyleId; + return defaultStyleId is not null + ? TryWriteCellWithReference(cell, defaultStyleId.Value, state) + : TryWriteCellWithReference(cell, state); + } + + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; + return TryWriteCellWithReference(formulaText, cachedValue, actualStyleId, state); + } + + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) + { + var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; + return WriteFormulaStartElement(actualStyleId, buffer); + } + + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) { var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; - return WriteFormulaStartElement(actualStyleId, state); + return WriteFormulaStartElementWithReference(actualStyleId, state); } } diff --git a/SpreadCheetah/CellValueWriters/Time/NullDateTimeCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Time/NullDateTimeCellValueWriter.cs index 7972c907..622b5c60 100644 --- a/SpreadCheetah/CellValueWriters/Time/NullDateTimeCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Time/NullDateTimeCellValueWriter.cs @@ -8,23 +8,43 @@ internal sealed class NullDateTimeCellValueWriter : NullValueWriterBase { protected override int GetStyleId(StyleId styleId) => styleId.DateTimeId; - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { var defaultStyleId = defaultStyling?.DateTimeStyleId; return defaultStyleId is not null - ? TryWriteCell(defaultStyleId.Value, state) - : TryWriteCell(state); + ? TryWriteCell(defaultStyleId.Value, buffer) + : TryWriteCell(buffer); } - public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCell(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) { var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; - return TryWriteCell(formulaText, actualStyleId, state); + return TryWriteCell(formulaText, actualStyleId, buffer); } - public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) + { + var defaultStyleId = defaultStyling?.DateTimeStyleId; + return defaultStyleId is not null + ? TryWriteCellWithReference(defaultStyleId.Value, state) + : TryWriteCellWithReference(state); + } + + public override bool TryWriteCellWithReference(string formulaText, in DataCell cachedValue, StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) + { + var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; + return TryWriteCellWithReference(formulaText, actualStyleId, state); + } + + public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer) + { + var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; + return WriteFormulaStartElement(actualStyleId, buffer); + } + + public override bool WriteFormulaStartElementWithReference(StyleId? styleId, DefaultStyling? defaultStyling, CellWriterState state) { var actualStyleId = styleId?.DateTimeId ?? defaultStyling?.DateTimeStyleId; - return WriteFormulaStartElement(actualStyleId, state); + return WriteFormulaStartElementWithReference(actualStyleId, state); } } diff --git a/SpreadCheetah/CellWriters/BaseCellWriter.cs b/SpreadCheetah/CellWriters/BaseCellWriter.cs index 13de1bf1..4673e5a9 100644 --- a/SpreadCheetah/CellWriters/BaseCellWriter.cs +++ b/SpreadCheetah/CellWriters/BaseCellWriter.cs @@ -265,4 +265,34 @@ private async ValueTask WriteCellPieceByPieceAsync(T cell, Stream stream, Cancel await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false); TryWriteEndElement(cell); } + + protected bool FinishWritingFormulaCellValue(in Cell cell, string formulaText, ref int cellValueIndex) + { + // Write the formula + if (cellValueIndex < formulaText.Length) + { + if (!Buffer.WriteLongString(formulaText, ref cellValueIndex)) return false; + + // Finish if there is no cached value to write piece by piece + if (!cell.DataCell.Writer.CanWriteValuePieceByPiece(cell.DataCell)) return true; + } + + // If there is a cached value, we need to write "[FORMULA][CACHEDVALUE]" + var cachedValueStartIndex = formulaText.Length + 1; + + // Write the "" part + if (cellValueIndex < cachedValueStartIndex) + { + var separator = FormulaCellHelper.EndFormulaBeginCachedValue; + if (separator.Length > Buffer.FreeCapacity) return false; + Buffer.Advance(SpanHelper.GetBytes(separator, Buffer.GetSpan())); + cellValueIndex = cachedValueStartIndex; + } + + // Write the cached value + var cachedValueIndex = cellValueIndex - cachedValueStartIndex; + var result = cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cachedValueIndex); + cellValueIndex = cachedValueIndex + cachedValueStartIndex; + return result; + } } diff --git a/SpreadCheetah/CellWriters/CellWithReferenceWriter.cs b/SpreadCheetah/CellWriters/CellWithReferenceWriter.cs new file mode 100644 index 00000000..be3d0204 --- /dev/null +++ b/SpreadCheetah/CellWriters/CellWithReferenceWriter.cs @@ -0,0 +1,41 @@ +using SpreadCheetah.Styling.Internal; + +namespace SpreadCheetah.CellWriters; + +internal sealed class CellWithReferenceWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) +{ + protected override bool TryWriteCell(in Cell cell) + { + var writer = cell.DataCell.Writer; + return cell switch + { + { Formula: { } f } => writer.TryWriteCellWithReference(f.FormulaText, cell.DataCell, cell.StyleId, DefaultStyling, State), + { StyleId: not null } => writer.TryWriteCellWithReference(cell.DataCell, cell.StyleId, State), + _ => writer.TryWriteCellWithReference(cell.DataCell, DefaultStyling, State) + }; + } + + protected override bool WriteStartElement(in Cell cell) + { + var writer = cell.DataCell.Writer; + return cell switch + { + { Formula: not null } => writer.WriteFormulaStartElementWithReference(cell.StyleId, DefaultStyling, State), + { StyleId: not null } => writer.WriteStartElementWithReference(cell.StyleId, State), + _ => writer.WriteStartElementWithReference(State) + }; + } + + protected override bool TryWriteEndElement(in Cell cell) + { + return cell.DataCell.Writer.TryWriteEndElement(cell, Buffer); + } + + protected override bool FinishWritingCellValue(in Cell cell, ref int cellValueIndex) + { + return cell.Formula is { } formula + ? FinishWritingFormulaCellValue(cell, formula.FormulaText, ref cellValueIndex) + : cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cellValueIndex); + } +} diff --git a/SpreadCheetah/CellWriters/CellWriter.cs b/SpreadCheetah/CellWriters/CellWriter.cs index 57cbd666..217d03b6 100644 --- a/SpreadCheetah/CellWriters/CellWriter.cs +++ b/SpreadCheetah/CellWriters/CellWriter.cs @@ -3,25 +3,21 @@ namespace SpreadCheetah.CellWriters; -internal sealed class CellWriter : BaseCellWriter +internal sealed class CellWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) { - public CellWriter(CellWriterState state, DefaultStyling? defaultStyling) - : base(state, defaultStyling) - { - } - protected override bool TryWriteCell(in Cell cell) => cell switch { - { Formula: not null } => cell.DataCell.Writer.TryWriteCell(cell.Formula.Value.FormulaText, cell.DataCell, cell.StyleId, DefaultStyling, State), - { StyleId: not null } => cell.DataCell.Writer.TryWriteCell(cell.DataCell, cell.StyleId, State), - _ => cell.DataCell.Writer.TryWriteCell(cell.DataCell, DefaultStyling, State) + { Formula: not null } => cell.DataCell.Writer.TryWriteCell(cell.Formula.Value.FormulaText, cell.DataCell, cell.StyleId, DefaultStyling, Buffer), + { StyleId: not null } => cell.DataCell.Writer.TryWriteCell(cell.DataCell, cell.StyleId, Buffer), + _ => cell.DataCell.Writer.TryWriteCell(cell.DataCell, DefaultStyling, Buffer) }; protected override bool WriteStartElement(in Cell cell) => cell switch { - { Formula: not null } => cell.DataCell.Writer.WriteFormulaStartElement(cell.StyleId, DefaultStyling, State), - { StyleId: not null } => cell.DataCell.Writer.WriteStartElement(cell.StyleId, State), - _ => cell.DataCell.Writer.WriteStartElement(State) + { Formula: not null } => cell.DataCell.Writer.WriteFormulaStartElement(cell.StyleId, DefaultStyling, Buffer), + { StyleId: not null } => cell.DataCell.Writer.WriteStartElement(cell.StyleId, Buffer), + _ => cell.DataCell.Writer.WriteStartElement(Buffer) }; protected override bool TryWriteEndElement(in Cell cell) @@ -31,36 +27,8 @@ protected override bool TryWriteEndElement(in Cell cell) protected override bool FinishWritingCellValue(in Cell cell, ref int cellValueIndex) { - if (cell.Formula is null) - return cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cellValueIndex); - - var formulaText = cell.Formula.Value.FormulaText; - - // Write the formula - if (cellValueIndex < formulaText.Length) - { - if (!Buffer.WriteLongString(formulaText, ref cellValueIndex)) return false; - - // Finish if there is no cached value to write piece by piece - if (!cell.DataCell.Writer.CanWriteValuePieceByPiece(cell.DataCell)) return true; - } - - // If there is a cached value, we need to write "[FORMULA][CACHEDVALUE]" - var cachedValueStartIndex = formulaText.Length + 1; - - // Write the "" part - if (cellValueIndex < cachedValueStartIndex) - { - var separator = FormulaCellHelper.EndFormulaBeginCachedValue; - if (separator.Length > Buffer.FreeCapacity) return false; - Buffer.Advance(SpanHelper.GetBytes(separator, Buffer.GetSpan())); - cellValueIndex = cachedValueStartIndex; - } - - // Write the cached value - var cachedValueIndex = cellValueIndex - cachedValueStartIndex; - var result = cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cachedValueIndex); - cellValueIndex = cachedValueIndex + cachedValueStartIndex; - return result; + return cell.Formula is { } formula + ? FinishWritingFormulaCellValue(cell, formula.FormulaText, ref cellValueIndex) + : cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cellValueIndex); } } diff --git a/SpreadCheetah/CellWriters/CellWriterState.cs b/SpreadCheetah/CellWriters/CellWriterState.cs index 4f091d3c..d2f7962f 100644 --- a/SpreadCheetah/CellWriters/CellWriterState.cs +++ b/SpreadCheetah/CellWriters/CellWriterState.cs @@ -1,15 +1,8 @@ namespace SpreadCheetah.CellWriters; -internal sealed class CellWriterState +internal sealed class CellWriterState(SpreadsheetBuffer buffer) { - public SpreadsheetBuffer Buffer { get; } - public bool WriteCellReferenceAttributes { get; } + public SpreadsheetBuffer Buffer { get; } = buffer; public uint NextRowIndex { get; set; } = 1; public int Column { get; set; } - - public CellWriterState(SpreadsheetBuffer buffer, bool writeCellReferenceAttributes) - { - Buffer = buffer; - WriteCellReferenceAttributes = writeCellReferenceAttributes; - } } diff --git a/SpreadCheetah/CellWriters/DataCellWithReferenceWriter.cs b/SpreadCheetah/CellWriters/DataCellWithReferenceWriter.cs new file mode 100644 index 00000000..3cdb96ad --- /dev/null +++ b/SpreadCheetah/CellWriters/DataCellWithReferenceWriter.cs @@ -0,0 +1,27 @@ +using SpreadCheetah.Styling.Internal; + +namespace SpreadCheetah.CellWriters; + +internal sealed class DataCellWithReferenceWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) +{ + protected override bool TryWriteCell(in DataCell cell) + { + return cell.Writer.TryWriteCellWithReference(cell, DefaultStyling, State); + } + + protected override bool WriteStartElement(in DataCell cell) + { + return cell.Writer.WriteStartElementWithReference(State); + } + + protected override bool TryWriteEndElement(in DataCell cell) + { + return cell.Writer.TryWriteEndElement(Buffer); + } + + protected override bool FinishWritingCellValue(in DataCell cell, ref int cellValueIndex) + { + return Buffer.WriteLongString(cell.StringValue, ref cellValueIndex); + } +} diff --git a/SpreadCheetah/CellWriters/DataCellWriter.cs b/SpreadCheetah/CellWriters/DataCellWriter.cs index f3c682ae..a8ad5a74 100644 --- a/SpreadCheetah/CellWriters/DataCellWriter.cs +++ b/SpreadCheetah/CellWriters/DataCellWriter.cs @@ -2,21 +2,17 @@ namespace SpreadCheetah.CellWriters; -internal sealed class DataCellWriter : BaseCellWriter +internal sealed class DataCellWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) { - public DataCellWriter(CellWriterState state, DefaultStyling? defaultStyling) - : base(state, defaultStyling) - { - } - protected override bool TryWriteCell(in DataCell cell) { - return cell.Writer.TryWriteCell(cell, DefaultStyling, State); + return cell.Writer.TryWriteCell(cell, DefaultStyling, Buffer); } protected override bool WriteStartElement(in DataCell cell) { - return cell.Writer.WriteStartElement(State); + return cell.Writer.WriteStartElement(Buffer); } protected override bool TryWriteEndElement(in DataCell cell) diff --git a/SpreadCheetah/CellWriters/StyledCellWithReferenceWriter.cs b/SpreadCheetah/CellWriters/StyledCellWithReferenceWriter.cs new file mode 100644 index 00000000..4a1fe680 --- /dev/null +++ b/SpreadCheetah/CellWriters/StyledCellWithReferenceWriter.cs @@ -0,0 +1,31 @@ +using SpreadCheetah.Styling.Internal; + +namespace SpreadCheetah.CellWriters; + +internal sealed class StyledCellWithReferenceWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) +{ + protected override bool TryWriteCell(in StyledCell cell) + { + return cell.StyleId is null + ? cell.DataCell.Writer.TryWriteCellWithReference(cell.DataCell, DefaultStyling, State) + : cell.DataCell.Writer.TryWriteCellWithReference(cell.DataCell, cell.StyleId, State); + } + + protected override bool WriteStartElement(in StyledCell cell) + { + return cell.StyleId is null + ? cell.DataCell.Writer.WriteStartElementWithReference(State) + : cell.DataCell.Writer.WriteStartElementWithReference(cell.StyleId, State); + } + + protected override bool TryWriteEndElement(in StyledCell cell) + { + return cell.DataCell.Writer.TryWriteEndElement(Buffer); + } + + protected override bool FinishWritingCellValue(in StyledCell cell, ref int cellValueIndex) + { + return Buffer.WriteLongString(cell.DataCell.StringValue, ref cellValueIndex); + } +} diff --git a/SpreadCheetah/CellWriters/StyledCellWriter.cs b/SpreadCheetah/CellWriters/StyledCellWriter.cs index c3e2ee73..c24be224 100644 --- a/SpreadCheetah/CellWriters/StyledCellWriter.cs +++ b/SpreadCheetah/CellWriters/StyledCellWriter.cs @@ -2,25 +2,21 @@ namespace SpreadCheetah.CellWriters; -internal sealed class StyledCellWriter : BaseCellWriter +internal sealed class StyledCellWriter(CellWriterState state, DefaultStyling? defaultStyling) + : BaseCellWriter(state, defaultStyling) { - public StyledCellWriter(CellWriterState state, DefaultStyling? defaultStyling) - : base(state, defaultStyling) - { - } - protected override bool TryWriteCell(in StyledCell cell) { return cell.StyleId is null - ? cell.DataCell.Writer.TryWriteCell(cell.DataCell, DefaultStyling, State) - : cell.DataCell.Writer.TryWriteCell(cell.DataCell, cell.StyleId, State); + ? cell.DataCell.Writer.TryWriteCell(cell.DataCell, DefaultStyling, Buffer) + : cell.DataCell.Writer.TryWriteCell(cell.DataCell, cell.StyleId, Buffer); } protected override bool WriteStartElement(in StyledCell cell) { return cell.StyleId is null - ? cell.DataCell.Writer.WriteStartElement(State) - : cell.DataCell.Writer.WriteStartElement(cell.StyleId, State); + ? cell.DataCell.Writer.WriteStartElement(Buffer) + : cell.DataCell.Writer.WriteStartElement(cell.StyleId, Buffer); } protected override bool TryWriteEndElement(in StyledCell cell) diff --git a/SpreadCheetah/Worksheet.cs b/SpreadCheetah/Worksheet.cs index df9e7ad8..7a67bc1d 100644 --- a/SpreadCheetah/Worksheet.cs +++ b/SpreadCheetah/Worksheet.cs @@ -13,9 +13,9 @@ internal sealed class Worksheet : IDisposable, IAsyncDisposable { private readonly Stream _stream; private readonly SpreadsheetBuffer _buffer; - private readonly CellWriter _cellWriter; - private readonly DataCellWriter _dataCellWriter; - private readonly StyledCellWriter _styledCellWriter; + private readonly BaseCellWriter _cellWriter; + private readonly BaseCellWriter _dataCellWriter; + private readonly BaseCellWriter _styledCellWriter; private readonly CellWriterState _state; private Dictionary? _validations; private HashSet? _cellMerges; @@ -28,10 +28,20 @@ public Worksheet(Stream stream, DefaultStyling? defaultStyling, SpreadsheetBuffe { _stream = stream; _buffer = buffer; - _state = new CellWriterState(buffer, writeCellReferenceAttributes); - _cellWriter = new CellWriter(_state, defaultStyling); - _dataCellWriter = new DataCellWriter(_state, defaultStyling); - _styledCellWriter = new StyledCellWriter(_state, defaultStyling); + _state = new CellWriterState(buffer); + + if (writeCellReferenceAttributes) + { + _cellWriter = new CellWithReferenceWriter(_state, defaultStyling); + _dataCellWriter = new DataCellWithReferenceWriter(_state, defaultStyling); + _styledCellWriter = new StyledCellWithReferenceWriter(_state, defaultStyling); + } + else + { + _cellWriter = new CellWriter(_state, defaultStyling); + _dataCellWriter = new DataCellWriter(_state, defaultStyling); + _styledCellWriter = new StyledCellWriter(_state, defaultStyling); + } } public int NextRowNumber => (int)_state.NextRowIndex;