summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRui Gonçalves <ruippeixotog@gmail.com>2014-05-12 00:29:44 +0100
committerRui Gonçalves <ruippeixotog@gmail.com>2014-05-12 00:29:44 +0100
commit7f08e00a3aa0f59314767313977d9f2899a80cd9 (patch)
treebbf7de6e28ca76648b72c31a5911c255f8b9eab6
parent3f28bbed6d1c2d49201da700907aae460f15cd4d (diff)
downloadscala-7f08e00a3aa0f59314767313977d9f2899a80cd9.tar.gz
scala-7f08e00a3aa0f59314767313977d9f2899a80cd9.tar.bz2
scala-7f08e00a3aa0f59314767313977d9f2899a80cd9.zip
SI-8553 WrappedArray throws exception on lastIndexWhere when index out 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.
-rwxr-xr-xsrc/library/scala/collection/IndexedSeqOptimized.scala2
-rw-r--r--test/junit/scala/collection/IndexedSeqOptimizedTest.scala16
2 files changed, 17 insertions, 1 deletions
diff --git a/src/library/scala/collection/IndexedSeqOptimized.scala b/src/library/scala/collection/IndexedSeqOptimized.scala
index ade04e4de8..42cb37aa24 100755
--- a/src/library/scala/collection/IndexedSeqOptimized.scala
+++ b/src/library/scala/collection/IndexedSeqOptimized.scala
@@ -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
}
diff --git a/test/junit/scala/collection/IndexedSeqOptimizedTest.scala b/test/junit/scala/collection/IndexedSeqOptimizedTest.scala
new file mode 100644
index 0000000000..e5382907af
--- /dev/null
+++ b/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))
+ }
+}