Skip to content

Commit

Permalink
Merge pull request #32 from sveinungf/dev/array-of-value-writers
Browse files Browse the repository at this point in the history
Get cell value writers from indexing into a static array
  • Loading branch information
sveinungf authored Dec 27, 2023
2 parents 89810d3 + 91c0966 commit 6396a8c
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 60 deletions.
25 changes: 16 additions & 9 deletions SpreadCheetah/CellValueWriters/CellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
using SpreadCheetah.CellWriters;
using SpreadCheetah.Styling;
using SpreadCheetah.Styling.Internal;
using System.Runtime.CompilerServices;

namespace SpreadCheetah.CellValueWriters;

internal abstract class CellValueWriter
{
public static CellValueWriter Null { get; } = new NullValueWriter();
public static CellValueWriter Integer { get; } = new IntegerCellValueWriter();
public static CellValueWriter Float { get; } = new FloatCellValueWriter();
public static CellValueWriter Double { get; } = new DoubleCellValueWriter();
public static CellValueWriter DateTime { get; } = new DateTimeCellValueWriter();
public static CellValueWriter NullDateTime { get; } = new NullDateTimeCellValueWriter();
public static CellValueWriter TrueBoolean { get; } = new TrueBooleanCellValueWriter();
public static CellValueWriter FalseBoolean { get; } = new FalseBooleanCellValueWriter();
public static CellValueWriter String { get; } = new StringCellValueWriter();
private static readonly CellValueWriter[] Writers =
[
new NullValueWriter(),
new IntegerCellValueWriter(),
new FloatCellValueWriter(),
new DoubleCellValueWriter(),
new DateTimeCellValueWriter(),
new NullDateTimeCellValueWriter(),
new TrueBooleanCellValueWriter(),
new FalseBooleanCellValueWriter(),
new StringCellValueWriter()
];

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CellValueWriter GetWriter(CellWriterType type) => Writers[(int)type];

public abstract bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer);
public abstract bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer);
Expand Down
14 changes: 14 additions & 0 deletions SpreadCheetah/CellValueWriters/CellWriterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace SpreadCheetah.CellValueWriters;

internal enum CellWriterType : byte
{
Null,
Integer,
Float,
Double,
DateTime,
NullDateTime,
TrueBoolean,
FalseBoolean,
String
}
7 changes: 5 additions & 2 deletions SpreadCheetah/CellWriters/BaseCellWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Helpers;
using SpreadCheetah.Styling.Internal;
using SpreadCheetah.Worksheets;
Expand Down Expand Up @@ -259,13 +260,15 @@ private async ValueTask WriteCellPieceByPieceAsync(T cell, Stream stream, Cancel

protected bool FinishWritingFormulaCellValue(in Cell cell, string formulaText, ref int cellValueIndex)
{
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);

// 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 (!writer.CanWriteValuePieceByPiece(cell.DataCell)) return true;
}

