Skip to content

Commit

Permalink
Merge pull request #759 from Akarinnnnn/fix-757
Browse files Browse the repository at this point in the history
Make StringLineGroup returns a count limited Enumerator
  • Loading branch information
xoofx authored Dec 12, 2023
2 parents feeb186 + 2ae2cf9 commit 040a778
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
26 changes: 25 additions & 1 deletion 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,27 @@ 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());

var nonBoxedEnumerator = text.GetEnumerator();

Assert.True(nonBoxedEnumerator.MoveNext());
currentLine = (StringLine)nonBoxedEnumerator.Current;
TextAssert.AreEqual("A", currentLine.ToString());
Assert.False(nonBoxedEnumerator.MoveNext());
}
}
35 changes: 32 additions & 3 deletions src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -444,4 +473,4 @@ public LineOffset(int linePosition, int column, int offset, int start, int end)

public readonly int End;
}
}
}

0 comments on commit 040a778

Please sign in to comment.