Make StringLineGroup returns a count limited Enumerator

Fixes #757, before we return the array enumerator directly, enumerate it will get phantom empty lines.
This commit is contained in:
Fa鸽
2023-12-11 19:57:51 +08:00
parent feeb1867ce
commit 2ab716bec1
2 changed files with 49 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections;
using System.Text;
using Markdig.Helpers;
@@ -215,4 +216,20 @@ public class TestStringSliceList
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());
}
}

View File

@@ -187,9 +187,40 @@ public struct StringLineGroup : IEnumerable
}
}
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()