-
Notifications
You must be signed in to change notification settings - Fork 3
/
xml.go
65 lines (58 loc) · 1.18 KB
/
xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ucenter
import (
"bytes"
"io"
"strings"
)
type CharsetISO88591er struct {
r io.ByteReader
buf *bytes.Buffer
}
func newCharsetISO88591(r io.Reader) *CharsetISO88591er {
buf := bytes.Buffer{}
return &CharsetISO88591er{r.(io.ByteReader), &buf}
}
func (cs *CharsetISO88591er) Read(p []byte) (n int, err error) {
for _ = range p {
if r, err := cs.r.ReadByte(); err != nil {
break
} else {
cs.buf.WriteRune(rune(r))
}
}
return cs.buf.Read(p)
}
func isCharset(charset string, names []string) bool {
charset = strings.ToLower(charset)
for _, n := range names {
if charset == strings.ToLower(n) {
return true
}
}
return false
}
func isCharsetISO88591(charset string) bool {
// http://www.iana.org/assignments/character-sets
// (last updated 2010-11-04)
names := []string{
// Name
"ISO_8859-1:1987",
// Alias (preferred MIME name)
"ISO-8859-1",
// Aliases
"iso-ir-100",
"ISO_8859-1",
"latin1",
"l1",
"IBM819",
"CP819",
"csISOLatin1",
}
return isCharset(charset, names)
}
func CharsetReader(charset string, input io.Reader) (io.Reader, error) {
if isCharsetISO88591(charset) {
return newCharsetISO88591(input), nil
}
return input, nil
}