Skip to content

Commit 889495f

Browse files
committed
Merge remote-tracking branch 'origin/1.0.x' into 1.1.x
2 parents cae40c4 + c7b62dc commit 889495f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

shared/src/main/scala/scala/util/parsing/input/OffsetPosition.scala

+6-4
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ case class OffsetPosition(source: CharSequence, offset: Int) extends Position {
6161
* @return the line at `offset` (not including a newline)
6262
*/
6363
def lineContents: String = {
64-
val endIndex = if (source.charAt(index(line) - 1) == '\n') {
65-
index(line) - 1
64+
val lineStart = index(line - 1)
65+
val lineEnd = index(line)
66+
val endIndex = if ( lineStart < lineEnd && source.charAt(lineEnd - 1) == '\n') {
67+
lineEnd - 1
6668
} else {
67-
index(line)
69+
lineEnd
6870
}
69-
source.subSequence(index(line - 1), endIndex).toString
71+
source.subSequence(lineStart, endIndex).toString
7072
}
7173

7274
/** Returns a string representation of the `Position`, of the form `line.column`. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scala.util.parsing.input
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class OffsetPositionTest {
7+
@Test
8+
def printLineContentsWithTrailingNewLine: Unit = {
9+
val op = new OffsetPosition("\n", 1)
10+
assertEquals(op.lineContents, "")
11+
}
12+
13+
@Test
14+
def printLineContentsWithEmptySource: Unit = {
15+
val op = new OffsetPosition("", 0)
16+
assertEquals(op.lineContents, "")
17+
}
18+
}

0 commit comments

Comments
 (0)