summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2007-03-16 13:34:42 +0000
committermichelou <michelou@epfl.ch>2007-03-16 13:34:42 +0000
commit3d4d7ce3ef2ef6a42c8e4f035058b4e5e76b88b1 (patch)
treea86e3c6829ef976c9e874c6ef9762ee43e34f420
parent6d93465512f46b7e26226ef565e399adc63b993c (diff)
downloadscala-3d4d7ce3ef2ef6a42c8e4f035058b4e5e76b88b1.tar.gz
scala-3d4d7ce3ef2ef6a42c8e4f035058b4e5e76b88b1.tar.bz2
scala-3d4d7ce3ef2ef6a42c8e4f035058b4e5e76b88b1.zip
added Array.deepToString, cleaned up MatchError
-rw-r--r--src/compiler/scala/tools/nsc/symtab/Definitions.scala8
-rw-r--r--src/library/scala/Array.scala54
-rw-r--r--src/library/scala/MatchError.scala34
-rw-r--r--src/library/scala/Predef.scala12
-rw-r--r--src/library/scala/runtime/BoxedArray.scala42
-rw-r--r--test/files/run/deepToString.check49
-rw-r--r--test/files/run/deepToString.scala41
7 files changed, 183 insertions, 57 deletions
diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
index f3aedf5e9b..3f46850e30 100644
--- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala
+++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
@@ -95,9 +95,9 @@ trait Definitions requires SymbolTable {
def Predef_identity = getMember(PredefModule, nme.identity)
var ConsoleModule: Symbol = _
var MatchErrorClass: Symbol = _
- var MatchErrorModule: Symbol = _
- def MatchError_fail = getMember(MatchErrorModule, nme.fail)
- def MatchError_report = getMember(MatchErrorModule, nme.report)
+ //var MatchErrorModule: Symbol = _
+ // def MatchError_fail = getMember(MatchErrorModule, nme.fail)
+ // def MatchError_report = getMember(MatchErrorModule, nme.report)
var IndexOutOfBoundsExceptionClass: Symbol = _
//var RemoteExecutionModule: Symbol = _
// def RemoteExecution_detach = getMember(RemoteExecutionModule, "detach")
@@ -793,7 +793,7 @@ trait Definitions requires SymbolTable {
PredefModule = getModule("scala.Predef")
ConsoleModule = getModule("scala.Console")
MatchErrorClass = getClass("scala.MatchError")
- MatchErrorModule = getModule("scala.MatchError")
+ //MatchErrorModule = getModule("scala.MatchError")
IndexOutOfBoundsExceptionClass =
getClass(if (forMSIL) "System.IndexOutOfRangeException"
else "java.lang.IndexOutOfBoundsException")
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala
index 8a29abb0b5..be53af2ff0 100644
--- a/src/library/scala/Array.scala
+++ b/src/library/scala/Array.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -194,9 +194,15 @@ final class Array[A](_length: Int) extends Seq[A] {
def length: Int = throw new Error()
/** The element at given index.
- * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first
- * element of array <code>xs</code>.
- * Note the indexing syntax <code>xs(i)</code> is a shorthand for <code>xs.apply(i)</code>.
+ * <p>
+ * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first
+ * element of array <code>xs</code>.
+ * </p>
+ * <p>
+ * Note the indexing syntax <code>xs(i)</code> is a shorthand for
+ * <code>xs.apply(i)</code>.
+ * </p>
+ *
* @param i the index
* @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or
* <code>length <= i</code>
@@ -208,6 +214,7 @@ final class Array[A](_length: Int) extends Seq[A] {
* element of array <code>xs</code>.
* Note the indexing syntax <code>xs(i) = x</code> is a shorthand
* for <code>xs.update(i, x)</code>.
+ *
* @param i the index
* @param x the value to be written at index <code>i</code>
* @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or
@@ -224,10 +231,11 @@ final class Array[A](_length: Int) extends Seq[A] {
/** A sub-array of <code>len</code> elements
* starting at index <code>from</code>
+ *
* @param from The index of the first element of the slice
* @param end The index of the element following the slice
- * @throws IndexOutOfBoundsException if <code>from < 0</code>
- * or <code>length < from + len<code>
+ * @throws IndexOutOfBoundsException if <code>from &lt; 0</code>
+ * or <code>length &lt; from + len<code>
*/
override def slice(from: Int, end: Int): Array[A] = throw new Error()
@@ -275,4 +283,36 @@ final class Array[A](_length: Int) extends Seq[A] {
* where <code>a<sub>i</sub></code> are the elements of this stream.
*/
def zipWithIndex: Array[Tuple2[A,Int]] = throw new Error()
+
+ /**
+ * @return a deep string representation of this sequence.
+ */
+ def deepToString(): String = throw new Error()
+
+ /** Returns a string representation of this array object. The resulting string
+ * begins with the string <code>start</code> and is finished by the string
+ * <code>end</code>. Inside, the string representations of elements (w.r.t.
+ * the method <code>deepToString()</code>) are separated by the string
+ * <code>sep</code>.
+ * <p/>
+ * Ex: <br/>
+ * <code>Array(Array(1, 2), Array(3)).deepMkString("[", "; ", "]") = "[[1; 2]; 3]"</code>
+ *
+ * @param start starting string.
+ * @param sep separator string.
+ * @param end ending string.
+ * @return a string representation of this array object.
+ */
+ def deepMkString(start: String, sep: String, end: String): String =
+ throw new Error()
+
+ /** Returns a string representation of this array object. The string
+ * representations of elements (w.r.t. the method <code>deepToString()</code>)
+ * are separated by the string <code>sep</code>.
+ *
+ * @param sep separator string.
+ * @return a string representation of this array object.
+ */
+ def deepMkString(sep: String): String = throw new Error()
+
}
diff --git a/src/library/scala/MatchError.scala b/src/library/scala/MatchError.scala
index a51504a73c..8c0fd2279e 100644
--- a/src/library/scala/MatchError.scala
+++ b/src/library/scala/MatchError.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -21,35 +21,7 @@ import Predef._
* @author Martin Odersky
* @version 1.1, 05/03/2004
*/
-object MatchError {
-
- def string(obj: Any) =
- if (null != obj) obj.toString() else "null"
-
-/* //bq: pattern matcher doesn't use these methods anymore
- // todo: change pattern matcher so that dummy type parameter T can be removed.
- def fail[T](source: String, line: Int): Nothing =
- throw new MatchError(source, line)
-
- def report(source: String, line: Int, obj: Any) =
- try {
- throw new MatchError(source, line, string(obj))
- } catch {
- case e: MatchError => throw e
- case e: Throwable => throw new MatchError(source, line)
- }
- */
-}
-
final class MatchError(msg: String) extends RuntimeException(msg) {
-/* //bq: pattern matcher doesn't use these constructors anymore
- def this(source: String, line: Int) =
- this(" in '" + source + "' at line " + line)
-
- def this(source: String, line: Int, obj: String) =
- this("for object " + obj + " in '" + source + "' at line " + line)
-*/
-
def this(obj: Any) =
- this(MatchError.string(obj))
+ this(if (null != obj) obj.toString() else "null")
}
diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala
index b4724266cd..8b58cf6d7d 100644
--- a/src/library/scala/Predef.scala
+++ b/src/library/scala/Predef.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -79,22 +79,22 @@ object Predef {
throw new Throwable()
}
- def assert(assertion: Boolean): Unit = {
+ def assert(assertion: Boolean) {
if (!assertion)
throw new java.lang.AssertionError("assertion failed")
}
- def assert(assertion: Boolean, message: Any): Unit = {
+ def assert(assertion: Boolean, message: Any) {
if (!assertion)
throw new java.lang.AssertionError("assertion failed: " + message)
}
- def assume(assumption: Boolean): Unit = {
+ def assume(assumption: Boolean) {
if (!assumption)
throw new Error("assumption failed")
}
- def assume(assumption: Boolean, message: Any): Unit = {
+ def assume(assumption: Boolean, message: Any) {
if (!assumption)
throw new Error("assumption failed: " + message)
}
diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala
index dbe70968e1..3c04532c65 100644
--- a/src/library/scala/runtime/BoxedArray.scala
+++ b/src/library/scala/runtime/BoxedArray.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -12,8 +12,7 @@
package scala.runtime
-import Predef.Class
-import Predef.Error
+import Predef.{Class, Error}
import collection.mutable.ArrayBuffer
/**
@@ -38,7 +37,7 @@ abstract class BoxedArray extends Seq[Any] {
def unbox(elemClass: Class): AnyRef
- override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length
+ override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length
def elements = new Iterator[Any] {
var index = 0
@@ -78,7 +77,7 @@ abstract class BoxedArray extends Seq[Any] {
var i = 0
while (i < len) {
result(i) = f(apply(i))
- i = i + 1
+ i += 1
}
result
}
@@ -89,7 +88,7 @@ abstract class BoxedArray extends Seq[Any] {
var i = 0
while (i < len) {
buf ++= f(apply(i))
- i = i + 1
+ i += 1
}
buf.toArray
}
@@ -102,7 +101,7 @@ abstract class BoxedArray extends Seq[Any] {
var i = 0
while (i < len) {
result(i) = new Tuple2(this(i), that(i))
- i = i + 1
+ i += 1
}
result
}
@@ -113,10 +112,35 @@ abstract class BoxedArray extends Seq[Any] {
var i = 0
while (i < len) {
result(i) = new Tuple2(this(i), i)
- i = i + 1
+ i += 1
}
result
}
+ final def deepToString() = deepMkString(stringPrefix + "(", ",", ")")
+
+ final def deepMkString(start: String, sep: String, end: String): String = {
+ val buf = new StringBuilder()
+ deepAddString(buf, start, sep, end).toString
+ }
+
+ final def deepMkString(sep: String): String = this.deepMkString("", sep, "")
+
+ private def deepAddString(buf: StringBuilder, start: String, sep: String, end: String): StringBuilder = {
+ def _deepToString(x: Any) = x match {
+ case a: AnyRef if ScalaRunTime.isArray(a) =>
+ ScalaRunTime.boxArray(a).deepMkString(start, sep, end)
+ case _ =>
+ x.toString
+ }
+ buf.append(start)
+ val elems = elements
+ if (elems.hasNext) buf.append(_deepToString(elems.next))
+ while (elems.hasNext) {
+ buf.append(sep); buf.append(_deepToString(elems.next))
+ }
+ buf.append(end)
+ }
+
override final def stringPrefix: String = "Array"
}
diff --git a/test/files/run/deepToString.check b/test/files/run/deepToString.check
new file mode 100644
index 0000000000..fcc0e04575
--- /dev/null
+++ b/test/files/run/deepToString.check
@@ -0,0 +1,49 @@
+Array(true,false)
+Array(true,false)
+[true;false]
+true;false
+
+Array([Z@0000000,[Z@0000000)
+Array(Array(true,false),Array(true,false))
+[[true;false];[true;false]]
+true;false;true;false
+
+Array([[Z@0000000,[[Z@0000000)
+Array(Array(Array(true,false),Array(true,false)),Array(Array(true,false),Array(true,false)))
+[[[true;false];[true;false]];[[true;false];[true;false]]]
+true;false;true;false;true;false;true;false
+
+Array(1.0,0.0)
+Array(1.0,0.0)
+[1.0;0.0]
+1.0;0.0
+
+Array([D@0000000,[D@0000000)
+Array(Array(1.0,0.0),Array(1.0,0.0))
+[[1.0;0.0];[1.0;0.0]]
+1.0;0.0;1.0;0.0
+
+Array([[D@0000000,[[D@0000000)
+Array(Array(Array(1.0,0.0),Array(1.0,0.0)),Array(Array(1.0,0.0),Array(1.0,0.0)))
+[[[1.0;0.0];[1.0;0.0]];[[1.0;0.0];[1.0;0.0]]]
+1.0;0.0;1.0;0.0;1.0;0.0;1.0;0.0
+
+Array(a,b)
+Array(a,b)
+[a;b]
+a;b
+
+Array([Ljava.lang.String;@0000000,[Ljava.lang.String;@0000000)
+Array(Array(a,b),Array(a,b))
+[[a;b];[a;b]]
+a;b;a;b
+
+Array([[Ljava.lang.String;@0000000,[[Ljava.lang.String;@0000000)
+Array(Array(Array(a,b),Array(a,b)),Array(Array(a,b),Array(a,b)))
+[[[a;b];[a;b]];[[a;b];[a;b]]]
+a;b;a;b;a;b;a;b
+
+[[true; false]; [false]]
+[[1; 2]; [3]]
+[[1; 2]; [3]]
+
diff --git a/test/files/run/deepToString.scala b/test/files/run/deepToString.scala
new file mode 100644
index 0000000000..1fb73fe5a5
--- /dev/null
+++ b/test/files/run/deepToString.scala
@@ -0,0 +1,41 @@
+object Test extends Application {
+
+ def sweep(s: String) =
+ s.replaceAll("D@[0-9a-fA-F]+", "D@0000000")
+ .replaceAll("Z@[0-9a-fA-F]+", "Z@0000000")
+ .replaceAll(";@[0-9a-fA-F]+", ";@0000000")
+
+ def test[A](a: Array[A]) {
+ Console.println(sweep(a.toString))
+ Console.println(a.deepToString)
+ Console.println(a.deepMkString("[", ";", "]"))
+ Console.println(a.deepMkString(";"))
+ Console.println
+ }
+
+ val ba1 = Array(true, false)
+ val ba2 = Array(ba1, ba1)
+ val ba3 = Array(ba2, ba2)
+ test(ba1)
+ test(ba2)
+ test(ba3)
+
+ val da1 = Array(1.0d, 0.0d)
+ val da2 = Array(da1, da1)
+ val da3 = Array(da2, da2)
+ test(da1)
+ test(da2)
+ test(da3)
+
+ val sa1 = Array("a", "b")
+ val sa2 = Array(sa1, sa1)
+ val sa3 = Array(sa2, sa2)
+ test(sa1)
+ test(sa2)
+ test(sa3)
+
+ Console.println(Array(Array(true, false), Array(false)).deepMkString("[", "; ", "]"))
+ Console.println(Array(Array('1', '2'), Array('3')).deepMkString("[", "; ", "]"))
+ Console.println(Array(Array(1, 2), Array(3)).deepMkString("[", "; ", "]"))
+ Console.println
+}