Skip to content

Commit

Permalink
bugfix: support carriage return
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Sep 22, 2023
1 parent a5528cb commit 6efc5e0
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ trait InternalInput {
private[meta] lazy val tokenCache: mutable.Map[Dialect, Tokens] =
Compat.newMutableMap[Dialect, Tokens]

private val newLine = Set('\n', '\r')

// NOTE: It's regrettable that we need to taint the pure abstraction of Input.
// However, as #334 shows, we just can't redo offset -> line conversions over and over again.
// This means that we gotta cache, and this instance is really the only place versatile enough.
Expand All @@ -21,8 +23,12 @@ trait InternalInput {
val buf = new mutable.ArrayBuffer[Int]
buf += 0
var i = 0
var lastIsCR = false
while (i < chars.length) {
// we consider all `\n`, `\r\n` and `\r` as new line
if (chars(i) == '\n') buf += (i + 1)
else if (lastIsCR) buf += i
lastIsCR = chars(i) == '\r'
i += 1
}
if (buf.last != chars.length) buf += chars.length // sentinel value used for binary search
Expand Down Expand Up @@ -50,7 +56,7 @@ trait InternalInput {
// NOTE: chars.length requires a really ugly special case.
// If the file doesn't end with \n, then it's simply last_line:last_col+1.
// But if the file does end with \n, then it's last_line+1:0.
if (offset == chars.length && (0 < chars.length && chars(offset - 1) == '\n')) {
if (offset == chars.length && (0 < chars.length && newLine(chars(offset - 1)))) {
return a.length - 1
}
var lo = 0
Expand Down

0 comments on commit 6efc5e0

Please sign in to comment.