...
1
2
3
4
5 package ianaindex
6
7 import (
8 "unicode"
9 "unicode/utf8"
10
11 "golang.org/x/text/encoding"
12 "golang.org/x/text/encoding/internal"
13 "golang.org/x/text/encoding/internal/identifier"
14 "golang.org/x/text/transform"
15 )
16
17 type asciiDecoder struct {
18 transform.NopResetter
19 }
20
21 func (d asciiDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
22 for _, c := range src {
23 if c > unicode.MaxASCII {
24 r := unicode.ReplacementChar
25 if nDst+utf8.RuneLen(r) > len(dst) {
26 err = transform.ErrShortDst
27 break
28 }
29 nDst += utf8.EncodeRune(dst[nDst:], r)
30 nSrc++
31 continue
32 }
33
34 if nDst >= len(dst) {
35 err = transform.ErrShortDst
36 break
37 }
38 dst[nDst] = c
39 nDst++
40 nSrc++
41 }
42 return nDst, nSrc, err
43 }
44
45 type asciiEncoder struct {
46 transform.NopResetter
47 }
48
49 func (d asciiEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
50 for _, c := range src {
51 if c > unicode.MaxASCII {
52 err = internal.RepertoireError(encoding.ASCIISub)
53 break
54 }
55
56 if nDst >= len(dst) {
57 err = transform.ErrShortDst
58 break
59 }
60 dst[nDst] = c
61 nDst++
62 nSrc++
63 }
64 return nDst, nSrc, err
65 }
66
67 var asciiEnc = &internal.Encoding{
68 Encoding: &internal.SimpleEncoding{
69 asciiDecoder{},
70 asciiEncoder{},
71 },
72 Name: "US-ASCII",
73 MIB: identifier.ASCII,
74 }
75
View as plain text