mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Added line count check to avoid out of bounds
This commit is contained in:
55
src/Markdig.Tests/TestStringLineGroup.cs
Normal file
55
src/Markdig.Tests/TestStringLineGroup.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Markdig.Helpers;
|
||||
using System.Text;
|
||||
|
||||
namespace Markdig.Tests;
|
||||
|
||||
[TestFixture]
|
||||
public class TestStringLineGroup
|
||||
{
|
||||
private static string ToString(ICharIterator text)
|
||||
{
|
||||
var chars = new StringBuilder();
|
||||
while (text.CurrentChar != '\0')
|
||||
{
|
||||
chars.Append(text.CurrentChar);
|
||||
text.NextChar();
|
||||
}
|
||||
return chars.ToString();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestStringLineGroupCharIteratorAtCapacity()
|
||||
{
|
||||
string str = "ABCDEFGHI";
|
||||
var text = new StringLineGroup(1)
|
||||
{
|
||||
// Will store the following line at capacity
|
||||
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 0, End = 2 },
|
||||
};
|
||||
|
||||
var iterator = text.ToCharIterator();
|
||||
var chars = ToString(iterator);
|
||||
TextAssert.AreEqual("ABC\r\n", chars.ToString());
|
||||
TextAssert.AreEqual("ABC", text.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestStringLineGroupCharIteratorForcingIncreaseCapacity()
|
||||
{
|
||||
string str = "ABCDEFGHI";
|
||||
var text = new StringLineGroup(1)
|
||||
{
|
||||
// Will store the following line at capacity
|
||||
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 0, End = 2 },
|
||||
|
||||
// Will force increase capacity to 2 and store the line at capacity
|
||||
new StringSlice(str, NewLine.CarriageReturnLineFeed) { Start = 3, End = 3 },
|
||||
};
|
||||
|
||||
var iterator = text.ToCharIterator();
|
||||
var chars = ToString(iterator);
|
||||
TextAssert.AreEqual("ABC\r\nD\r\n", chars.ToString());
|
||||
TextAssert.AreEqual("ABC\r\nD", text.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -334,9 +334,12 @@ public struct StringLineGroup : IEnumerable
|
||||
goto Return;
|
||||
|
||||
MoveToNewLine:
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
_currentSlice = _lines.Lines[SliceIndex];
|
||||
if (SliceIndex < _lines.Count - 1)
|
||||
{
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
_currentSlice = _lines.Lines[SliceIndex];
|
||||
}
|
||||
|
||||
Return:
|
||||
return CurrentChar;
|
||||
|
||||
Reference in New Issue
Block a user