Skip to content

Commit 0b82557

Browse files
committed
Make ArrayOps.startsWith return true for out-of-bounds empty array
This is consistent with Seqs (both old and new).
1 parent 4046983 commit 0b82557

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/library/scala/collection/ArrayOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ final class ArrayOps[A](val xs: Array[A]) extends AnyVal {
13001300
*/
13011301
def startsWith[B >: A](that: Array[B], offset: Int): Boolean = {
13021302
val thatl = that.length
1303-
if(thatl > xs.length-offset) false
1303+
if(thatl > xs.length-offset) thatl == 0
13041304
else {
13051305
var i = 0
13061306
while(i < thatl) {

test/junit/scala/collection/ArrayOpsTest.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ class ArrayOpsTest {
6969
assertArrayEquals(Array(0), zero)
7070
}
7171

72+
@Test
73+
def startsWith: Unit = {
74+
val l0 = Nil
75+
val l1 = 1 :: Nil
76+
val a0 = Array[Int]()
77+
val a1 = Array[Int](1)
78+
assertEquals(l0.startsWith(l0, 0), a0.startsWith(a0, 0))
79+
assertEquals(l0.startsWith(l0, 1), a0.startsWith(a0, 1))
80+
assertEquals(l0.startsWith(l1, 0), a0.startsWith(a1, 0))
81+
assertEquals(l0.startsWith(l1, 1), a0.startsWith(a1, 1))
82+
}
83+
7284
@Test
7385
def patch(): Unit = {
7486
val a1 = Array.empty[Int]

0 commit comments

Comments
 (0)