1 // Copyright 2013 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 //go:generate go run maketables.go
7 // Package charmap provides simple character encodings such as IBM Code Page 437
9 package charmap // import "golang.org/x/text/encoding/charmap"
14 "golang.org/x/text/encoding"
15 "golang.org/x/text/encoding/internal"
16 "golang.org/x/text/encoding/internal/identifier"
17 "golang.org/x/text/transform"
20 // These encodings vary only in the way clients should interpret them. Their
21 // coded character set is identical and a single implementation can be shared.
23 // ISO8859_6E is the ISO 8859-6E encoding.
24 ISO8859_6E encoding.Encoding = &iso8859_6E
26 // ISO8859_6I is the ISO 8859-6I encoding.
27 ISO8859_6I encoding.Encoding = &iso8859_6I
29 // ISO8859_8E is the ISO 8859-8E encoding.
30 ISO8859_8E encoding.Encoding = &iso8859_8E
32 // ISO8859_8I is the ISO 8859-8I encoding.
33 ISO8859_8I encoding.Encoding = &iso8859_8I
35 iso8859_6E = internal.Encoding{
38 MIB: identifier.ISO88596E,
41 iso8859_6I = internal.Encoding{
44 MIB: identifier.ISO88596I,
47 iso8859_8E = internal.Encoding{
50 MIB: identifier.ISO88598E,
53 iso8859_8I = internal.Encoding{
56 MIB: identifier.ISO88598I,
60 // All is a list of all defined encodings in this package.
61 var All []encoding.Encoding = listAll
63 // TODO: implement these encodings, in order of importance.
64 // ASCII, ISO8859_1: Rather common. Close to Windows 1252.
65 // ISO8859_9: Close to Windows 1254.
67 // utf8Enc holds a rune's UTF-8 encoding in data[:len].
73 // Charmap is an 8-bit character set encoding.
75 // name is the encoding's name.
77 // mib is the encoding type of this encoder.
79 // asciiSuperset states whether the encoding is a superset of ASCII.
81 // low is the lower bound of the encoded byte for a non-ASCII rune. If
82 // Charmap.asciiSuperset is true then this will be 0x80, otherwise 0x00.
84 // replacement is the encoded replacement character.
86 // decode is the map from encoded byte to UTF-8.
88 // encoding is the map from runes to encoded bytes. Each entry is a
89 // uint32: the high 8 bits are the encoded byte and the low 24 bits are
90 // the rune. The table entries are sorted by ascending rune.
94 // NewDecoder implements the encoding.Encoding interface.
95 func (m *Charmap) NewDecoder() *encoding.Decoder {
96 return &encoding.Decoder{Transformer: charmapDecoder{charmap: m}}
99 // NewEncoder implements the encoding.Encoding interface.
100 func (m *Charmap) NewEncoder() *encoding.Encoder {
101 return &encoding.Encoder{Transformer: charmapEncoder{charmap: m}}
104 // String returns the Charmap's name.
105 func (m *Charmap) String() string {
109 // ID implements an internal interface.
110 func (m *Charmap) ID() (mib identifier.MIB, other string) {
114 // charmapDecoder implements transform.Transformer by decoding to UTF-8.
115 type charmapDecoder struct {
116 transform.NopResetter
120 func (m charmapDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
121 for i, c := range src {
122 if m.charmap.asciiSuperset && c < utf8.RuneSelf {
123 if nDst >= len(dst) {
124 err = transform.ErrShortDst
133 decode := &m.charmap.decode[c]
135 if nDst+n > len(dst) {
136 err = transform.ErrShortDst
139 // It's 15% faster to avoid calling copy for these tiny slices.
140 for j := 0; j < n; j++ {
141 dst[nDst] = decode.data[j]
146 return nDst, nSrc, err
149 // DecodeByte returns the Charmap's rune decoding of the byte b.
150 func (m *Charmap) DecodeByte(b byte) rune {
151 switch x := &m.decode[b]; x.len {
153 return rune(x.data[0])
155 return rune(x.data[0]&0x1f)<<6 | rune(x.data[1]&0x3f)
157 return rune(x.data[0]&0x0f)<<12 | rune(x.data[1]&0x3f)<<6 | rune(x.data[2]&0x3f)
161 // charmapEncoder implements transform.Transformer by encoding from UTF-8.
162 type charmapEncoder struct {
163 transform.NopResetter
167 func (m charmapEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
168 r, size := rune(0), 0
170 for nSrc < len(src) {
171 if nDst >= len(dst) {
172 err = transform.ErrShortDst
177 // Decode a 1-byte rune.
178 if r < utf8.RuneSelf {
179 if m.charmap.asciiSuperset {
188 // Decode a multi-byte rune.
189 r, size = utf8.DecodeRune(src[nSrc:])
191 // All valid runes of size 1 (those below utf8.RuneSelf) were
192 // handled above. We have invalid UTF-8 or we haven't seen the
193 // full character yet.
194 if !atEOF && !utf8.FullRune(src[nSrc:]) {
195 err = transform.ErrShortSrc
197 err = internal.RepertoireError(m.charmap.replacement)
203 // Binary search in [low, high) for that rune in the m.charmap.encode table.
204 for low, high := int(m.charmap.low), 0x100; ; {
206 err = internal.RepertoireError(m.charmap.replacement)
209 mid := (low + high) / 2
210 got := m.charmap.encode[mid]
211 gotRune := rune(got & (1<<24 - 1))
214 } else if gotRune > r {
217 dst[nDst] = byte(got >> 24)
224 return nDst, nSrc, err
227 // EncodeRune returns the Charmap's byte encoding of the rune r. ok is whether
228 // r is in the Charmap's repertoire. If not, b is set to the Charmap's
229 // replacement byte. This is often the ASCII substitute character '\x1a'.
230 func (m *Charmap) EncodeRune(r rune) (b byte, ok bool) {
231 if r < utf8.RuneSelf && m.asciiSuperset {
234 for low, high := int(m.low), 0x100; ; {
236 return m.replacement, false
238 mid := (low + high) / 2
240 gotRune := rune(got & (1<<24 - 1))
243 } else if gotRune > r {
246 return byte(got >> 24), true