Skip to content

Commit

Permalink
Remove temporary duplicate string interpolation handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Jan 20, 2024
1 parent 1fabf77 commit 94eb054
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 151 deletions.
4 changes: 2 additions & 2 deletions SpreadCheetah/CellValueWriters/NullValueWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ protected static bool TryWriteCell(string formulaText, int? styleId, Spreadsheet
{
return buffer.TryWrite(
$"{StyledCellHelper.BeginStyledNumberCell}{style}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{EndFormulaEndCell}");
}

return buffer.TryWrite(
$"{FormulaCellHelper.BeginNumberFormulaCell}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{EndFormulaEndCell}");
}

Expand Down
16 changes: 8 additions & 8 deletions SpreadCheetah/CellValueWriters/StringCellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ internal sealed class StringCellValueWriter : CellValueWriter

public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer)
{
return buffer.TryWriteWithXmlEncode($"{BeginStringCell}{cell.StringValue}{EndStringCell}");
return buffer.TryWrite($"{BeginStringCell}{cell.StringValue}{EndStringCell}");
}

public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer)
{
return buffer.TryWriteWithXmlEncode(
return buffer.TryWrite(
$"{BeginStyledStringCell}{styleId.Id}{EndStyleBeginInlineString}" +
$"{cell.StringValue}" +
$"{EndStringCell}");
Expand All @@ -37,28 +37,28 @@ public override bool TryWriteCell(string formulaText, in DataCell cachedValue, S
{
return buffer.TryWrite(
$"{BeginStyledStringFormulaCell}{style.Id}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.StringValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

return buffer.TryWrite(
$"{BeginStringFormulaCell}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.StringValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state)
{
return state.Buffer.TryWriteWithXmlEncode($"{state}{EndReferenceBeginString}{cell.StringValue}{EndStringCell}");
return state.Buffer.TryWrite($"{state}{EndReferenceBeginString}{cell.StringValue}{EndStringCell}");
}

public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state)
{
return state.Buffer.TryWriteWithXmlEncode(
return state.Buffer.TryWrite(
$"{state}{EndReferenceBeginStyle}{styleId.Id}{EndStyleBeginInlineString}" +
$"{cell.StringValue}" +
$"{EndStringCell}");
Expand All @@ -70,15 +70,15 @@ public override bool TryWriteCellWithReference(string formulaText, in DataCell c
{
return state.Buffer.TryWrite(
$"{state}{EndReferenceBeginFormulaCellStyle}{style.Id}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.StringValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

return state.Buffer.TryWrite(
$"{state}{EndReferenceBeginFormula}" +
$"{new RawString(formulaText)}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.StringValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
Expand Down
141 changes: 0 additions & 141 deletions SpreadCheetah/SpreadsheetBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace SpreadCheetah;

internal readonly record struct RawString(string Value);

internal sealed class SpreadsheetBuffer(byte[] buffer)
{
private readonly byte[] _buffer = buffer;
Expand Down Expand Up @@ -53,17 +51,6 @@ public bool TryWrite([InterpolatedStringHandlerArgument("")] ref TryWriteInterpo
return false;
}

public bool TryWriteWithXmlEncode([InterpolatedStringHandlerArgument("")] ref TryWriteInterpolatedStringHandler2 handler)
{
if (handler._success)
{
Advance(handler._pos);
return true;
}

return false;
}

[InterpolatedStringHandler]
public ref struct TryWriteInterpolatedStringHandler
{
Expand Down Expand Up @@ -140,134 +127,6 @@ public bool AppendFormatted<T>(T value)
return AppendFormatted(s);
}

public bool AppendFormatted(string? value)
{
if (Utf8Helper.TryGetBytes(value, GetSpan(), out int bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

public bool AppendFormatted(RawString value)
{
if (XmlUtility.TryXmlEncodeToUtf8(value.Value.AsSpan(), GetSpan(), out var bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

public bool AppendFormatted(scoped ReadOnlySpan<byte> utf8Value)
{
if (utf8Value.TryCopyTo(GetSpan()))
{
_pos += utf8Value.Length;
return true;
}

return Fail();
}

public bool AppendFormatted(CellWriterState state)
{
var bytes = GetSpan();
var bytesWritten = 0;

if (!"<c r=\""u8.TryCopyTo(bytes, ref bytesWritten)) return Fail();
if (!SpanHelper.TryWriteCellReference(state.Column + 1, state.NextRowIndex - 1, bytes, ref bytesWritten)) return Fail();

_pos += bytesWritten;
return true;
}

private bool Fail()
{
_success = false;
return false;
}
}

[InterpolatedStringHandler]
public ref struct TryWriteInterpolatedStringHandler2
{
private readonly SpreadsheetBuffer _buffer;
internal int _pos;
internal bool _success;

public TryWriteInterpolatedStringHandler2(int literalLength, int formattedCount, SpreadsheetBuffer buffer)
{
_ = literalLength;
_ = formattedCount;
_buffer = buffer;
_success = true;
}

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

[ExcludeFromCodeCoverage]
public bool AppendLiteral(string value)
{
Debug.Fail("Use ReadOnlySpan<byte> instead of string literals");

if (value is not null && Utf8Helper.TryGetBytes(value, GetSpan(), out var bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

public bool AppendFormatted(int value)
{
if (Utf8Formatter.TryFormat(value, GetSpan(), out var bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

public bool AppendFormatted(float value)
{
if (Utf8Formatter.TryFormat(value, GetSpan(), out var bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

public bool AppendFormatted(double value)
{
if (Utf8Formatter.TryFormat(value, GetSpan(), out var bytesWritten))
{
_pos += bytesWritten;
return true;
}

return Fail();
}

[ExcludeFromCodeCoverage]
public bool AppendFormatted<T>(T value)
{
Debug.Fail("Create non-generic overloads to avoid allocations when running on .NET Framework");

string? s = value is IFormattable f
? f.ToString(null, CultureInfo.InvariantCulture)
: value?.ToString();

return AppendFormatted(s);
}

public bool AppendFormatted(string? value)
{
if (XmlUtility.TryXmlEncodeToUtf8(value.AsSpan(), GetSpan(), out var bytesWritten))
Expand Down

0 comments on commit 94eb054

Please sign in to comment.