internal.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package internal contains code that is shared among encoding implementations.
  5. package internal
  6. import (
  7. "golang.org/x/text/encoding"
  8. "golang.org/x/text/encoding/internal/identifier"
  9. "golang.org/x/text/transform"
  10. )
  11. // Encoding is an implementation of the Encoding interface that adds the String
  12. // and ID methods to an existing encoding.
  13. type Encoding struct {
  14. encoding.Encoding
  15. Name string
  16. MIB identifier.MIB
  17. }
  18. // _ verifies that Encoding implements identifier.Interface.
  19. var _ identifier.Interface = (*Encoding)(nil)
  20. func (e *Encoding) String() string {
  21. return e.Name
  22. }
  23. func (e *Encoding) ID() (mib identifier.MIB, other string) {
  24. return e.MIB, ""
  25. }
  26. // SimpleEncoding is an Encoding that combines two Transformers.
  27. type SimpleEncoding struct {
  28. Decoder transform.Transformer
  29. Encoder transform.Transformer
  30. }
  31. func (e *SimpleEncoding) NewDecoder() *encoding.Decoder {
  32. return &encoding.Decoder{Transformer: e.Decoder}
  33. }
  34. func (e *SimpleEncoding) NewEncoder() *encoding.Encoder {
  35. return &encoding.Encoder{Transformer: e.Encoder}
  36. }
  37. // FuncEncoding is an Encoding that combines two functions returning a new
  38. // Transformer.
  39. type FuncEncoding struct {
  40. Decoder func() transform.Transformer
  41. Encoder func() transform.Transformer
  42. }
  43. func (e FuncEncoding) NewDecoder() *encoding.Decoder {
  44. return &encoding.Decoder{Transformer: e.Decoder()}
  45. }
  46. func (e FuncEncoding) NewEncoder() *encoding.Encoder {
  47. return &encoding.Encoder{Transformer: e.Encoder()}
  48. }
  49. // A RepertoireError indicates a rune is not in the repertoire of a destination
  50. // encoding. It is associated with an encoding-specific suggested replacement
  51. // byte.
  52. type RepertoireError byte
  53. // Error implements the error interrface.
  54. func (r RepertoireError) Error() string {
  55. return "encoding: rune not supported by encoding."
  56. }
  57. // Replacement returns the replacement string associated with this error.
  58. func (r RepertoireError) Replacement() byte { return byte(r) }
  59. var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub)