|
@@ -10,6 +10,7 @@ package charset // import "golang.org/x/net/html/charset"
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
|
|
+ "fmt"
|
|
|
"io"
|
|
"io"
|
|
|
"mime"
|
|
"mime"
|
|
|
"strings"
|
|
"strings"
|
|
@@ -110,6 +111,18 @@ func NewReader(r io.Reader, contentType string) (io.Reader, error) {
|
|
|
return r, nil
|
|
return r, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// NewReaderByName returns a reader that converts from the specified charset to
|
|
|
|
|
+// UTF-8. It returns an error if the charset is not one of the standard
|
|
|
|
|
+// encodings for HTML. It is suitable for use as encoding/xml.Decoder's
|
|
|
|
|
+// CharsetReader function.
|
|
|
|
|
+func NewReaderByName(charset string, input io.Reader) (io.Reader, error) {
|
|
|
|
|
+ e, _ := Lookup(charset)
|
|
|
|
|
+ if e == nil {
|
|
|
|
|
+ return nil, fmt.Errorf("unsupported charset: %q", charset)
|
|
|
|
|
+ }
|
|
|
|
|
+ return transform.NewReader(input, e.NewDecoder()), nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func prescan(content []byte) (e encoding.Encoding, name string) {
|
|
func prescan(content []byte) (e encoding.Encoding, name string) {
|
|
|
z := html.NewTokenizer(bytes.NewReader(content))
|
|
z := html.NewTokenizer(bytes.NewReader(content))
|
|
|
for {
|
|
for {
|