summaryrefslogtreecommitdiff
path: root/src/pkg/xml/xml.go
diff options
context:
space:
mode:
authorStephen Weinberg <stephen@q5comm.com>2010-01-25 18:50:51 -0800
committerStephen Weinberg <stephen@q5comm.com>2010-01-25 18:50:51 -0800
commit3ed83878d067bfb4eaa5ba5390bcde974382000f (patch)
tree8d0d9e3b2971a73057ff25f7a13b6b790352dfae /src/pkg/xml/xml.go
parent0994534adbc649cd88a7c66eefb549a8d05231aa (diff)
downloadgolang-3ed83878d067bfb4eaa5ba5390bcde974382000f.tar.gz
xml: add Escape, copied from template.HTMLEscape.
R=rsc CC=golang-dev http://codereview.appspot.com/186282 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/xml/xml.go')
-rw-r--r--src/pkg/xml/xml.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/pkg/xml/xml.go b/src/pkg/xml/xml.go
index 346b34649..33a86a255 100644
--- a/src/pkg/xml/xml.go
+++ b/src/pkg/xml/xml.go
@@ -1479,3 +1479,38 @@ var htmlAutoClose = []string{
"base",
"meta",
}
+
+var (
+ esc_quot = strings.Bytes("&#34;") // shorter than "&quot;"
+ esc_apos = strings.Bytes("&#39;") // shorter than "&apos;"
+ esc_amp = strings.Bytes("&amp;")
+ esc_lt = strings.Bytes("&lt;")
+ esc_gt = strings.Bytes("&gt;")
+)
+
+// Escape writes to w the properly escaped XML equivalent
+// of the plain text data s.
+func Escape(w io.Writer, s []byte) {
+ var esc []byte
+ last := 0
+ for i, c := range s {
+ switch c {
+ case '"':
+ esc = esc_quot
+ case '\'':
+ esc = esc_apos
+ case '&':
+ esc = esc_amp
+ case '<':
+ esc = esc_lt
+ case '>':
+ esc = esc_gt
+ default:
+ continue
+ }
+ w.Write(s[last:i])
+ w.Write(esc)
+ last = i + 1
+ }
+ w.Write(s[last:])
+}