diff options
author | David Pollak <feeder.of.the.bears@gmail.com> | 2010-02-03 18:04:50 +0000 |
---|---|---|
committer | David Pollak <feeder.of.the.bears@gmail.com> | 2010-02-03 18:04:50 +0000 |
commit | 6cb01719eb7e406b7238fd742c358872c0666ff3 (patch) | |
tree | 6722a424925762c62202dfcf5cebd84ce74c6568 /src/library | |
parent | 96a42a2eda85489bb7dba25d9e8597bd35d7bcbd (diff) | |
download | scala-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
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/xml/Utility.scala | 28 |
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("<") + case '>' => s.append(">") + case '&' => s.append("&") + case '"' => s.append(""") + 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 & |