...
1
2
3
4
5
6
7
8
9 package charmap
10
11 import (
12 "unicode/utf8"
13
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"
18 )
19
20
21
22 var (
23
24 ISO8859_6E encoding.Encoding = &iso8859_6E
25
26
27 ISO8859_6I encoding.Encoding = &iso8859_6I
28
29
30 ISO8859_8E encoding.Encoding = &iso8859_8E
31
32
33 ISO8859_8I encoding.Encoding = &iso8859_8I
34
35 iso8859_6E = internal.Encoding{
36 Encoding: ISO8859_6,
37 Name: "ISO-8859-6E",
38 MIB: identifier.ISO88596E,
39 }
40
41 iso8859_6I = internal.Encoding{
42 Encoding: ISO8859_6,
43 Name: "ISO-8859-6I",
44 MIB: identifier.ISO88596I,
45 }
46
47 iso8859_8E = internal.Encoding{
48 Encoding: ISO8859_8,
49 Name: "ISO-8859-8E",
50 MIB: identifier.ISO88598E,
51 }
52
53 iso8859_8I = internal.Encoding{
54 Encoding: ISO8859_8,
55 Name: "ISO-8859-8I",
56 MIB: identifier.ISO88598I,
57 }
58 )
59
60
61 var All []encoding.Encoding = listAll
62
63
64
65
66
67
68 type utf8Enc struct {
69 len uint8
70 data [3]byte
71 }
72
73
74 type Charmap struct {
75
76 name string
77
78 mib identifier.MIB
79
80 asciiSuperset bool
81
82
83 low uint8
84
85 replacement byte
86
87 decode [256]utf8Enc
88
89
90
91 encode [256]uint32
92 }
93
94
95 func (m *Charmap) NewDecoder() *encoding.Decoder {
96 return &encoding.Decoder{Transformer: charmapDecoder{charmap: m}}
97 }
98
99
100 func (m *Charmap) NewEncoder() *encoding.Encoder {
101 return &encoding.Encoder{Transformer: charmapEncoder{charmap: m}}
102 }
103
104
105 func (m *Charmap) String() string {
106 return m.name
107 }
108
109
110 func (m *Charmap) ID() (mib identifier.MIB, other string) {
111 return m.mib, ""
112 }
113
114
115 type charmapDecoder struct {
116 transform.NopResetter
117 charmap *Charmap
118 }
119
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
125 break
126 }
127 dst[nDst] = c
128 nDst++
129 nSrc = i + 1
130 continue
131 }
132
133 decode := &m.charmap.decode[c]
134 n := int(decode.len)
135 if nDst+n > len(dst) {
136 err = transform.ErrShortDst
137 break
138 }
139
140 for j := 0; j < n; j++ {
141 dst[nDst] = decode.data[j]
142 nDst++
143 }
144 nSrc = i + 1
145 }
146 return nDst, nSrc, err
147 }
148
149
150 func (m *Charmap) DecodeByte(b byte) rune {
151 switch x := &m.decode[b]; x.len {
152 case 1:
153 return rune(x.data[0])
154 case 2:
155 return rune(x.data[0]&0x1f)<<6 | rune(x.data[1]&0x3f)
156 default:
157 return rune(x.data[0]&0x0f)<<12 | rune(x.data[1]&0x3f)<<6 | rune(x.data[2]&0x3f)
158 }
159 }
160
161
162 type charmapEncoder struct {
163 transform.NopResetter
164 charmap *Charmap
165 }
166
167 func (m charmapEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
168 r, size := rune(0), 0
169 loop:
170 for nSrc < len(src) {
171 if nDst >= len(dst) {
172 err = transform.ErrShortDst
173 break
174 }
175 r = rune(src[nSrc])
176
177
178 if r < utf8.RuneSelf {
179 if m.charmap.asciiSuperset {
180 nSrc++
181 dst[nDst] = uint8(r)
182 nDst++
183 continue
184 }
185 size = 1
186
187 } else {
188
189 r, size = utf8.DecodeRune(src[nSrc:])
190 if size == 1 {
191
192
193
194 if !atEOF && !utf8.FullRune(src[nSrc:]) {
195 err = transform.ErrShortSrc
196 } else {
197 err = internal.RepertoireError(m.charmap.replacement)
198 }
199 break
200 }
201 }
202
203
204 for low, high := int(m.charmap.low), 0x100; ; {
205 if low >= high {
206 err = internal.RepertoireError(m.charmap.replacement)
207 break loop
208 }
209 mid := (low + high) / 2
210 got := m.charmap.encode[mid]
211 gotRune := rune(got & (1<<24 - 1))
212 if gotRune < r {
213 low = mid + 1
214 } else if gotRune > r {
215 high = mid
216 } else {
217 dst[nDst] = byte(got >> 24)
218 nDst++
219 break
220 }
221 }
222 nSrc += size
223 }
224 return nDst, nSrc, err
225 }
226
227
228
229
230 func (m *Charmap) EncodeRune(r rune) (b byte, ok bool) {
231 if r < utf8.RuneSelf && m.asciiSuperset {
232 return byte(r), true
233 }
234 for low, high := int(m.low), 0x100; ; {
235 if low >= high {
236 return m.replacement, false
237 }
238 mid := (low + high) / 2
239 got := m.encode[mid]
240 gotRune := rune(got & (1<<24 - 1))
241 if gotRune < r {
242 low = mid + 1
243 } else if gotRune > r {
244 high = mid
245 } else {
246 return byte(got >> 24), true
247 }
248 }
249 }
250
View as plain text