internal.go raw

   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.
   6  package internal
   7  
   8  import (
   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.
  16  type Encoding struct {
  17  	encoding.Encoding
  18  	Name string
  19  	MIB  identifier.MIB
  20  }
  21  
  22  // _ verifies that Encoding implements identifier.Interface.
  23  var _ identifier.Interface = (*Encoding)(nil)
  24  
  25  func (e *Encoding) String() string {
  26  	return e.Name
  27  }
  28  
  29  func (e *Encoding) ID() (mib identifier.MIB, other string) {
  30  	return e.MIB, ""
  31  }
  32  
  33  // SimpleEncoding is an Encoding that combines two Transformers.
  34  type SimpleEncoding struct {
  35  	Decoder transform.Transformer
  36  	Encoder transform.Transformer
  37  }
  38  
  39  func (e *SimpleEncoding) NewDecoder() *encoding.Decoder {
  40  	return &encoding.Decoder{Transformer: e.Decoder}
  41  }
  42  
  43  func (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.
  49  type FuncEncoding struct {
  50  	Decoder func() transform.Transformer
  51  	Encoder func() transform.Transformer
  52  }
  53  
  54  func (e FuncEncoding) NewDecoder() *encoding.Decoder {
  55  	return &encoding.Decoder{Transformer: e.Decoder()}
  56  }
  57  
  58  func (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.
  65  type RepertoireError byte
  66  
  67  // Error implements the error interface.
  68  func (r RepertoireError) Error() string {
  69  	return "encoding: rune not supported by encoding."
  70  }
  71  
  72  // Replacement returns the replacement string associated with this error.
  73  func (r RepertoireError) Replacement() byte { return byte(r) }
  74  
  75  var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub)
  76