Skip to content

Commit

Permalink
feat: 性能优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
CYJB committed Mar 16, 2024
1 parent 2197db5 commit 5580f08
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions Cyjb.Markdown/ParseBlock/LineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ public MappedText Text
lastMappedIndex = token.Span.Start;
lastLength = token.Text.Length;
length += lastLength;
texts.Add(token.Text);
if (texts.Count > 0 && texts[^1].TryConcat(token.Text, out var concated))
{
texts[^1] = concated;
}
else
{
texts.Add(token.Text);
}
spanBuilder.Add(token.Span);
}
text = new MappedText(texts, length, spanBuilder.GetSpan(), maps);
Expand Down Expand Up @@ -213,7 +220,9 @@ public void SkipIndent(int count)
indentStartColumn += count;
if (indentStartColumn >= indentEndColumn)
{
SkipIndent();
indentStartColumn = indentEndColumn;
start = indentEnd;
text = null;
return;
}
int column;
Expand Down Expand Up @@ -273,28 +282,7 @@ public void AppendTo(StringBuilder builder)
// 添加缩进。
if (hasIndent && Indent > 0)
{
if (indentStartColumn == indentEndColumn)
{
// 所有缩进均已消费。
return;
}
int column = locator.GetPosition(start).Column;
if (column == indentStartColumn)
{
builder.Append(indentText.AsSpan(start - indentOriginalStart));
}
else
{
// 当前是部分 Tab,需要使用空格补齐 column(start) 到 startColumn 的位置。
column = locator.GetPosition(start + 1).Column;
builder.Append(' ', column - indentStartColumn);
int idx = start + 1 - indentOriginalStart;
// 存在 Tab 时,可能会出现列数超出字符数的场景。
if (idx < indentText.Length)
{
builder.Append(indentText.AsSpan(idx));
}
}
builder.Append(GetIndentText().AsSpan());
}
// 添加剩余词法单元。
int count = tokens.Count;
Expand Down Expand Up @@ -399,11 +387,6 @@ internal Token<BlockKind> Read()
/// <returns>缩进文本。</returns>
private StringView GetIndentText()
{
if (indentStartColumn == indentEndColumn)
{
// 所有缩进均已消费。
return StringView.Empty;
}
int column = locator.GetPosition(start).Column;
if (column == indentStartColumn)
{
Expand Down

0 comments on commit 5580f08

Please sign in to comment.