mirror of
https://github.com/xoofx/markdig.git
synced 2026-11-16 17:30:52 +00:00
Eliminate a few allocations to hold indents
This commit is contained in:
@@ -67,10 +67,10 @@ namespace Markdig.Renderers.Roundtrip
|
||||
}
|
||||
else
|
||||
{
|
||||
var indents = new List<string>();
|
||||
foreach (var cbl in obj.CodeBlockLines)
|
||||
var indents = new string[obj.CodeBlockLines.Count];
|
||||
for (int i = 0; i < obj.CodeBlockLines.Count; i++)
|
||||
{
|
||||
indents.Add(cbl.TriviaBefore.ToString());
|
||||
indents[i] = obj.CodeBlockLines[i].TriviaBefore.ToString();
|
||||
}
|
||||
renderer.PushIndent(indents);
|
||||
WriteLeafRawLines(renderer, obj);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
|
||||
@@ -28,7 +27,7 @@ namespace Markdig.Renderers.Roundtrip
|
||||
var bws = listItem.TriviaBefore.ToString();
|
||||
var bullet = listItem.SourceBullet.ToString();
|
||||
var delimiter = listBlock.OrderedDelimiter;
|
||||
renderer.PushIndent(new List<string> { $@"{bws}{bullet}{delimiter}" });
|
||||
renderer.PushIndent(new string[] { $"{bws}{bullet}{delimiter}" });
|
||||
renderer.WriteChildren(listItem);
|
||||
renderer.RenderLinesAfter(listItem);
|
||||
}
|
||||
@@ -45,7 +44,7 @@ namespace Markdig.Renderers.Roundtrip
|
||||
char bullet = listBlock.BulletType;
|
||||
StringSlice aws = listItem.TriviaAfter;
|
||||
|
||||
renderer.PushIndent(new List<string> { $@"{bws}{bullet}{aws}" });
|
||||
renderer.PushIndent(new string[] { $"{bws}{bullet}{aws}" });
|
||||
if (listItem.Count == 0)
|
||||
{
|
||||
renderer.Write(""); // trigger writing of indent
|
||||
|
||||
@@ -18,14 +18,15 @@ namespace Markdig.Renderers.Roundtrip
|
||||
renderer.RenderLinesBefore(quoteBlock);
|
||||
renderer.Write(quoteBlock.TriviaBefore);
|
||||
|
||||
var indents = new List<string>();
|
||||
foreach (var quoteLine in quoteBlock.QuoteLines)
|
||||
var indents = new string[quoteBlock.QuoteLines.Count];
|
||||
for (int i = 0; i < quoteBlock.QuoteLines.Count; i++)
|
||||
{
|
||||
var quoteLine = quoteBlock.QuoteLines[i];
|
||||
var wsb = quoteLine.TriviaBefore.ToString();
|
||||
var quoteChar = quoteLine.QuoteChar ? ">" : "";
|
||||
var spaceAfterQuoteChar = quoteLine.HasSpaceAfterQuoteChar ? " " : "";
|
||||
var wsa = quoteLine.TriviaAfter.ToString();
|
||||
indents.Add(wsb + quoteChar + spaceAfterQuoteChar + wsa);
|
||||
indents[i] = (wsb + quoteChar + spaceAfterQuoteChar + wsa);
|
||||
}
|
||||
bool noChildren = false;
|
||||
if (quoteBlock.Count == 0)
|
||||
|
||||
@@ -71,19 +71,20 @@ namespace Markdig.Renderers
|
||||
/// <seealso cref="RendererBase" />
|
||||
public abstract class TextRendererBase<T> : TextRendererBase where T : TextRendererBase<T>
|
||||
{
|
||||
private class Indent
|
||||
private sealed class Indent
|
||||
{
|
||||
private readonly string? _constant;
|
||||
private readonly Queue<string>? _lineSpecific;
|
||||
private readonly string[]? _lineSpecific;
|
||||
private int position;
|
||||
|
||||
internal Indent(string constant)
|
||||
{
|
||||
_constant = constant;
|
||||
}
|
||||
|
||||
internal Indent(IEnumerable<string> lineSpecific)
|
||||
internal Indent(string[] lineSpecific)
|
||||
{
|
||||
_lineSpecific = new Queue<string>(lineSpecific);
|
||||
_lineSpecific = lineSpecific;
|
||||
}
|
||||
|
||||
internal string Next()
|
||||
@@ -94,9 +95,9 @@ namespace Markdig.Renderers
|
||||
}
|
||||
|
||||
//if (_lineSpecific.Count == 0) throw new Exception("Indents empty");
|
||||
if (_lineSpecific!.Count == 0) return string.Empty;
|
||||
var next = _lineSpecific.Dequeue();
|
||||
return next;
|
||||
if (position == _lineSpecific!.Length) return string.Empty;
|
||||
|
||||
return _lineSpecific![position++];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +161,7 @@ namespace Markdig.Renderers
|
||||
indents.Add(new Indent(indent));
|
||||
}
|
||||
|
||||
public void PushIndent(IEnumerable<string> lineSpecific)
|
||||
public void PushIndent(string[] lineSpecific)
|
||||
{
|
||||
if (indents is null) ThrowHelper.ArgumentNullException(nameof(indents));
|
||||
indents.Add(new Indent(lineSpecific));
|
||||
|
||||
Reference in New Issue
Block a user