Skip to content

Commit

Permalink
Fixed XML Utility.escape method to conform to X...
Browse files Browse the repository at this point in the history
Fixed XML Utility.escape method to conform to XML spec. Closes #3014
  • Loading branch information
dpp authored and adriaanm committed Jul 17, 2013
1 parent 3adacdf commit 81d7e2a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/library/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 81d7e2a

Please sign in to comment.