]> Cypherpunks repositories - gostls13.git/commitdiff
xml: add Escape, copied from template.HTMLEscape.
authorStephen Weinberg <stephen@q5comm.com>
Tue, 26 Jan 2010 02:50:51 +0000 (18:50 -0800)
committerRuss Cox <rsc@golang.org>
Tue, 26 Jan 2010 02:50:51 +0000 (18:50 -0800)
R=rsc
CC=golang-dev
https://golang.org/cl/186282

src/pkg/xml/xml.go

index 346b346492a7a0dcbea7b8030a37970d61947d92..33a86a2557c4a0b01fcd539a8cd7b6a96b5a8e97 100644 (file)
@@ -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:])
+}