From a75e4a7fafef9ce619a8d0f0622333d20502e7c8 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 30 Dec 2016 11:33:48 -0800 Subject: SI-9936 LinearSeqOptimized.indexWhere Also suffered from the negative `from` bug. Prefer `math.max` to avoid `RichInt`. --- .../scala/collection/IndexedSeqOptimized.scala | 2 +- src/library/scala/collection/LinearSeqOptimized.scala | 2 +- src/library/scala/collection/SeqLike.scala | 3 +-- .../scala/collection/LinearSeqOptimizedTest.scala | 19 +++++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/junit/scala/collection/LinearSeqOptimizedTest.scala diff --git a/src/library/scala/collection/IndexedSeqOptimized.scala b/src/library/scala/collection/IndexedSeqOptimized.scala index 3765b2fff0..320725c30e 100644 --- a/src/library/scala/collection/IndexedSeqOptimized.scala +++ b/src/library/scala/collection/IndexedSeqOptimized.scala @@ -199,7 +199,7 @@ trait IndexedSeqOptimized[+A, +Repr] extends Any with IndexedSeqLike[A, Repr] { override /*SeqLike*/ def indexWhere(p: A => Boolean, from: Int): Int = { - val start = from max 0 + val start = math.max(from, 0) negLength(start + segmentLength(!p(_), start)) } diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index a3860f10a4..68b85dcfe5 100644 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -291,7 +291,7 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea override /*SeqLike*/ def indexWhere(p: A => Boolean, from: Int): Int = { - var i = from + var i = math.max(from, 0) var these = this drop from while (these.nonEmpty) { if (p(these.head)) diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index 2d662257e5..3e025bc43f 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -113,13 +113,12 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[ } def indexWhere(p: A => Boolean, from: Int): Int = { - var i = from max 0 + var i = math.max(from, 0) val it = iterator.drop(from) while (it.hasNext) { if (p(it.next())) return i else i += 1 } - -1 } diff --git a/test/junit/scala/collection/LinearSeqOptimizedTest.scala b/test/junit/scala/collection/LinearSeqOptimizedTest.scala new file mode 100644 index 0000000000..b9c34ed17c --- /dev/null +++ b/test/junit/scala/collection/LinearSeqOptimizedTest.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 LinearSeqOptimizedTest { + + @Test def `SI-9936 indexWhere`(): Unit = { + assertEquals(2, "abcde".indexOf('c', -1)) + assertEquals(2, "abcde".indexOf('c', -2)) + assertEquals(2, "abcde".toList.indexOf('c', -1)) + assertEquals(2, "abcde".toList.indexOf('c', -2)) + assertEquals(2, "abcde".toList.indexWhere(_ == 'c', -1)) + assertEquals(2, "abcde".toList.indexWhere(_ == 'c', -2)) + } +} -- cgit v1.2.3