Skip to content

Commit

Permalink
Merge pull request #399 from KrisVandermotten/StringLineGroup
Browse files Browse the repository at this point in the history
Some optimizations in StringLineGroup
  • Loading branch information
xoofx authored Feb 18, 2020
2 parents 51f9da1 + 7d61df2 commit ef5b958
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ public void Clear()
/// <param name="index">The index.</param>
public void RemoveAt(int index)
{
if (Count - 1 == index)
{
Count--;
}
else
if (index != Count - 1)
{
Array.Copy(Lines, index + 1, Lines, index, Count - index - 1);
Lines[Count - 1] = new StringLine();
Count--;
}

Lines[Count - 1] = new StringLine();
Count--;
}

/// <summary>
Expand Down Expand Up @@ -115,22 +112,19 @@ public readonly override string ToString()
/// <returns>A single slice concatenating the lines of this instance</returns>
public readonly StringSlice ToSlice(List<LineOffset> lineOffsets = null)
{
// Optimization case when no lines
if (Count == 0)
{
return new StringSlice(string.Empty);
}

// Optimization case for a single line.
if (Count == 1)
{
if (lineOffsets != null)
{
lineOffsets.Add(new LineOffset(Lines[0].Position, Lines[0].Column, Lines[0].Slice.Start - Lines[0].Position, Lines[0].Slice.Start, Lines[0].Slice.End + 1));
}
lineOffsets?.Add(new LineOffset(Lines[0].Position, Lines[0].Column, Lines[0].Slice.Start - Lines[0].Position, Lines[0].Slice.Start, Lines[0].Slice.End + 1));
return Lines[0];
}

// Optimization case when no lines
if (Count == 0)
{
return new StringSlice(string.Empty);
}

if (lineOffsets != null && lineOffsets.Capacity < lineOffsets.Count + Count)
{
lineOffsets.Capacity = Math.Max(lineOffsets.Count + Count, lineOffsets.Capacity * 2);
Expand All @@ -143,21 +137,16 @@ public readonly StringSlice ToSlice(List<LineOffset> lineOffsets = null)
{
if (i > 0)
{
if (lineOffsets != null)
{
lineOffsets.Add(new LineOffset(Lines[i - 1].Position, Lines[i - 1].Column, Lines[i - 1].Slice.Start - Lines[i - 1].Position, previousStartOfLine, builder.Length));
}
builder.Append('\n');
previousStartOfLine = builder.Length;
}
if (!Lines[i].Slice.IsEmpty)
ref var line = ref Lines[i];
if (!line.Slice.IsEmpty)
{
builder.Append(Lines[i].Slice.Text, Lines[i].Slice.Start, Lines[i].Slice.Length);
builder.Append(line.Slice.Text, line.Slice.Start, line.Slice.Length);
}
}
if (lineOffsets != null)
{
lineOffsets.Add(new LineOffset(Lines[Count - 1].Position, Lines[Count - 1].Column, Lines[Count - 1].Slice.Start - Lines[Count - 1].Position, previousStartOfLine, builder.Length));

lineOffsets?.Add(new LineOffset(line.Position, line.Column, line.Slice.Start - line.Position, previousStartOfLine, builder.Length));
}
return new StringSlice(builder.GetStringAndReset());
}
Expand Down Expand Up @@ -268,10 +257,10 @@ public StringLineGroup Remaining()
public char NextChar()
{
Start++;
_offset++;
if (Start <= End)
{
var slice = _lines.Lines[SliceIndex].Slice;
_offset++;
if (_offset < slice.Length)
{
CurrentChar = slice[slice.Start + _offset];
Expand All @@ -288,7 +277,6 @@ public char NextChar()
CurrentChar = '\0';
Start = End + 1;
SliceIndex = _lines.Count;
_offset--;
}
return CurrentChar;
}
Expand Down

0 comments on commit ef5b958

Please sign in to comment.