From c5de85e4329457b4d0b3019e0a8bb89c8132d7f1 Mon Sep 17 00:00:00 2001 From: stepancheg Date: Fri, 6 Jun 2008 18:53:49 +0000 Subject: makeRichChar to, until return RandomAccessSeq.P... makeRichChar to, until return RandomAccessSeq.Projection (#970) --- src/library/scala/RandomAccessSeq.scala | 8 ++++++++ src/library/scala/Seq.scala | 6 +----- src/library/scala/runtime/RichChar.scala | 24 ++++++++++++++---------- test/files/run/runtime-richChar.check | 10 ++++++++++ test/files/run/runtime-richChar.scala | 25 +++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 test/files/run/runtime-richChar.check create mode 100644 test/files/run/runtime-richChar.scala 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 RandomAccessSeq.Projection[Char] 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 RandomAccessSeq.Projection[Char] 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) } diff --git a/test/files/run/runtime-richChar.check b/test/files/run/runtime-richChar.check new file mode 100644 index 0000000000..8d7a008585 --- /dev/null +++ b/test/files/run/runtime-richChar.check @@ -0,0 +1,10 @@ +'a' to 'c' ok +'a' until 'c' ok +'a' to 'b' ok +'a' until 'b' ok +'a' to 'a' ok +'a' until 'a' ok +'b' to 'a' ok +'b' until 'a' ok +'c' to 'a' ok +'c' until 'a' ok diff --git a/test/files/run/runtime-richChar.scala b/test/files/run/runtime-richChar.scala new file mode 100644 index 0000000000..b2c488983d --- /dev/null +++ b/test/files/run/runtime-richChar.scala @@ -0,0 +1,25 @@ +object Test extends Application { + def testSeq(name: String, expected: Seq[Char], got: Seq[Char]) { + if (expected.toList == got.toList) + println(name + " ok") + else + println(name + " failed: " + expected + " differs from " + got) + } + + testSeq("'a' to 'c'", List('a', 'b', 'c'), 'a' to 'c') + testSeq("'a' until 'c'", List('a', 'b'), 'a' until 'c') + + testSeq("'a' to 'b'", List('a', 'b'), 'a' to 'b') + testSeq("'a' until 'b'", List('a'), 'a' until 'b') + + testSeq("'a' to 'a'", List('a'), 'a' to 'a') + testSeq("'a' until 'a'", List(), 'a' until 'a') + + testSeq("'b' to 'a'", List(), 'b' to 'a') + testSeq("'b' until 'a'", List(), 'b' until 'a') + + testSeq("'c' to 'a'", List(), 'c' to 'a') + testSeq("'c' until 'a'", List(), 'c' until 'a') +} + +// vim: set ts=2 sw=2 et: -- cgit v1.2.3