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.
5 // Package internal contains code that is shared among encoding implementations.
9 "golang.org/x/text/encoding"
10 "golang.org/x/text/encoding/internal/identifier"
11 "golang.org/x/text/transform"
14 // Encoding is an implementation of the Encoding interface that adds the String
15 // and ID methods to an existing encoding.
16 type Encoding struct {
22 // _ verifies that Encoding implements identifier.Interface.
23 var _ identifier.Interface = (*Encoding)(nil)
25 func (e *Encoding) String() string {
29 func (e *Encoding) ID() (mib identifier.MIB, other string) {
33 // SimpleEncoding is an Encoding that combines two Transformers.
34 type SimpleEncoding struct {
35 Decoder transform.Transformer
36 Encoder transform.Transformer
39 func (e *SimpleEncoding) NewDecoder() *encoding.Decoder {
40 return &encoding.Decoder{Transformer: e.Decoder}
43 func (e *SimpleEncoding) NewEncoder() *encoding.Encoder {
44 return &encoding.Encoder{Transformer: e.Encoder}
47 // FuncEncoding is an Encoding that combines two functions returning a new
49 type FuncEncoding struct {
50 Decoder func() transform.Transformer
51 Encoder func() transform.Transformer
54 func (e FuncEncoding) NewDecoder() *encoding.Decoder {
55 return &encoding.Decoder{Transformer: e.Decoder()}
58 func (e FuncEncoding) NewEncoder() *encoding.Encoder {
59 return &encoding.Encoder{Transformer: e.Encoder()}
62 // A RepertoireError indicates a rune is not in the repertoire of a destination
63 // encoding. It is associated with an encoding-specific suggested replacement
65 type RepertoireError byte
67 // Error implements the error interrface.
68 func (r RepertoireError) Error() string {
69 return "encoding: rune not supported by encoding."
72 // Replacement returns the replacement string associated with this error.
73 func (r RepertoireError) Replacement() byte { return byte(r) }
75 var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub)