...
1
2
3
4
5 package encoding_test
6
7 import (
8 "fmt"
9 "io"
10 "os"
11 "strings"
12
13 "golang.org/x/text/encoding"
14 "golang.org/x/text/encoding/charmap"
15 "golang.org/x/text/encoding/unicode"
16 "golang.org/x/text/transform"
17 )
18
19 func ExampleDecodeWindows1252() {
20 sr := strings.NewReader("Gar\xe7on !")
21 tr := charmap.Windows1252.NewDecoder().Reader(sr)
22 io.Copy(os.Stdout, tr)
23
24 }
25
26 func ExampleUTF8Validator() {
27 for i := 0; i < 2; i++ {
28 var transformer transform.Transformer
29 transformer = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()
30 if i == 1 {
31 transformer = transform.Chain(encoding.UTF8Validator, transformer)
32 }
33 dst := make([]byte, 256)
34 src := []byte("abc\xffxyz")
35 nDst, nSrc, err := transformer.Transform(dst, src, true)
36 fmt.Printf("i=%d: produced %q, consumed %q, error %v\n",
37 i, dst[:nDst], src[:nSrc], err)
38 }
39
40
41
42 }
43
View as plain text