encoding.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Package charset provides common text encodings for HTML documents.
  2. //
  3. // The mapping from encoding labels to encodings is defined at
  4. // http://encoding.spec.whatwg.org.
  5. package charset
  6. import (
  7. "errors"
  8. "strings"
  9. "code.google.com/p/go.text/encoding"
  10. "code.google.com/p/go.text/transform"
  11. )
  12. // Encoding returns the encoding with the specified label, or nil if label is
  13. // not one of the standard encodings for HTML. Matching is case-insensitive
  14. // and ignores leading and trailing whitespace.
  15. func Encoding(label string) encoding.Encoding {
  16. label = strings.ToLower(strings.Trim(label, "\t\n\r\f "))
  17. return encodings[label]
  18. }
  19. type utf8Encoding struct{}
  20. func (utf8Encoding) NewDecoder() transform.Transformer {
  21. return transform.Nop
  22. }
  23. func (utf8Encoding) NewEncoder() transform.Transformer {
  24. return transform.Nop
  25. }
  26. var ErrReplacementEncoding = errors.New("charset: ISO-2022-CN and ISO-2022-KR are obsolete encodings for HTML")
  27. type errorTransformer struct {
  28. err error
  29. }
  30. func (t errorTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  31. return 0, 0, t.err
  32. }
  33. type replacementEncoding struct{}
  34. func (replacementEncoding) NewDecoder() transform.Transformer {
  35. return errorTransformer{ErrReplacementEncoding}
  36. }
  37. func (replacementEncoding) NewEncoder() transform.Transformer {
  38. return transform.Nop
  39. }