From ae0269200c6e5af8120587e2317f595e746c6114 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 26 Sep 2016 14:29:12 -0700 Subject: SI-9936 SeqLike.indexWhere starts at zero This follows the Scaladoc, and makes ``` "abcdef".indexOf('c', -1) ``` work like ``` "abcdef".toVector.indexOf('c', -1) ``` --- src/library/scala/collection/SeqLike.scala | 2 +- test/junit/scala/collection/SeqLikeTest.scala | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/junit/scala/collection/SeqLikeTest.scala 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)) + } +} -- cgit v1.2.3