1package message
2
3import (
4 "io"
5 "strings"
6
7 "golang.org/x/text/encoding/ianaindex"
8)
9
10// DecodeReader returns a reader that reads from r, decoding as charset. If
11// charset is empty, us-ascii, utf-8 or unknown, the original reader is
12// returned and no decoding takes place.
13func DecodeReader(charset string, r io.Reader) io.Reader {
14 switch strings.ToLower(charset) {
15 case "", "us-ascii", "utf-8":
16 return r
17 }
18 enc, _ := ianaindex.MIME.Encoding(charset)
19 if enc == nil {
20 enc, _ = ianaindex.IANA.Encoding(charset)
21 }
22 // todo: ianaindex doesn't know all encodings, e.g. gb2312. should we transform them, with which code?
23 if enc == nil {
24 return r
25 }
26 return enc.NewDecoder().Reader(r)
27}
28