diff --git a/src/Markdig.Tests/TestStringSliceList.cs b/src/Markdig.Tests/TestStringSliceList.cs index 1709c4d8..1ce99dda 100644 --- a/src/Markdig.Tests/TestStringSliceList.cs +++ b/src/Markdig.Tests/TestStringSliceList.cs @@ -1,3 +1,4 @@ +using System.Collections; using System.Text; using Markdig.Helpers; @@ -215,4 +216,27 @@ public void TestStringLineGroupCharIteratorForcingIncreaseCapacity() TextAssert.AreEqual("ABC\r\nD\r\n", chars.ToString()); TextAssert.AreEqual("ABC\r\nD", text.ToString()); } -} \ No newline at end of file + + [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()); + + var nonBoxedEnumerator = text.GetEnumerator(); + + Assert.True(nonBoxedEnumerator.MoveNext()); + currentLine = (StringLine)nonBoxedEnumerator.Current; + TextAssert.AreEqual("A", currentLine.ToString()); + Assert.False(nonBoxedEnumerator.MoveNext()); + } +} diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index 22447543..817ae6e1 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -187,9 +187,38 @@ public void Trim() } } - IEnumerator IEnumerable.GetEnumerator() + public struct Enumerator : IEnumerator { - return Lines.GetEnumerator(); + private readonly StringLineGroup _parent; + private int _index; + + public Enumerator(StringLineGroup parent) + { + _parent = parent; + _index = -1; + } + + public object Current => _parent.Lines[_index]; + + public bool MoveNext() + { + return ++_index < _parent.Count; + } + + public void Reset() + { + _index = -1; + } + } + + public Enumerator GetEnumerator() + { + return new Enumerator(this); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } private void IncreaseCapacity() @@ -444,4 +473,4 @@ public LineOffset(int linePosition, int column, int offset, int start, int end) public readonly int End; } -} \ No newline at end of file +}