diff options
author | stepancheg <stepancheg@epfl.ch> | 2008-06-06 18:53:49 +0000 |
---|---|---|
committer | stepancheg <stepancheg@epfl.ch> | 2008-06-06 18:53:49 +0000 |
commit | c5de85e4329457b4d0b3019e0a8bb89c8132d7f1 (patch) | |
tree | 87a840e4727c5f92a28376b404b8d2dee336c133 /src/library | |
parent | 5efe5b8017ed97ca37809dba0e3b11a14a6f7dd7 (diff) | |
download | scala-c5de85e4329457b4d0b3019e0a8bb89c8132d7f1.tar.gz scala-c5de85e4329457b4d0b3019e0a8bb89c8132d7f1.tar.bz2 scala-c5de85e4329457b4d0b3019e0a8bb89c8132d7f1.zip |
makeRichChar to, until return RandomAccessSeq.P...
makeRichChar to, until return RandomAccessSeq.Projection (#970)
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/RandomAccessSeq.scala | 8 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 6 | ||||
-rw-r--r-- | src/library/scala/runtime/RichChar.scala | 24 |
3 files changed, 23 insertions, 15 deletions
diff --git a/src/library/scala/RandomAccessSeq.scala b/src/library/scala/RandomAccessSeq.scala index 437d7ae8fb..363fc72d85 100644 --- a/src/library/scala/RandomAccessSeq.scala +++ b/src/library/scala/RandomAccessSeq.scala @@ -13,6 +13,14 @@ package scala import collection.mutable.ArrayBuffer object RandomAccessSeq { + + /** The empty sequence */ + val empty : RandomAccessSeq[Nothing] = new RandomAccessSeq[Nothing] { + def length = 0 + def apply(i: Int): Nothing = throw new NoSuchElementException("empty sequence") + override def elements = Iterator.empty + } + trait Projection[+A] extends Seq.Projection[A] with RandomAccessSeq[A] { override def projection = this override def force : RandomAccessSeq[A] = toArray diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index 3b184a1ba5..763a2e8ec2 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -17,11 +17,7 @@ import collection.mutable.ArrayBuffer object Seq { /** The empty sequence */ - val empty : Seq[Nothing] = new RandomAccessSeq[Nothing] { - def length = 0 - def apply(i: Int): Nothing = throw new NoSuchElementException("empty sequence") - override def elements = Iterator.empty - } + val empty : Seq[Nothing] = RandomAccessSeq.empty /** This method is called in a pattern match { case Seq(...) => }. * diff --git a/src/library/scala/runtime/RichChar.scala b/src/library/scala/runtime/RichChar.scala index d06aa62a57..52fe17d751 100644 --- a/src/library/scala/runtime/RichChar.scala +++ b/src/library/scala/runtime/RichChar.scala @@ -52,20 +52,24 @@ final class RichChar(x: Char) extends Proxy with Ordered[Char] { def toLowerCase: Char = Character.toLowerCase(x) def toUpperCase: Char = Character.toUpperCase(x) - /** Create an Iterator[Char] over the characters from 'x' to 'y' - 1 + /** Create a <code>RandomAccessSeq.Projection[Char]</code> over the characters from 'x' to 'y' - 1 */ - def until(limit: Char): Iterator[Char] = new Iterator[Char] { - private var ch = x - def hasNext: Boolean = ch < limit - def next: Char = - if (hasNext) { val j = ch; ch = (ch + 1).toChar; j } - else throw new NoSuchElementException("next on empty iterator") - } + def until(limit: Char): RandomAccessSeq.Projection[Char] = + if (limit <= x) RandomAccessSeq.empty.projection + else + new RandomAccessSeq.Projection[Char] { + def length = limit - x + def apply(i: Int): Char = { + Predef.require(i >= 0 && i < length) + (x + i).toChar + } + override def stringPrefix = "RandomAccessSeq.Projection" + } //def until(y: Char): Iterator[Char] = to(y) - /** Create an Iterator[Char] over the characters from 'x' to 'y' + /** Create a <code>RandomAccessSeq.Projection[Char]</code> over the characters from 'x' to 'y' */ - def to(y: Char): Iterator[Char] = until((y + 1).toChar) + def to(y: Char): RandomAccessSeq.Projection[Char] = until((y + 1).toChar) } |