diff options
author | Rex Kerr <ichoran@gmail.com> | 2014-11-24 16:43:21 -0800 |
---|---|---|
committer | Rex Kerr <ichoran@gmail.com> | 2014-11-25 11:49:26 -0800 |
commit | 1620b8cf772f0c4371220e05c54a3e3c5ccab349 (patch) | |
tree | adc0c4da956e8ffa3da1d1d761d7749d4aab2f7b | |
parent | 6f5b853dda0b00c65776b05abde23ceb03d9621d (diff) | |
download | scala-1620b8cf772f0c4371220e05c54a3e3c5ccab349.tar.gz scala-1620b8cf772f0c4371220e05c54a3e3c5ccab349.tar.bz2 scala-1620b8cf772f0c4371220e05c54a3e3c5ccab349.zip |
SI-8819 Range slice does not return a Range
Added an override to the `slice` method that creates a `Range`. No tests (except verifying by hand that it solves the bug); scala-collections-laws found and will test this.
-rw-r--r-- | src/library/scala/collection/immutable/Range.scala | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 720dfeed59..9fa877055f 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -196,7 +196,24 @@ extends scala.collection.AbstractSeq[Int] copy(locationAfterN(n), end, step) } ) - + + /** Creates a new range containing the elements starting at `from` up to but not including `until`. + * + * $doesNotUseBuilders + * + * @param from the element at which to start + * @param until the element at which to end (not included in the range) + * @return a new range consisting of a contiguous interval of values in the old range + */ + override def slice(from: Int, until: Int): Range = + if (from <= 0) take(until) + else if (until >= numRangeElements && numRangeElements >= 0) drop(from) + else { + val fromValue = locationAfterN(from) + if (from >= until) newEmptyRange(fromValue) + else new Range.Inclusive(fromValue, locationAfterN(until-1), step) + } + /** Creates a new range containing all the elements of this range except the last one. * * $doesNotUseBuilders |