diff options
author | Paul Phillips <paulp@improving.org> | 2009-09-24 23:06:30 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-09-24 23:06:30 +0000 |
commit | d17b40768ccb498e36464dd800ed5b602a7de372 (patch) | |
tree | ab923a2709e91831cae4f41012c752bd6597d0d4 /src/library | |
parent | 67a8cdb404b54439abdce81790fdff147e073103 (diff) | |
download | scala-d17b40768ccb498e36464dd800ed5b602a7de372.tar.gz scala-d17b40768ccb498e36464dd800ed5b602a7de372.tar.bz2 scala-d17b40768ccb498e36464dd800ed5b602a7de372.zip |
This patch represents breaking out bits of code...
This patch represents breaking out bits of code generation related to
equality so that I can log what they're doing and easily alter them; to
that end there is some code generation refactoring and a couple minor
XML issues that came up.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/runtime/BoxesRunTime.java | 45 | ||||
-rw-r--r-- | src/library/scala/runtime/Equality.java | 51 | ||||
-rw-r--r-- | src/library/scala/xml/Utility.scala | 8 | ||||
-rw-r--r-- | src/library/scala/xml/parsing/MarkupParser.scala | 36 |
4 files changed, 72 insertions, 68 deletions
diff --git a/src/library/scala/runtime/BoxesRunTime.java b/src/library/scala/runtime/BoxesRunTime.java index 17a1729467..2e4af2d13a 100644 --- a/src/library/scala/runtime/BoxesRunTime.java +++ b/src/library/scala/runtime/BoxesRunTime.java @@ -29,43 +29,6 @@ import java.io.*; * @version 2.0 */ public class BoxesRunTime { - /**** Temporary code to support logging all equality comparisons. ****/ - private static boolean eqeqLogging = false; - private static String eqeqLogName = "/tmp/trunk-eqeq.log"; - private static FileWriter eqeqLog; - public static void setEqEqLogging(boolean state) { - eqeqLogging = state; - if (state) { - try { eqeqLog = new FileWriter(eqeqLogName, true); } - catch (IOException e) { eqeqLog = null; } - - log("Started eqeq log at " + (new java.util.Date())); - } - } - private static String obToString(Object o) { - String s = o.toString() + " (" + o.getClass().getSimpleName() + ")"; - return s.replaceAll("\\n", " "); - } - private static void logInternal(String msg, Object a, Object b, String where) { - log(msg + obToString(a) + " == " + obToString(b) + " " + where); - } - - public static String whereAreWe() { - StackTraceElement e = Thread.currentThread().getStackTrace()[3]; - return"(" + e.getClassName() + "." + e.getMethodName() + e.getFileName() + ":" + e.getLineNumber() + ")"; - } - public static void log(String msg) { - if (eqeqLogging && eqeqLog != null) { - try { - eqeqLog.write(msg + "\n"); - eqeqLog.flush(); - } - catch (IOException e) { } - } - } - - /**** End temporary logging section. ****/ - private static final int CHAR = 0, BYTE = 1, SHORT = 2, INT = 3, LONG = 4, FLOAT = 5, DOUBLE = 6, OTHER = 7; private static int typeCode(Object a) { @@ -92,8 +55,8 @@ public class BoxesRunTime // foo(-100) // and the -100 will get to Character, which will duly crash. // The bug was masked before because the Characters were created - // with "new Character(c)" and the constructor avenue must have - // some check against negative values, whereas the static method doesn't. + // with "new Character(c)", but now the static method uses the argument + // as an index into a cache array, which can't be negative. // // It appears to be Short-specific; I can't get anything similar // out of Byte or Int. @@ -369,7 +332,7 @@ public class BoxesRunTime public static boolean equals(Object a, Object b) { if ((a instanceof Number || a instanceof Character) && (b instanceof Number || b instanceof Character)) { if (a.getClass() != b.getClass()) { - logInternal("[ BOXED ] Comparing: ", a, b, whereAreWe()); + Equality.logInternal("[ BOXED ] Comparing: ", a, b, Equality.whereAreWe()); } } @@ -407,7 +370,7 @@ public class BoxesRunTime String msg; if (res) msg = "[ BOXED ] Overriding equals between different types: "; else msg = "[ BOXED ] Overriding equals because b.equals(a): "; - logInternal(msg, a, b, whereAreWe()); + Equality.logInternal(msg, a, b, Equality.whereAreWe()); return true; } return false; diff --git a/src/library/scala/runtime/Equality.java b/src/library/scala/runtime/Equality.java new file mode 100644 index 0000000000..3ec89360b8 --- /dev/null +++ b/src/library/scala/runtime/Equality.java @@ -0,0 +1,51 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + +import java.io.*; + +/** An object (static class) encapsulating the variations on equality + * presently under consideration. It's written in java so it can easily + * be made available to BoxesRunTime as well as scala code. + */ +public class Equality +{ + public static boolean disallowNullEquals = true; + public static boolean disallowPrimitiveEquals = true; + public static boolean warnOnEqualsAny = true; + public static boolean callCanEqualIfAvailable = true; + public static boolean logEverything = false; + + public static String obToString(Object o) { + String s = o.toString() + " (" + o.getClass().getSimpleName() + ")"; + return s.replaceAll("\\n", " "); + } + + public static void logInternal(String msg, Object a, Object b, String where) { + log(String.format("%s %s == %s at %s", msg, obToString(a), obToString(b), where)); + } + + public static String whereAreWe() { + StackTraceElement[] es = Thread.currentThread().getStackTrace(); + if (es.length < 4) + return "<unknown>"; + + StackTraceElement e = es[3]; + String clazz = e.getClassName().replaceAll("\\$.*$", "\\$..."); + return String.format("%s.%s(%s.%d)", clazz, e.getMethodName(), e.getFileName(), e.getLineNumber()); + } + + public static void log(String msg) { + if (logEverything) + System.out.println(msg); + } +} diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala index 302f77d07c..f4452fd064 100644 --- a/src/library/scala/xml/Utility.scala +++ b/src/library/scala/xml/Utility.scala @@ -87,7 +87,7 @@ object Utility extends AnyRef with parsing.TokenTests object Escapes { /** For reasons unclear escape and unescape are a long ways from being logical inverses. */ - private val pairs = List( + val pairs = Map( "lt" -> '<', "gt" -> '>', "amp" -> '&', @@ -96,10 +96,10 @@ object Utility extends AnyRef with parsing.TokenTests // is valid xhtml but not html, and IE doesn't know it, says jweb // "apos" -> '\'' ) - val escMap = Map((for ((s, c) <- pairs) yield (c, "&%s;" format s)) : _*) - val unescMap = Map(("apos" -> '\'') :: pairs : _*) + val escMap = pairs map { case (s, c) => c-> ("&%s;" format s) } + val unescMap = pairs ++ Map("apos" -> '\'') } - import Escapes._ + import Escapes.{ escMap, unescMap } /** * Appends escaped string to <code>s</code>. diff --git a/src/library/scala/xml/parsing/MarkupParser.scala b/src/library/scala/xml/parsing/MarkupParser.scala index fa60329456..a4af954e5a 100644 --- a/src/library/scala/xml/parsing/MarkupParser.scala +++ b/src/library/scala/xml/parsing/MarkupParser.scala @@ -14,6 +14,7 @@ package parsing import scala.io.Source import scala.xml.dtd._ +import Utility.Escapes.{ pairs => unescape } /** * An XML parser. @@ -515,32 +516,21 @@ trait MarkupParser extends AnyRef with TokenTests { self: MarkupParser with Mar // postcond: xEmbeddedBlock == false! case '&' => // EntityRef or CharRef nextch; - ch match { - case '#' => // CharacterRef - nextch; - val theChar = handle.text( tmppos, - xCharRef ({ ()=> ch },{ () => nextch }) ); - xToken(';'); - ts &+ theChar ; - case _ => // EntityRef - val n = xName - xToken(';') - n match { - case "lt" => ts &+ '<' - case "gt" => ts &+ '>' - case "amp" => ts &+ '&' - case "quot" => ts &+ '"' - case _ => - /* - ts + handle.entityRef( tmppos, n ) ; - */ - push(n) - } + if (ch == '#') { // CharacterRef + nextch + val theChar = handle.text(tmppos, xCharRef(() => ch, () => nextch)) + xToken(';'); + ts &+ theChar + } + else { // EntityRef + val n = xName + xToken(';') + + if (unescape contains n) ts &+ unescape(n) + else push(n) } case _ => // text content - //Console.println("text content?? pos = "+pos); appendText(tmppos, ts, xText); - // here xEmbeddedBlock might be true } /*}*/ } |