Skip to content

Commit

Permalink
Merge pull request #220 from veelo/fix_eol
Browse files Browse the repository at this point in the history
Prevent position() from counting DOS line endings twice.
  • Loading branch information
PhilippeSigaud authored Feb 26, 2017
2 parents 806c077 + 44c1c3b commit 8139a50
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions pegged/peg.d
Original file line number Diff line number Diff line change
Expand Up @@ -386,30 +386,26 @@ assert(position("abc
*/
Position position(string s)
{
size_t col, line, index;
version (tracer)
{
// Do not trace the eol scan below, prevent recursion.
traceBlocked = true;
}
size_t col, line, index, prev_i;
char prev_c;
foreach(i,c; s)
{
if (eol(ParseTree("", false, [], s, 0,i)).successful)
if ((c == '\n' && !(i == prev_i + 1 && prev_c == '\r')) || // new line except when directly following a carriage return.
c == '\r')
{
col = 0;
++line;
++index;
prev_i = i;
prev_c = c;
}
else
{
++col;
if (c != '\n')
++col;
++index;
}
}
version (tracer)
{
traceBlocked = false;
}

return Position(line,col,index);
}
Expand Down Expand Up @@ -438,6 +434,7 @@ unittest
") == Position(3,0,3), "Four lines, all empty.");
assert(position("one\r\ntwo\r\nthree") == Position(2, 5, 15), "Three lines, DOS line endings");
}

string getName(alias expr)() @property
Expand Down

0 comments on commit 8139a50

Please sign in to comment.