Skip to content

Commit

Permalink
Make StringLineGroup returns a count limited Enumerator
Browse files Browse the repository at this point in the history
Fixes #757, before we return the array enumerator directly, enumerate it will get phantom empty lines.
  • Loading branch information
Akarinnnnn committed Dec 11, 2023
1 parent feeb186 commit 2ab716b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Markdig.Tests/TestStringSliceList.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Text;

using Markdig.Helpers;
Expand Down Expand Up @@ -215,4 +216,20 @@ public void TestStringLineGroupCharIteratorForcingIncreaseCapacity()
TextAssert.AreEqual("ABC\r\nD\r\n", chars.ToString());
TextAssert.AreEqual("ABC\r\nD", text.ToString());
}

[Test]
public void TestStringLineGroup_EnumeratorReturnsRealLines()
{
string str = "A\r\n";
var text = new StringLineGroup(4)
{
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 0, End = 0 }
};

var enumerator = ((IEnumerable)text).GetEnumerator();
Assert.True(enumerator.MoveNext());
StringLine currentLine = (StringLine)enumerator.Current;
TextAssert.AreEqual("A", currentLine.ToString());
Assert.False(enumerator.MoveNext());
}
}
33 changes: 32 additions & 1 deletion src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,40 @@ public void Trim()
}
}

private struct Enumerator : IEnumerator
{
private readonly StringLineGroup _parent;
private int _index;

public Enumerator(StringLineGroup parent)
{
_parent = parent;
_index = -1;
}

public object Current => _parent.Lines[_index];

public bool MoveNext()
{
if (++_index < _parent.Count)
{
return true;
}
else
{
return false;
}
}

public void Reset()
{
_index = -1;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return Lines.GetEnumerator();
return new Enumerator(this);
}

private void IncreaseCapacity()
Expand Down

0 comments on commit 2ab716b

Please sign in to comment.