Skip to content

Commit

Permalink
SI-8553 WrappedArray throws exception on lastIndexWhere when index ou…
Browse files Browse the repository at this point in the history
…t of range

Adds a check in `IndexedSeqOptimized#lastIndexWhere(A => Boolean, Int)`
to begin searching in the end of the collection if `end` is greater than
the collection's length.

Discussed in
https://groups.google.com/d/topic/scala-internals/-MacXivbY0Q/discussion.
  • Loading branch information
ruippeixotog committed May 11, 2014
1 parent 3f28bbe commit 7f08e00
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/library/scala/collection/IndexedSeqOptimized.scala
Expand Up @@ -206,7 +206,7 @@ trait IndexedSeqOptimized[+A, +Repr] extends Any with IndexedSeqLike[A, Repr] {

override /*SeqLike*/
def lastIndexWhere(p: A => Boolean, end: Int): Int = {
var i = end
var i = math.min(end, length - 1)
while (i >= 0 && !p(this(i))) i -= 1
i
}
Expand Down
16 changes: 16 additions & 0 deletions test/junit/scala/collection/IndexedSeqOptimizedTest.scala
@@ -0,0 +1,16 @@
package scala.collection

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Assert._
import org.junit.Test

@RunWith(classOf[JUnit4])
class IndexedSeqOptimizedTest {

@Test
def notThrowsAnExceptionInLastIndexOf() {
assertEquals(0, (Array(2): collection.mutable.WrappedArray[Int]).lastIndexWhere(_ => true, 1))
assertEquals(2, "abc123".lastIndexWhere(_.isLetter, 6))
}
}

0 comments on commit 7f08e00

Please sign in to comment.