internal.go

 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
 5// Package internal contains code that is shared among encoding implementations.
 6package internal
 7
 8import (
 9	"golang.org/x/text/encoding"
10	"golang.org/x/text/encoding/internal/identifier"
11	"golang.org/x/text/transform"
12)
13
14// Encoding is an implementation of the Encoding interface that adds the String
15// and ID methods to an existing encoding.
16type Encoding struct {
17	encoding.Encoding
18	Name string
19	MIB  identifier.MIB
20}
21
22// _ verifies that Encoding implements identifier.Interface.
23var _ identifier.Interface = (*Encoding)(nil)
24
25func (e *Encoding) String() string {
26	return e.Name
27}
28
29func (e *Encoding) ID() (mib identifier.MIB, other string) {
30	return e.MIB, ""
31}
32
33// SimpleEncoding is an Encoding that combines two Transformers.
34type SimpleEncoding struct {
35	Decoder transform.Transformer
36	Encoder transform.Transformer
37}
38
39func (e *SimpleEncoding) NewDecoder() *encoding.Decoder {
40	return &encoding.Decoder{Transformer: e.Decoder}
41}
42
43func (e *SimpleEncoding) NewEncoder() *encoding.Encoder {
44	return &encoding.Encoder{Transformer: e.Encoder}
45}
46
47// FuncEncoding is an Encoding that combines two functions returning a new
48// Transformer.
49type FuncEncoding struct {
50	Decoder func() transform.Transformer
51	Encoder func() transform.Transformer
52}
53
54func (e FuncEncoding) NewDecoder() *encoding.Decoder {
55	return &encoding.Decoder{Transformer: e.Decoder()}
56}
57
58func (e FuncEncoding) NewEncoder() *encoding.Encoder {
59	return &encoding.Encoder{Transformer: e.Encoder()}
60}
61
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
64// byte.
65type RepertoireError byte
66
67// Error implements the error interface.
68func (r RepertoireError) Error() string {
69	return "encoding: rune not supported by encoding."
70}
71
72// Replacement returns the replacement string associated with this error.
73func (r RepertoireError) Replacement() byte { return byte(r) }
74
75var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub)