summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pollak <feeder.of.the.bears@gmail.com>2010-02-03 18:04:50 +0000
committerDavid Pollak <feeder.of.the.bears@gmail.com>2010-02-03 18:04:50 +0000
commit6cb01719eb7e406b7238fd742c358872c0666ff3 (patch)
tree6722a424925762c62202dfcf5cebd84ce74c6568
parent96a42a2eda85489bb7dba25d9e8597bd35d7bcbd (diff)
downloadscala-6cb01719eb7e406b7238fd742c358872c0666ff3.tar.gz
scala-6cb01719eb7e406b7238fd742c358872c0666ff3.tar.bz2
scala-6cb01719eb7e406b7238fd742c358872c0666ff3.zip
Fixed XML Utility.escape method to conform to X...
Fixed XML Utility.escape method to conform to XML spec. Closes #3014
-rw-r--r--src/library/scala/xml/Utility.scala28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala
index c39c787bfe..138a704957 100644
--- a/src/library/scala/xml/Utility.scala
+++ b/src/library/scala/xml/Utility.scala
@@ -106,11 +106,29 @@ object Utility extends AnyRef with parsing.TokenTests
* @param s ...
* @return ...
*/
- final def escape(text: String, s: StringBuilder): StringBuilder =
- text.foldLeft(s)((s, c) => escMap.get(c) match {
- case Some(str) => s append str
- case None => s append c
- })
+ final def escape(text: String, s: StringBuilder): StringBuilder = {
+ // Implemented per XML spec:
+ // http://www.w3.org/International/questions/qa-controls
+ // imperative code 3x-4x faster than current implementation
+ // dpp (David Pollak) 2010/02/03
+ val len = text.length
+ var pos = 0
+ while (pos < len) {
+ text.charAt(pos) match {
+ case '<' => s.append("&lt;")
+ case '>' => s.append("&gt;")
+ case '&' => s.append("&amp;")
+ case '"' => s.append("&quot;")
+ case '\n' => s.append('\n')
+ case '\r' => s.append('\r')
+ case '\t' => s.append('\t')
+ case c => if (c >= ' ') s.append(c)
+ }
+
+ pos += 1
+ }
+ s
+ }
/**
* Appends unescaped string to <code>s</code>, amp becomes &amp;