Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StringLineGroup returns a count limited Enumerator #759

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Akarinnnnn marked this conversation as resolved.
Show resolved Hide resolved
{
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;
}
xoofx marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

private void IncreaseCapacity()
Expand Down
Loading