Skip to content

Commit

Permalink
Fix AdvanceNoNewLines not updating column count. (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Genteure authored Feb 15, 2024
1 parent 1067efd commit c0e2f94
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Parlot/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,21 @@ public void Advance(int count)
public void AdvanceNoNewLines(int offset)
{
var newOffset = _offset + offset;
var length = _textLength - 1;

// Detect if the cursor will be over Eof
if (newOffset > _textLength - 1)
if (newOffset > length)
{
Eof = true;
_column += newOffset - length;
_offset = _textLength;
_current = NullChar;
return;
}

_current = _buffer[newOffset];
_offset = newOffset;
_column += offset;
}

/// <summary>
Expand Down
57 changes: 57 additions & 0 deletions test/Parlot.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,63 @@ public void AdvanceShouldStopAtEof()
Assert.Equal(1, c.Position.Line);
}

[Fact]
public void AdvanceNoNewLinesShouldCountColumns()
{
var c = new Cursor("123456789");

Assert.Equal('1', c.Current);
Assert.Equal(0, c.Position.Offset);
Assert.Equal(1, c.Position.Column);
Assert.Equal(1, c.Position.Line);

c.AdvanceNoNewLines(4);

Assert.Equal('5', c.Current);
Assert.Equal(4, c.Position.Offset);
Assert.Equal(5, c.Position.Column);
Assert.Equal(1, c.Position.Line);

c.AdvanceNoNewLines(4);

Assert.Equal('9', c.Current);
Assert.Equal(8, c.Position.Offset);
Assert.Equal(9, c.Position.Column);
Assert.Equal(1, c.Position.Line);

c.AdvanceNoNewLines(1);

Assert.Equal(Cursor.NullChar, c.Current);
Assert.Equal(9, c.Position.Offset);
Assert.Equal(10, c.Position.Column);
Assert.Equal(1, c.Position.Line);
}

[Fact]
public void AdvanceNoNewLinesShouldStopAtEof()
{
var c = new Cursor("1234\n5678");

Assert.Equal('1', c.Current);
Assert.Equal(0, c.Position.Offset);
Assert.Equal(1, c.Position.Column);
Assert.Equal(1, c.Position.Line);

c.Advance(5);

Assert.Equal('5', c.Current);
Assert.Equal(5, c.Position.Offset);
Assert.Equal(1, c.Position.Column);
Assert.Equal(2, c.Position.Line);

c.AdvanceNoNewLines(6);

Assert.Equal(Cursor.NullChar, c.Current);
Assert.Equal(9, c.Position.Offset);
Assert.Equal(4, c.Position.Column);
Assert.Equal(2, c.Position.Line);
}

[Fact]
public void ResetPositionShouldMoveToEof()
{
Expand Down

0 comments on commit c0e2f94

Please sign in to comment.