// If there is a cached value, we need to write "[FORMULA]</f><v>[CACHEDVALUE]"
Expand All @@ -282,7 +285,7 @@ protected bool FinishWritingFormulaCellValue(in Cell cell, string formulaText, r

// Write the cached value
var cachedValueIndex = cellValueIndex - cachedValueStartIndex;
var result = cell.DataCell.Writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cachedValueIndex);
var result = writer.WriteValuePieceByPiece(cell.DataCell, Buffer, ref cachedValueIndex);
cellValueIndex = cachedValueIndex + cachedValueStartIndex;
return result;
}
Expand Down
9 changes: 5 additions & 4 deletions SpreadCheetah/CellWriters/CellWithReferenceWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;
Expand All @@ -7,7 +8,7 @@ internal sealed class CellWithReferenceWriter(CellWriterState state, DefaultStyl
{
protected override bool TryWriteCell(in Cell cell)
{
var writer = cell.DataCell.Writer;
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell switch
{
{ Formula: { } f } => writer.TryWriteCellWithReference(f.FormulaText, cell.DataCell, cell.StyleId, DefaultStyling, State),
Expand All @@ -18,7 +19,7 @@ protected override bool TryWriteCell(in Cell cell)

protected override bool WriteStartElement(in Cell cell)
{
var writer = cell.DataCell.Writer;
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell switch
{
{ Formula: not null } => writer.WriteFormulaStartElementWithReference(cell.StyleId, DefaultStyling, State),
Expand All @@ -29,13 +30,13 @@ protected override bool WriteStartElement(in Cell cell)

protected override bool TryWriteEndElement(in Cell cell)
{
return cell.DataCell.Writer.TryWriteEndElement(cell, Buffer);
return CellValueWriter.GetWriter(cell.DataCell.Type).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);
: CellValueWriter.GetWriter(cell.DataCell.Type).WriteValuePieceByPiece(cell.DataCell, Buffer, ref cellValueIndex);
}
}
34 changes: 22 additions & 12 deletions SpreadCheetah/CellWriters/CellWriter.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;

internal sealed class CellWriter(CellWriterState state, DefaultStyling? defaultStyling)
: BaseCellWriter<Cell>(state, defaultStyling)
{
protected override bool TryWriteCell(in Cell cell) => cell switch
protected override bool TryWriteCell(in Cell cell)
{
{ Formula: { } formula } => cell.DataCell.Writer.TryWriteCell(formula.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)
};
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell switch
{
{ Formula: { } formula } => writer.TryWriteCell(formula.FormulaText, cell.DataCell, cell.StyleId, DefaultStyling, Buffer),
{ StyleId: not null } => writer.TryWriteCell(cell.DataCell, cell.StyleId, Buffer),
_ => writer.TryWriteCell(cell.DataCell, DefaultStyling, Buffer)
};
}

protected override bool WriteStartElement(in Cell cell) => cell switch
protected override bool WriteStartElement(in Cell cell)
{
{ 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)
};
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell switch
{
{ Formula: not null } => writer.WriteFormulaStartElement(cell.StyleId, DefaultStyling, Buffer),
{ StyleId: not null } => writer.WriteStartElement(cell.StyleId, Buffer),
_ => writer.WriteStartElement(Buffer)
};
}

protected override bool TryWriteEndElement(in Cell cell)
{
return cell.DataCell.Writer.TryWriteEndElement(cell, Buffer);
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return 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);
: CellValueWriter.GetWriter(cell.DataCell.Type).WriteValuePieceByPiece(cell.DataCell, Buffer, ref cellValueIndex);
}
}
7 changes: 4 additions & 3 deletions SpreadCheetah/CellWriters/DataCellWithReferenceWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;
Expand All @@ -7,17 +8,17 @@ internal sealed class DataCellWithReferenceWriter(CellWriterState state, Default
{
protected override bool TryWriteCell(in DataCell cell)
{
return cell.Writer.TryWriteCellWithReference(cell, DefaultStyling, State);
return CellValueWriter.GetWriter(cell.Type).TryWriteCellWithReference(cell, DefaultStyling, State);
}

protected override bool WriteStartElement(in DataCell cell)
{
return cell.Writer.WriteStartElementWithReference(State);
return CellValueWriter.GetWriter(cell.Type).WriteStartElementWithReference(State);
}

protected override bool TryWriteEndElement(in DataCell cell)
{
return cell.Writer.TryWriteEndElement(Buffer);
return CellValueWriter.GetWriter(cell.Type).TryWriteEndElement(Buffer);
}

protected override bool FinishWritingCellValue(in DataCell cell, ref int cellValueIndex)
Expand Down
7 changes: 4 additions & 3 deletions SpreadCheetah/CellWriters/DataCellWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;
Expand All @@ -7,17 +8,17 @@ internal sealed class DataCellWriter(CellWriterState state, DefaultStyling? defa
{
protected override bool TryWriteCell(in DataCell cell)
{
return cell.Writer.TryWriteCell(cell, DefaultStyling, Buffer);
return CellValueWriter.GetWriter(cell.Type).TryWriteCell(cell, DefaultStyling, Buffer);
}

protected override bool WriteStartElement(in DataCell cell)
{
return cell.Writer.WriteStartElement(Buffer);
return CellValueWriter.GetWriter(cell.Type).WriteStartElement(Buffer);
}

protected override bool TryWriteEndElement(in DataCell cell)
{
return cell.Writer.TryWriteEndElement(Buffer);
return CellValueWriter.GetWriter(cell.Type).TryWriteEndElement(Buffer);
}

protected override bool FinishWritingCellValue(in DataCell cell, ref int cellValueIndex)
Expand Down
14 changes: 9 additions & 5 deletions SpreadCheetah/CellWriters/StyledCellWithReferenceWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;
Expand All @@ -7,21 +8,24 @@ internal sealed class StyledCellWithReferenceWriter(CellWriterState state, Defau
{
protected override bool TryWriteCell(in StyledCell cell)
{
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell.StyleId is null
? cell.DataCell.Writer.TryWriteCellWithReference(cell.DataCell, DefaultStyling, State)
: cell.DataCell.Writer.TryWriteCellWithReference(cell.DataCell, cell.StyleId, State);
? writer.TryWriteCellWithReference(cell.DataCell, DefaultStyling, State)
: writer.TryWriteCellWithReference(cell.DataCell, cell.StyleId, State);
}

protected override bool WriteStartElement(in StyledCell cell)
{
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return cell.StyleId is null
? cell.DataCell.Writer.WriteStartElementWithReference(State)
: cell.DataCell.Writer.WriteStartElementWithReference(cell.StyleId, State);
? writer.WriteStartElementWithReference(State)
: writer.WriteStartElementWithReference(cell.StyleId, State);
}

protected override bool TryWriteEndElement(in StyledCell cell)
{
return cell.DataCell.Writer.TryWriteEndElement(Buffer);
var writer = CellValueWriter.GetWriter(cell.DataCell.Type);
return writer.TryWriteEndElement(Buffer);
}

protected override bool FinishWritingCellValue(in StyledCell cell, ref int cellValueIndex)
Expand Down
11 changes: 6 additions & 5 deletions SpreadCheetah/CellWriters/StyledCellWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SpreadCheetah.CellValueWriters;
using SpreadCheetah.Styling.Internal;

namespace SpreadCheetah.CellWriters;
Expand All @@ -8,20 +9,20 @@ internal sealed class StyledCellWriter(CellWriterState state, DefaultStyling? de
protected override bool TryWriteCell(in StyledCell cell)
{
return cell.StyleId is null
? cell.DataCell.Writer.TryWriteCell(cell.DataCell, DefaultStyling, Buffer)
: cell.DataCell.Writer.TryWriteCell(cell.DataCell, cell.StyleId, Buffer);
? CellValueWriter.GetWriter(cell.DataCell.Type).TryWriteCell(cell.DataCell, DefaultStyling, Buffer)
: CellValueWriter.GetWriter(cell.DataCell.Type).TryWriteCell(cell.DataCell, cell.StyleId, Buffer);
}

protected override bool WriteStartElement(in StyledCell cell)
{
return cell.StyleId is null
? cell.DataCell.Writer.WriteStartElement(Buffer)
: cell.DataCell.Writer.WriteStartElement(cell.StyleId, Buffer);
? CellValueWriter.GetWriter(cell.DataCell.Type).WriteStartElement(Buffer)
: CellValueWriter.GetWriter(cell.DataCell.Type).WriteStartElement(cell.StyleId, Buffer);
}

protected override bool TryWriteEndElement(in StyledCell cell)
{
return cell.DataCell.Writer.TryWriteEndElement(Buffer);
return CellValueWriter.GetWriter(cell.DataCell.Type).TryWriteEndElement(Buffer);
}

protected override bool FinishWritingCellValue(in StyledCell cell, ref int cellValueIndex)
Expand Down
Loading

0 comments on commit 6396a8c

Please sign in to comment.