From 2ab716bec1b2240c9ee2df8613dd460ecb1b26b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=E9=B8=BD?= <43724908+Akarinnnnn@users.noreply.github.com> Date: Mon, 11 Dec 2023 19:57:51 +0800 Subject: [PATCH 1/5] Make StringLineGroup returns a count limited Enumerator Fixes #757, before we return the array enumerator directly, enumerate it will get phantom empty lines. --- src/Markdig.Tests/TestStringSliceList.cs | 17 ++++++++++++ src/Markdig/Helpers/StringLineGroup.cs | 33 +++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Markdig.Tests/TestStringSliceList.cs b/src/Markdig.Tests/TestStringSliceList.cs index 1709c4d8b..6136c2464 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,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()); + } } \ No newline at end of file diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index 224475434..28570124d 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -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() From e78833ae30ac75c855979652e3da80369c360a77 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Tue, 12 Dec 2023 08:00:54 +0100 Subject: [PATCH 2/5] Update src/Markdig/Helpers/StringLineGroup.cs --- src/Markdig/Helpers/StringLineGroup.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index 28570124d..80a31059c 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -202,14 +202,7 @@ public Enumerator(StringLineGroup parent) public bool MoveNext() { - if (++_index < _parent.Count) - { - return true; - } - else - { - return false; - } + return ++_index < _parent.Count; } public void Reset() From 65a02e44ec8467a22cdf05125028e735e1d0cbe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=E9=B8=BD?= <43724908+Akarinnnnn@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:45:30 +0800 Subject: [PATCH 3/5] Add `Enumerator GetEnumerator()` for `StringLineGroup` --- src/Markdig/Helpers/StringLineGroup.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index 80a31059c..cef3f6031 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -187,7 +187,7 @@ public void Trim() } } - private struct Enumerator : IEnumerator + public struct Enumerator : IEnumerator { private readonly StringLineGroup _parent; private int _index; @@ -210,12 +210,17 @@ public void Reset() _index = -1; } } - - IEnumerator IEnumerable.GetEnumerator() + + public Enumerator GetEnumerator() { return new Enumerator(this); } + IEnumerator IEnumerable.GetEnumerator() + { + (IEnumerator)GetEnumerator(); + } + private void IncreaseCapacity() { var newItems = _pool.Rent(Lines.Length * 2); @@ -468,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 +} From ba1e562d2fe7418efeb53f466712bbd6ad9c39f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=E9=B8=BD?= <43724908+Akarinnnnn@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:15:55 +0800 Subject: [PATCH 4/5] Fix complication error of `StringLineGroup`. --- src/Markdig/Helpers/StringLineGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index cef3f6031..817ae6e15 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -218,7 +218,7 @@ public Enumerator GetEnumerator() IEnumerator IEnumerable.GetEnumerator() { - (IEnumerator)GetEnumerator(); + return GetEnumerator(); } private void IncreaseCapacity() From 2ae2cf9263d5d0e3752ebf94b0021c5e8bf92d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=E9=B8=BD?= <43724908+Akarinnnnn@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:18:10 +0800 Subject: [PATCH 5/5] Add tests for non-boxed enumerator of `StringLineGroup`. --- src/Markdig.Tests/TestStringSliceList.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Markdig.Tests/TestStringSliceList.cs b/src/Markdig.Tests/TestStringSliceList.cs index 6136c2464..1ce99ddac 100644 --- a/src/Markdig.Tests/TestStringSliceList.cs +++ b/src/Markdig.Tests/TestStringSliceList.cs @@ -231,5 +231,12 @@ public void TestStringLineGroup_EnumeratorReturnsRealLines() 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()); } -} \ No newline at end of file +}