Skip to content

Revert imperative Utility.escape from 81d7e2a #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions shared/src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,21 @@ object Utility extends AnyRef with parsing.TokenTests {
val escMap = (pairs - "apos") map { case (s, c) => c -> ("&%s;" format s) }
val unescMap = pairs
}
import Escapes.unescMap
import Escapes.{ escMap, unescMap }

/**
* Appends escaped string to `s`.
*/
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)
text.iterator.foldLeft(s) { (s, c) =>
escMap.get(c) match {
case Some(str) => s ++= str
case _ if c >= ' ' || "\n\r\t".contains(c) => s += c
case _ => s // noop
}

pos += 1
}
s
}

/**
Expand Down