Skip to content

Commit cce8670

Browse files
committed
IterableOnce#copyToArray uses helper for count
1 parent 0fa1f2e commit cce8670

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/src/scala/collection/IterableOnce.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ object IterableOnce {
273273
* @return the number of elements that will be copied to the destination array
274274
*/
275275
@inline private[collection] def elemsToCopyToArray(srcLen: Int, destLen: Int, start: Int, len: Int): Int =
276-
math.max(math.min(math.min(len, srcLen), destLen - start), 0)
276+
math.max(0,
277+
math.min(if (start < 0) destLen else destLen - start,
278+
math.min(len, srcLen)))
277279

278280
/** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
279281
@inline private[collection] def copyElemsToArray[A, B >: A](elems: IterableOnce[A],
@@ -1035,7 +1037,11 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A] =>
10351037
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int = {
10361038
val it = iterator
10371039
var i = start
1038-
val end = start + math.min(len, xs.length - start)
1040+
val srclen = knownSize match {
1041+
case -1 => xs.length
1042+
case k => k
1043+
}
1044+
val end = start + IterableOnce.elemsToCopyToArray(srclen, xs.length, start, len)
10391045
while (i < end && it.hasNext) {
10401046
xs(i) = it.next()
10411047
i += 1

0 commit comments

Comments
 (0)