summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2016-09-26 14:29:12 -0700
committerSom Snytt <som.snytt@gmail.com>2016-09-26 14:29:12 -0700
commitae0269200c6e5af8120587e2317f595e746c6114 (patch)
tree0338816b25d52661777c8936ccc6f154f8fae474
parent63f5eb5eb751fa5bfb69d0e783aa360abac82a1b (diff)
downloadscala-ae0269200c6e5af8120587e2317f595e746c6114.tar.gz
scala-ae0269200c6e5af8120587e2317f595e746c6114.tar.bz2
scala-ae0269200c6e5af8120587e2317f595e746c6114.zip
SI-9936 SeqLike.indexWhere starts at zero
This follows the Scaladoc, and makes ``` "abcdef".indexOf('c', -1) ``` work like ``` "abcdef".toVector.indexOf('c', -1) ```
-rw-r--r--src/library/scala/collection/SeqLike.scala2
-rw-r--r--test/junit/scala/collection/SeqLikeTest.scala19
2 files changed, 20 insertions, 1 deletions
diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala
index a26765027c..2d662257e5 100644
--- a/src/library/scala/collection/SeqLike.scala
+++ b/src/library/scala/collection/SeqLike.scala
@@ -113,7 +113,7 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[
}
def indexWhere(p: A => Boolean, from: Int): Int = {
- var i = from
+ var i = from max 0
val it = iterator.drop(from)
while (it.hasNext) {
if (p(it.next())) return i
diff --git a/test/junit/scala/collection/SeqLikeTest.scala b/test/junit/scala/collection/SeqLikeTest.scala
new file mode 100644
index 0000000000..2ab682299d
--- /dev/null
+++ b/test/junit/scala/collection/SeqLikeTest.scala
@@ -0,0 +1,19 @@
+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 SeqLikeTest {
+
+ @Test def `SI-9936 indexWhere`(): Unit = {
+ assertEquals(2, "abcde".indexOf('c', -1))
+ assertEquals(2, "abcde".indexOf('c', -2))
+ assertEquals(2, "abcde".toVector.indexOf('c', -1))
+ assertEquals(2, "abcde".toVector.indexOf('c', -2))
+ assertEquals(2, "abcde".toVector.indexWhere(_ == 'c', -1))
+ assertEquals(2, "abcde".toVector.indexWhere(_ == 'c', -2))
+ }
+}