mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
xml: add Escape, copied from template.HTMLEscape.
R=rsc CC=golang-dev https://golang.org/cl/186282
This commit is contained in:
parent
9f3738a4eb
commit
c90b05bf7d
1 changed files with 35 additions and 0 deletions
|
|
@ -1479,3 +1479,38 @@ var htmlAutoClose = []string{
|
||||||
"base",
|
"base",
|
||||||
"meta",
|
"meta",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
esc_quot = strings.Bytes(""") // shorter than """
|
||||||
|
esc_apos = strings.Bytes("'") // shorter than "'"
|
||||||
|
esc_amp = strings.Bytes("&")
|
||||||
|
esc_lt = strings.Bytes("<")
|
||||||
|
esc_gt = strings.Bytes(">")
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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:])
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue